Skip to content

Commit 70bc9b5

Browse files
authored
Merge pull request #18 from anatelli10/an-optional-keys
Make nullable keys optional in object type definitions
2 parents 4fe1130 + f8a9ad3 commit 70bc9b5

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

dist/decoder.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export declare type Decoded<Model, AltErr = never> = Model extends Decoder<infer
1818
export declare type DecoderObject<Val, AltErr extends any> = {
1919
[Key in keyof Val]: ComposedDecoder<Val[Key], AltErr>;
2020
};
21+
/**
22+
* Treats nullable fields as optional
23+
* https://github.com/Microsoft/TypeScript/issues/12400#issuecomment-758523767
24+
*/
25+
export declare type OptionalNullable<T> = Optional<T> & Required<T>;
26+
declare type Optional<T> = Partial<Pick<T, KeysOfType<T, null | undefined>>>;
27+
declare type Required<T> = Omit<T, KeysOfType<T, null | undefined>>;
28+
declare type KeysOfType<T, SelectedType> = {
29+
[key in keyof T]: SelectedType extends T[key] ? key : never;
30+
}[keyof T];
2131
declare type PathElement = TypedObject | Index | ObjectKey | Array<any>;
2232
export declare class Index {
2333
index: number;
@@ -87,7 +97,7 @@ export declare function oneOf<Val, AltErr = never>(decoders: ReadonlyArray<Compo
8797
* });
8898
* ```
8999
*/
90-
export declare function object<Val, AltErr>(name: string, decoders: DecoderObject<Val, AltErr>): Decoder<Val, AltErr>;
100+
export declare function object<Val, AltErr>(name: string, decoders: DecoderObject<Val, AltErr>): Decoder<OptionalNullable<Val>, AltErr>;
91101
/**
92102
* Creates an intersection between two decoders. Equivalent to TypeScript's `&` operator.
93103
*

dist/decoder.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)