diff --git a/as/mod.ts b/as/mod.ts index cc78ac7..6b946e8 100644 --- a/as/mod.ts +++ b/as/mod.ts @@ -32,7 +32,7 @@ export const as: { * }); * const a: unknown = {}; * if (isMyType(a)) { - * const _: {foo?: string} = a; + * const _: {foo?: string | undefined} = a; * } * ``` */ diff --git a/as/optional.ts b/as/optional.ts index 53ea462..937c828 100644 --- a/as/optional.ts +++ b/as/optional.ts @@ -28,7 +28,7 @@ import { * }); * const a: unknown = {}; * if (isMyType(a)) { - * const _: {foo?: string} = a; + * const _: {foo?: string | undefined} = a; * } * ``` */ diff --git a/as/optional_test.ts b/as/optional_test.ts index 4fe66bf..8516acb 100644 --- a/as/optional_test.ts +++ b/as/optional_test.ts @@ -34,7 +34,12 @@ Deno.test("asOptional", async (t) => { await t.step("predicated type is correct", () => { const v: unknown = undefined; if (pred(v)) { - assertType>( + assertType< + Equal< + typeof v, + { a: number; b?: number | undefined; c?: number | undefined } + > + >( true, ); } @@ -71,7 +76,10 @@ Deno.test("asOptional", async (t) => { const v: unknown = undefined; if (pred(v)) { assertType< - Equal + Equal< + typeof v, + [number, (number | undefined)?, (number | undefined)?] + > >( true, ); diff --git a/deno.jsonc b/deno.jsonc index 7d37baf..7d5c5be 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,6 +1,9 @@ { "name": "@core/unknownutil", "version": "0.0.0", + "compilerOptions": { + "exactOptionalPropertyTypes": true + }, "exports": { ".": "./mod.ts", "./as": "./as/mod.ts", diff --git a/is/mod.ts b/is/mod.ts index 59c77da..0fd6be4 100644 --- a/is/mod.ts +++ b/is/mod.ts @@ -492,7 +492,7 @@ export const is: { * ] as const); * const a: unknown = [0, undefined, "a"]; * if (isMyType(a)) { - * const _: [number, string | undefined, boolean, number?, string?, boolean?] = a; + * const _: [number, string | undefined, boolean, (number | undefined)?, (string | undefined)?, (boolean | undefined)?] = a; * } * ``` * @@ -511,7 +511,7 @@ export const is: { * ); * const a: unknown = [0, "a", true, 0, 1, 2]; * if (isMyType(a)) { - * const _: [number, string?, boolean?, ...number[]] = a; + * const _: [number, (string | undefined)?, (boolean | undefined)?, ...number[]] = a; * } * ``` * @@ -525,7 +525,7 @@ export const is: { * const isMyType = is.ParametersOf(predTup); * const a: unknown = [0, "a"]; * if (isMyType(a)) { - * const _: [number, string, boolean?] = a; + * const _: [number, string, (boolean | undefined)?] = a; * } * ``` */ @@ -766,7 +766,7 @@ export const is: { * })); * const a: unknown = { a: 0, b: "b", c: true, other: "other" }; * if (isMyType(a)) { - * const _: { a: number; b: string | undefined; c: boolean } = a; + * const _: { a: number; b: string | undefined; c: boolean | undefined } = a; * } * ``` */ diff --git a/is/object_of_test.ts b/is/object_of_test.ts index 514ef4b..38eca30 100644 --- a/is/object_of_test.ts +++ b/is/object_of_test.ts @@ -89,10 +89,10 @@ Deno.test("isObjectOf", async (t) => { Equal< typeof a, { - readonly a?: string; - readonly b?: string; + readonly a?: string | undefined; + readonly b?: string | undefined; readonly c: string; - d?: string; + d?: string | undefined; e: string; f: string; } @@ -256,10 +256,10 @@ Deno.test("isObjectOf", async (t) => { Equal< typeof x, { - readonly [a]?: string; - readonly [b]?: string; + readonly [a]?: string | undefined; + readonly [b]?: string | undefined; readonly [c]: string; - [d]?: string; + [d]?: string | undefined; [e]: string; [f]: string; } diff --git a/is/parameters_of.ts b/is/parameters_of.ts index 39134dd..d89951e 100644 --- a/is/parameters_of.ts +++ b/is/parameters_of.ts @@ -25,7 +25,7 @@ import { isArray } from "./array.ts"; * ] as const); * const a: unknown = [0, undefined, "a"]; * if (isMyType(a)) { - * const _: [number, string | undefined, boolean, number?, string?, boolean?] = a; + * const _: [number, string | undefined, boolean, (number | undefined)?, (string | undefined)?, (boolean | undefined)?] = a; * } * ``` * @@ -44,7 +44,7 @@ import { isArray } from "./array.ts"; * ); * const a: unknown = [0, "a", true, 0, 1, 2]; * if (isMyType(a)) { - * const _: [number, string?, boolean?, ...number[]] = a; + * const _: [number, (string | undefined)?, (boolean | undefined)?, ...number[]] = a; * } * ``` * @@ -58,7 +58,7 @@ import { isArray } from "./array.ts"; * const isMyType = is.ParametersOf(predTup); * const a: unknown = [0, "a"]; * if (isMyType(a)) { - * const _: [number, string, boolean?] = a; + * const _: [number, string, (boolean | undefined)?] = a; * } * ``` */ diff --git a/is/parameters_of_test.ts b/is/parameters_of_test.ts index 6bf5840..bbeea8d 100644 --- a/is/parameters_of_test.ts +++ b/is/parameters_of_test.ts @@ -55,7 +55,15 @@ Deno.test("isParametersOf", async (t) => { const a: unknown = [0, "a"]; if (isParametersOf(predTup)(a)) { assertType< - Equal + Equal< + typeof a, + [ + number | undefined, + string, + (string | undefined)?, + (boolean | undefined)?, + ] + > >(true); } }); @@ -160,7 +168,13 @@ Deno.test("isParametersOf", async (t) => { assertType< Equal< typeof a, - [number | undefined, string, string?, boolean?, ...number[]] + [ + number | undefined, + string, + (string | undefined)?, + (boolean | undefined)?, + ...number[], + ] > >( true, diff --git a/is/partial_of_test.ts b/is/partial_of_test.ts index e0433a9..400d624 100644 --- a/is/partial_of_test.ts +++ b/is/partial_of_test.ts @@ -42,7 +42,14 @@ Deno.test("isPartialOf", async (t) => { assertType< Equal< typeof a, - Partial<{ a: number; b: string; c: boolean; readonly d: string }> + Partial< + { + a: number; + b: string | undefined; + c: boolean | undefined; + readonly d: string; + } + > > >(true); } @@ -89,8 +96,8 @@ Deno.test("isPartialOf", async (t) => { typeof a, Partial<{ a: number; - [b]: string; - [c]: boolean; + [b]: string | undefined; + [c]: boolean | undefined; readonly [d]: string; }> > diff --git a/is/required_of.ts b/is/required_of.ts index 77fc75f..e602091 100644 --- a/is/required_of.ts +++ b/is/required_of.ts @@ -30,7 +30,7 @@ import { isObjectOf } from "./object_of.ts"; * })); * const a: unknown = { a: 0, b: "b", c: true, other: "other" }; * if (isMyType(a)) { - * const _: { a: number; b: string | undefined; c: boolean } = a; + * const _: { a: number; b: string | undefined; c: boolean | undefined } = a; * } * ``` */ diff --git a/is/required_of_test.ts b/is/required_of_test.ts index 9cb4e40..a460130 100644 --- a/is/required_of_test.ts +++ b/is/required_of_test.ts @@ -52,7 +52,12 @@ Deno.test("isRequiredOf", async (t) => { assertType< Equal< typeof a, - { a: number; b: string | undefined; c: boolean; readonly d: string } + { + a: number; + b: string | undefined; + c: boolean | undefined; + readonly d: string; + } > >(true); } @@ -113,7 +118,7 @@ Deno.test("isRequiredOf", async (t) => { { a: number; [b]: string | undefined; - [c]: boolean; + [c]: boolean | undefined; readonly [d]: string; } > diff --git a/is/strict_of_test.ts b/is/strict_of_test.ts index 97cbeca..d8f5b11 100644 --- a/is/strict_of_test.ts +++ b/is/strict_of_test.ts @@ -150,7 +150,10 @@ Deno.test("isStrictOf", async (t) => { const a: unknown = { a: 0, b: "a" }; if (isStrictOf(is.ObjectOf(predObj))(a)) { assertType< - Equal + Equal< + typeof a, + { a: number; b: string | undefined; c?: boolean | undefined } + > >(true); } }); @@ -258,7 +261,10 @@ Deno.test("isStrictOf", async (t) => { const a: unknown = { a: 0, [b]: "a" }; if (isStrictOf(is.ObjectOf(predObj))(a)) { assertType< - Equal + Equal< + typeof a, + { a: number; [b]: string | undefined; [c]?: boolean | undefined } + > >(true); } }); diff --git a/type.ts b/type.ts index e53d27e..58bd4b1 100644 --- a/type.ts +++ b/type.ts @@ -7,7 +7,7 @@ * type Person = { * name: string; * age: number; - * address?: string; + * address?: string | undefined; * }; * const isPerson = is.ObjectOf({ * name: is.String, @@ -34,7 +34,7 @@ export type Predicate = (x: unknown) => x is T; * // type Person = { * // name: string; * // age: number; - * // address?: string; + * // address?: string | undefined; * // }; * ``` */