Skip to content

Commit 6cf6aeb

Browse files
authored
Merge pull request #132 from jsr-core/fix-as-optional
test: Fix type tests related to `asOptional` under `compilerOptions.exactOptionalPropertyTypes`
2 parents 6f1acaa + 00449d9 commit 6cf6aeb

13 files changed

+72
-29
lines changed

as/mod.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const as: {
3232
* });
3333
* const a: unknown = {};
3434
* if (isMyType(a)) {
35-
* const _: {foo?: string} = a;
35+
* const _: {foo?: string | undefined} = a;
3636
* }
3737
* ```
3838
*/

as/optional.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
* });
2929
* const a: unknown = {};
3030
* if (isMyType(a)) {
31-
* const _: {foo?: string} = a;
31+
* const _: {foo?: string | undefined} = a;
3232
* }
3333
* ```
3434
*/

as/optional_test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ Deno.test("asOptional<T>", async (t) => {
3434
await t.step("predicated type is correct", () => {
3535
const v: unknown = undefined;
3636
if (pred(v)) {
37-
assertType<Equal<typeof v, { a: number; b?: number; c?: number }>>(
37+
assertType<
38+
Equal<
39+
typeof v,
40+
{ a: number; b?: number | undefined; c?: number | undefined }
41+
>
42+
>(
3843
true,
3944
);
4045
}
@@ -71,7 +76,10 @@ Deno.test("asOptional<T>", async (t) => {
7176
const v: unknown = undefined;
7277
if (pred(v)) {
7378
assertType<
74-
Equal<typeof v, [number, number?, number?]>
79+
Equal<
80+
typeof v,
81+
[number, (number | undefined)?, (number | undefined)?]
82+
>
7583
>(
7684
true,
7785
);

deno.jsonc

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "@core/unknownutil",
33
"version": "0.0.0",
4+
"compilerOptions": {
5+
"exactOptionalPropertyTypes": true
6+
},
47
"exports": {
58
".": "./mod.ts",
69
"./as": "./as/mod.ts",

is/mod.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ export const is: {
492492
* ] as const);
493493
* const a: unknown = [0, undefined, "a"];
494494
* if (isMyType(a)) {
495-
* const _: [number, string | undefined, boolean, number?, string?, boolean?] = a;
495+
* const _: [number, string | undefined, boolean, (number | undefined)?, (string | undefined)?, (boolean | undefined)?] = a;
496496
* }
497497
* ```
498498
*
@@ -511,7 +511,7 @@ export const is: {
511511
* );
512512
* const a: unknown = [0, "a", true, 0, 1, 2];
513513
* if (isMyType(a)) {
514-
* const _: [number, string?, boolean?, ...number[]] = a;
514+
* const _: [number, (string | undefined)?, (boolean | undefined)?, ...number[]] = a;
515515
* }
516516
* ```
517517
*
@@ -525,7 +525,7 @@ export const is: {
525525
* const isMyType = is.ParametersOf(predTup);
526526
* const a: unknown = [0, "a"];
527527
* if (isMyType(a)) {
528-
* const _: [number, string, boolean?] = a;
528+
* const _: [number, string, (boolean | undefined)?] = a;
529529
* }
530530
* ```
531531
*/
@@ -766,7 +766,7 @@ export const is: {
766766
* }));
767767
* const a: unknown = { a: 0, b: "b", c: true, other: "other" };
768768
* if (isMyType(a)) {
769-
* const _: { a: number; b: string | undefined; c: boolean } = a;
769+
* const _: { a: number; b: string | undefined; c: boolean | undefined } = a;
770770
* }
771771
* ```
772772
*/

is/object_of_test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ Deno.test("isObjectOf<T>", async (t) => {
8989
Equal<
9090
typeof a,
9191
{
92-
readonly a?: string;
93-
readonly b?: string;
92+
readonly a?: string | undefined;
93+
readonly b?: string | undefined;
9494
readonly c: string;
95-
d?: string;
95+
d?: string | undefined;
9696
e: string;
9797
f: string;
9898
}
@@ -256,10 +256,10 @@ Deno.test("isObjectOf<T>", async (t) => {
256256
Equal<
257257
typeof x,
258258
{
259-
readonly [a]?: string;
260-
readonly [b]?: string;
259+
readonly [a]?: string | undefined;
260+
readonly [b]?: string | undefined;
261261
readonly [c]: string;
262-
[d]?: string;
262+
[d]?: string | undefined;
263263
[e]: string;
264264
[f]: string;
265265
}

is/parameters_of.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { isArray } from "./array.ts";
2525
* ] as const);
2626
* const a: unknown = [0, undefined, "a"];
2727
* if (isMyType(a)) {
28-
* const _: [number, string | undefined, boolean, number?, string?, boolean?] = a;
28+
* const _: [number, string | undefined, boolean, (number | undefined)?, (string | undefined)?, (boolean | undefined)?] = a;
2929
* }
3030
* ```
3131
*
@@ -44,7 +44,7 @@ import { isArray } from "./array.ts";
4444
* );
4545
* const a: unknown = [0, "a", true, 0, 1, 2];
4646
* if (isMyType(a)) {
47-
* const _: [number, string?, boolean?, ...number[]] = a;
47+
* const _: [number, (string | undefined)?, (boolean | undefined)?, ...number[]] = a;
4848
* }
4949
* ```
5050
*
@@ -58,7 +58,7 @@ import { isArray } from "./array.ts";
5858
* const isMyType = is.ParametersOf(predTup);
5959
* const a: unknown = [0, "a"];
6060
* if (isMyType(a)) {
61-
* const _: [number, string, boolean?] = a;
61+
* const _: [number, string, (boolean | undefined)?] = a;
6262
* }
6363
* ```
6464
*/

is/parameters_of_test.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ Deno.test("isParametersOf<T>", async (t) => {
5555
const a: unknown = [0, "a"];
5656
if (isParametersOf(predTup)(a)) {
5757
assertType<
58-
Equal<typeof a, [number | undefined, string, string?, boolean?]>
58+
Equal<
59+
typeof a,
60+
[
61+
number | undefined,
62+
string,
63+
(string | undefined)?,
64+
(boolean | undefined)?,
65+
]
66+
>
5967
>(true);
6068
}
6169
});
@@ -160,7 +168,13 @@ Deno.test("isParametersOf<T, R>", async (t) => {
160168
assertType<
161169
Equal<
162170
typeof a,
163-
[number | undefined, string, string?, boolean?, ...number[]]
171+
[
172+
number | undefined,
173+
string,
174+
(string | undefined)?,
175+
(boolean | undefined)?,
176+
...number[],
177+
]
164178
>
165179
>(
166180
true,

is/partial_of_test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ Deno.test("isPartialOf<T>", async (t) => {
4242
assertType<
4343
Equal<
4444
typeof a,
45-
Partial<{ a: number; b: string; c: boolean; readonly d: string }>
45+
Partial<
46+
{
47+
a: number;
48+
b: string | undefined;
49+
c: boolean | undefined;
50+
readonly d: string;
51+
}
52+
>
4653
>
4754
>(true);
4855
}
@@ -89,8 +96,8 @@ Deno.test("isPartialOf<T>", async (t) => {
8996
typeof a,
9097
Partial<{
9198
a: number;
92-
[b]: string;
93-
[c]: boolean;
99+
[b]: string | undefined;
100+
[c]: boolean | undefined;
94101
readonly [d]: string;
95102
}>
96103
>

is/required_of.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { isObjectOf } from "./object_of.ts";
3030
* }));
3131
* const a: unknown = { a: 0, b: "b", c: true, other: "other" };
3232
* if (isMyType(a)) {
33-
* const _: { a: number; b: string | undefined; c: boolean } = a;
33+
* const _: { a: number; b: string | undefined; c: boolean | undefined } = a;
3434
* }
3535
* ```
3636
*/

is/required_of_test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ Deno.test("isRequiredOf<T>", async (t) => {
5252
assertType<
5353
Equal<
5454
typeof a,
55-
{ a: number; b: string | undefined; c: boolean; readonly d: string }
55+
{
56+
a: number;
57+
b: string | undefined;
58+
c: boolean | undefined;
59+
readonly d: string;
60+
}
5661
>
5762
>(true);
5863
}
@@ -113,7 +118,7 @@ Deno.test("isRequiredOf<T>", async (t) => {
113118
{
114119
a: number;
115120
[b]: string | undefined;
116-
[c]: boolean;
121+
[c]: boolean | undefined;
117122
readonly [d]: string;
118123
}
119124
>

is/strict_of_test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ Deno.test("isStrictOf<T>", async (t) => {
150150
const a: unknown = { a: 0, b: "a" };
151151
if (isStrictOf(is.ObjectOf(predObj))(a)) {
152152
assertType<
153-
Equal<typeof a, { a: number; b: string | undefined; c?: boolean }>
153+
Equal<
154+
typeof a,
155+
{ a: number; b: string | undefined; c?: boolean | undefined }
156+
>
154157
>(true);
155158
}
156159
});
@@ -258,7 +261,10 @@ Deno.test("isStrictOf<T>", async (t) => {
258261
const a: unknown = { a: 0, [b]: "a" };
259262
if (isStrictOf(is.ObjectOf(predObj))(a)) {
260263
assertType<
261-
Equal<typeof a, { a: number; [b]: string | undefined; [c]?: boolean }>
264+
Equal<
265+
typeof a,
266+
{ a: number; [b]: string | undefined; [c]?: boolean | undefined }
267+
>
262268
>(true);
263269
}
264270
});

type.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* type Person = {
88
* name: string;
99
* age: number;
10-
* address?: string;
10+
* address?: string | undefined;
1111
* };
1212
* const isPerson = is.ObjectOf({
1313
* name: is.String,
@@ -34,7 +34,7 @@ export type Predicate<T> = (x: unknown) => x is T;
3434
* // type Person = {
3535
* // name: string;
3636
* // age: number;
37-
* // address?: string;
37+
* // address?: string | undefined;
3838
* // };
3939
* ```
4040
*/

0 commit comments

Comments
 (0)