Skip to content

Commit db8bfe6

Browse files
committed
test: add type test for OptionalParameter transform
1 parent f5b89a6 commit db8bfe6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

packages/types/src/util.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { Exact, OptionalParameter } from "./util";
2+
3+
type Assignable<LHS, RHS> = [RHS] extends [LHS] ? true : false;
4+
5+
type OptionalInput = {
6+
key?: string;
7+
optional?: string;
8+
};
9+
10+
type RequiredInput = {
11+
key: string | undefined;
12+
optional?: string;
13+
};
14+
15+
{
16+
// optional parameter transform of an optional input is not equivalent to exactly 1 parameter.
17+
type A = [...OptionalParameter<OptionalInput>];
18+
type B = [OptionalInput];
19+
type C = [OptionalInput] | [];
20+
21+
const assert1: Exact<A, B> = false as const;
22+
const assert2: Exact<A, C> = true as const;
23+
24+
const assert3: Assignable<A, []> = true as const;
25+
const assert4: A = [];
26+
27+
const assert5: Assignable<A, [{ key: "" }]> = true as const;
28+
const assert6: A = [{ key: "" }];
29+
}
30+
31+
{
32+
// optional parameter transform of a required input is equivalent to exactly 1 parameter.
33+
type A = [...OptionalParameter<RequiredInput>];
34+
type B = [RequiredInput];
35+
36+
const assert1: Exact<A, B> = true as const;
37+
const assert2: Assignable<A, []> = false as const;
38+
const assert3: Assignable<A, [{ key: "" }]> = true as const;
39+
const assert4: A = [{ key: "" }];
40+
}

0 commit comments

Comments
 (0)