Skip to content

Commit

Permalink
Merge pull request #132 from jsr-core/fix-as-optional
Browse files Browse the repository at this point in the history
test: Fix type tests related to `asOptional` under `compilerOptions.exactOptionalPropertyTypes`
  • Loading branch information
lambdalisue authored Sep 7, 2024
2 parents 6f1acaa + 00449d9 commit 6cf6aeb
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 29 deletions.
2 changes: 1 addition & 1 deletion as/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const as: {
* });
* const a: unknown = {};
* if (isMyType(a)) {
* const _: {foo?: string} = a;
* const _: {foo?: string | undefined} = a;
* }
* ```
*/
Expand Down
2 changes: 1 addition & 1 deletion as/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
* });
* const a: unknown = {};
* if (isMyType(a)) {
* const _: {foo?: string} = a;
* const _: {foo?: string | undefined} = a;
* }
* ```
*/
Expand Down
12 changes: 10 additions & 2 deletions as/optional_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ Deno.test("asOptional<T>", async (t) => {
await t.step("predicated type is correct", () => {
const v: unknown = undefined;
if (pred(v)) {
assertType<Equal<typeof v, { a: number; b?: number; c?: number }>>(
assertType<
Equal<
typeof v,
{ a: number; b?: number | undefined; c?: number | undefined }
>
>(
true,
);
}
Expand Down Expand Up @@ -71,7 +76,10 @@ Deno.test("asOptional<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number?, number?]>
Equal<
typeof v,
[number, (number | undefined)?, (number | undefined)?]
>
>(
true,
);
Expand Down
3 changes: 3 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@core/unknownutil",
"version": "0.0.0",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"exports": {
".": "./mod.ts",
"./as": "./as/mod.ts",
Expand Down
8 changes: 4 additions & 4 deletions is/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
* }
* ```
*
Expand All @@ -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;
* }
* ```
*
Expand All @@ -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;
* }
* ```
*/
Expand Down Expand Up @@ -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;
* }
* ```
*/
Expand Down
12 changes: 6 additions & 6 deletions is/object_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ Deno.test("isObjectOf<T>", 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;
}
Expand Down Expand Up @@ -256,10 +256,10 @@ Deno.test("isObjectOf<T>", 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;
}
Expand Down
6 changes: 3 additions & 3 deletions is/parameters_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
* }
* ```
*
Expand All @@ -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;
* }
* ```
*
Expand All @@ -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;
* }
* ```
*/
Expand Down
18 changes: 16 additions & 2 deletions is/parameters_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ Deno.test("isParametersOf<T>", async (t) => {
const a: unknown = [0, "a"];
if (isParametersOf(predTup)(a)) {
assertType<
Equal<typeof a, [number | undefined, string, string?, boolean?]>
Equal<
typeof a,
[
number | undefined,
string,
(string | undefined)?,
(boolean | undefined)?,
]
>
>(true);
}
});
Expand Down Expand Up @@ -160,7 +168,13 @@ Deno.test("isParametersOf<T, R>", async (t) => {
assertType<
Equal<
typeof a,
[number | undefined, string, string?, boolean?, ...number[]]
[
number | undefined,
string,
(string | undefined)?,
(boolean | undefined)?,
...number[],
]
>
>(
true,
Expand Down
13 changes: 10 additions & 3 deletions is/partial_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ Deno.test("isPartialOf<T>", 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);
}
Expand Down Expand Up @@ -89,8 +96,8 @@ Deno.test("isPartialOf<T>", async (t) => {
typeof a,
Partial<{
a: number;
[b]: string;
[c]: boolean;
[b]: string | undefined;
[c]: boolean | undefined;
readonly [d]: string;
}>
>
Expand Down
2 changes: 1 addition & 1 deletion is/required_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
* }
* ```
*/
Expand Down
9 changes: 7 additions & 2 deletions is/required_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ Deno.test("isRequiredOf<T>", 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);
}
Expand Down Expand Up @@ -113,7 +118,7 @@ Deno.test("isRequiredOf<T>", async (t) => {
{
a: number;
[b]: string | undefined;
[c]: boolean;
[c]: boolean | undefined;
readonly [d]: string;
}
>
Expand Down
10 changes: 8 additions & 2 deletions is/strict_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ Deno.test("isStrictOf<T>", async (t) => {
const a: unknown = { a: 0, b: "a" };
if (isStrictOf(is.ObjectOf(predObj))(a)) {
assertType<
Equal<typeof a, { a: number; b: string | undefined; c?: boolean }>
Equal<
typeof a,
{ a: number; b: string | undefined; c?: boolean | undefined }
>
>(true);
}
});
Expand Down Expand Up @@ -258,7 +261,10 @@ Deno.test("isStrictOf<T>", async (t) => {
const a: unknown = { a: 0, [b]: "a" };
if (isStrictOf(is.ObjectOf(predObj))(a)) {
assertType<
Equal<typeof a, { a: number; [b]: string | undefined; [c]?: boolean }>
Equal<
typeof a,
{ a: number; [b]: string | undefined; [c]?: boolean | undefined }
>
>(true);
}
});
Expand Down
4 changes: 2 additions & 2 deletions type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* type Person = {
* name: string;
* age: number;
* address?: string;
* address?: string | undefined;
* };
* const isPerson = is.ObjectOf({
* name: is.String,
Expand All @@ -34,7 +34,7 @@ export type Predicate<T> = (x: unknown) => x is T;
* // type Person = {
* // name: string;
* // age: number;
* // address?: string;
* // address?: string | undefined;
* // };
* ```
*/
Expand Down

0 comments on commit 6cf6aeb

Please sign in to comment.