-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Konrad Jamrozik
committed
Jul 12, 2024
1 parent
5911849
commit e2e30ed
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// https://stackoverflow.com/questions/78739104/how-can-i-avoid-duplicating-key-in-value-and-duplicating-type-and-const-definit | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import _ from 'lodash' | ||
import { describe, expect, test } from 'vitest' | ||
import { z } from 'zod' | ||
|
||
// type Name = 'foo' | 'bar' | ||
|
||
// type ProviderFuncFromString = (data: string) => { name: Name; data: string } | ||
|
||
// type ProviderFuncFromNumber = (numb: number) => { name: Name; data: string } | ||
|
||
// function providerFuncFromStringFactory(name: Name): ProviderFuncFromString { | ||
// return (data) => ({ name, data }) | ||
// } | ||
|
||
// function providerFuncFromNumberFactory(name: Name): ProviderFuncFromNumber { | ||
// return (numb) => ({ name, data: numb.toString() }) | ||
// } | ||
|
||
// type ProviderMap = { | ||
// foo: ProviderFuncFromString | ||
// bar: ProviderFuncFromNumber | ||
// } | ||
|
||
// const providerMap: ProviderMap = { | ||
// foo: providerFuncFromStringFactory('foo'), | ||
// bar: providerFuncFromNumberFactory('bar'), | ||
// } | ||
|
||
const NameSchema = z.enum(['foo', 'bar']) | ||
|
||
const ProviderFuncFromStringSchema = z | ||
.function() | ||
.args(z.string()) | ||
.returns( | ||
z.object({ | ||
name: NameSchema, | ||
data: z.string(), | ||
}), | ||
) | ||
|
||
type ProviderFuncFromStringType = z.infer<typeof ProviderFuncFromStringSchema> | ||
|
||
const ProviderFuncFromNumberSchema = z | ||
.function() | ||
.args(z.number()) | ||
.returns( | ||
z.object({ | ||
name: NameSchema, | ||
data: z.string(), | ||
}), | ||
) | ||
|
||
type ProviderFuncFromNumberType = z.infer<typeof ProviderFuncFromNumberSchema> | ||
|
||
function providerFuncFromStringFactory( | ||
name: z.infer<typeof NameSchema>, | ||
): ProviderFuncFromStringType { | ||
return (data: string) => ({ name, data }) | ||
} | ||
|
||
function providerFuncFromNumberFactory( | ||
name: z.infer<typeof NameSchema>, | ||
): ProviderFuncFromNumberType { | ||
return (numb: number) => ({ name, data: numb.toString() }) | ||
} | ||
|
||
const ProviderMapSchema = z.object({ | ||
foo: ProviderFuncFromStringSchema, | ||
bar: ProviderFuncFromNumberSchema, | ||
}) | ||
|
||
const providerMap1 = { | ||
foo: providerFuncFromStringFactory('foo'), | ||
bar: providerFuncFromNumberFactory('bar'), | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const providerMap2 = Object.fromEntries([ | ||
['foo', providerFuncFromNumberFactory('foo')], | ||
['bar', providerFuncFromNumberFactory('bar')], | ||
]) | ||
|
||
describe('scratchpad tests', () => { | ||
test('test scratchpad', () => { | ||
const parsedProviderMap1 = ProviderMapSchema.safeParse(providerMap1) | ||
const parsedProviderMap2 = ProviderMapSchema.safeParse(providerMap2) | ||
expect(parsedProviderMap1.success).toBe(true) | ||
expect(parsedProviderMap2.success).toBe(false) | ||
Check failure on line 90 in web/test/lib_tests/scratchpad.test.ts GitHub Actions / Testtest/lib_tests/scratchpad.test.ts > scratchpad tests > test scratchpad
Check failure on line 90 in web/test/lib_tests/scratchpad.test.ts GitHub Actions / Testtest/lib_tests/scratchpad.test.ts > scratchpad tests > test scratchpad
|
||
}) | ||
}) |