-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix nested object schema type inference
- Loading branch information
1 parent
da8e0cc
commit b4d9675
Showing
9 changed files
with
88 additions
and
24 deletions.
There are no files selected for viewing
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,39 @@ | ||
import { Config } from './Config'; | ||
import type { Equals, Expect } from './types'; | ||
|
||
describe('Config', () => { | ||
it('should parse nested object', () => { | ||
const config = new Config({ | ||
s: Config.string(), | ||
n: Config.number().require(), | ||
foo: Config.object({ | ||
foo1: Config.string().require(), | ||
foo2: Config.object({ foo3: Config.boolean() }), | ||
}), | ||
}) | ||
.parse({ | ||
s: 's', | ||
n: 0, | ||
foo: { foo1: 'foo1', foo2: { foo3: false } }, | ||
}) | ||
.getAll(); | ||
|
||
expect(config).toEqual({ | ||
s: 's', | ||
n: 0, | ||
foo: { foo1: 'foo1', foo2: { foo3: false } }, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
type Test = Expect< | ||
Equals< | ||
typeof config, | ||
{ | ||
s: string | undefined; | ||
n: number; | ||
foo: { foo1: string; foo2: { foo3: boolean | undefined } }; | ||
} | ||
> | ||
>; | ||
}); | ||
}); |
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 was deleted.
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
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './Config'; | ||
export type { InferType } from './InferType'; | ||
export type { InferSchema } from './types'; |
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,32 @@ | ||
import type { Schema, StringSchema } from './Schema'; | ||
import type { ObjectSchema } from './Schema/ObjectSchema'; | ||
|
||
export type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; // eslint-disable-line @typescript-eslint/ban-types | ||
|
||
export type InferObjectSchema<T extends ObjectSchema> = T extends ObjectSchema< | ||
infer G | ||
> | ||
? G extends Record<string, Schema> | ||
? Prettify<InferSchema<G>> | ||
: never | ||
: never; | ||
|
||
export type InferSchema<T extends Record<string, Schema>> = { | ||
[K in keyof T]: T[K] extends ObjectSchema | ||
? InferObjectSchema<T[K]> | ||
: T[K]['isRequired'] extends true | ||
? NonNullable<T[K]['value']> | ||
: T[K]['value']; | ||
}; | ||
|
||
export type Expect<T extends true> = T; | ||
|
||
export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends < | ||
T, | ||
>() => T extends Y ? 1 : 2 | ||
? true | ||
: false; | ||
|
||
export type RequiredSchema<T extends Schema> = T & { isRequired: true }; |