Skip to content

Commit

Permalink
test: 🧪 testOptionalKeys (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beraliv authored Sep 29, 2021
1 parent aa879ca commit f44466d
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,18 +909,29 @@ function testPickProperties() {
}

function testOptionalKeys() {
type Input = {
req: string;
opt?: string;
opt2?: string;
undef: string | undefined;
nullable: string | null;
};

type Expected = "opt" | "opt2";
type Actual = OptionalKeys<Input>;

type Test = Assert<IsExact<Expected, Actual>>;
type cases = [
// @ts-expect-error converts to Number and gets its optional keys
Assert<IsExact<OptionalKeys<number>, never>>,
// @ts-expect-error converts to String and gets its optional keys
Assert<IsExact<OptionalKeys<string>, never>>,
// wtf?
Assert<IsExact<OptionalKeys<boolean>, () => boolean>>,
// @ts-expect-error converts to BigInt and gets its optional keys
Assert<IsExact<OptionalKeys<bigint>, never>>,
// wtf?
Assert<IsExact<OptionalKeys<symbol>, string | ((hint: string) => symbol) | (() => string) | (() => symbol)>>,
Assert<IsExact<OptionalKeys<undefined>, never>>,
Assert<IsExact<OptionalKeys<null>, never>>,
Assert<IsExact<OptionalKeys<Function>, never>>,
Assert<IsExact<OptionalKeys<Date>, never>>,
Assert<IsExact<OptionalKeys<Error>, "stack">>,
Assert<IsExact<OptionalKeys<RegExp>, never>>,
Assert<IsExact<OptionalKeys<{}>, never>>,
Assert<IsExact<OptionalKeys<{ a: 1 }>, never>>,
Assert<IsExact<OptionalKeys<{ a?: 1 }>, "a">>,
Assert<IsExact<OptionalKeys<{ a: 1 | undefined }>, never>>,
Assert<IsExact<OptionalKeys<{ a: 1 | null }>, never>>,
];
}

function testRequiredKeys() {
Expand Down

0 comments on commit f44466d

Please sign in to comment.