Skip to content

Commit feb5235

Browse files
committed
implementation sketch of dependent-type-like functions
1 parent 396e7d5 commit feb5235

File tree

6 files changed

+1403
-39
lines changed

6 files changed

+1403
-39
lines changed

src/compiler/checker.ts

Lines changed: 226 additions & 39 deletions
Large diffs are not rendered by default.
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
tests/cases/compiler/depTypeLikeFun.ts(30,9): error TS2322: Type 'true' is not assignable to type 'number'.
2+
tests/cases/compiler/depTypeLikeFun.ts(33,9): error TS2322: Type '1' is not assignable to type 'boolean'.
3+
tests/cases/compiler/depTypeLikeFun.ts(55,9): error TS2322: Type '1' is not assignable to type 'F[X]'.
4+
Type '1' is not assignable to type 'never'.
5+
tests/cases/compiler/depTypeLikeFun.ts(58,9): error TS2322: Type 'true' is not assignable to type 'F[X]'.
6+
Type 'true' is not assignable to type 'never'.
7+
tests/cases/compiler/depTypeLikeFun.ts(65,15): error TS2322: Type 'F[T]' is not assignable to type 'number'.
8+
Type 'number | boolean' is not assignable to type 'number'.
9+
Type 'false' is not assignable to type 'number'.
10+
tests/cases/compiler/depTypeLikeFun.ts(78,9): error TS2322: Type '2' is not assignable to type 'F[T]'.
11+
Type '2' is not assignable to type 'never'.
12+
tests/cases/compiler/depTypeLikeFun.ts(91,11): error TS2322: Type '1' is not assignable to type 'F[X]'.
13+
Type '1' is not assignable to type 'never'.
14+
tests/cases/compiler/depTypeLikeFun.ts(94,11): error TS2322: Type 'true' is not assignable to type 'F[X]'.
15+
Type 'true' is not assignable to type 'never'.
16+
tests/cases/compiler/depTypeLikeFun.ts(112,7): error TS2322: Type '{ a: true; b: 1; }' is not assignable to type 'Complex<X>'.
17+
Types of property 'a' are incompatible.
18+
Type 'true' is not assignable to type 'number'.
19+
tests/cases/compiler/depTypeLikeFun.ts(115,7): error TS2322: Type '{ a: 1; b: true; }' is not assignable to type 'Complex<X>'.
20+
Types of property 'a' are incompatible.
21+
Type '1' is not assignable to type 'boolean'.
22+
tests/cases/compiler/depTypeLikeFun.ts(122,5): error TS2322: Type '(ft: number) => 1' is not assignable to type '(ft: F[T]) => F[T]'.
23+
Types of parameters 'ft' and 'ft' are incompatible.
24+
Type 'F[T]' is not assignable to type 'number'.
25+
Type 'number | boolean' is not assignable to type 'number'.
26+
Type 'false' is not assignable to type 'number'.
27+
tests/cases/compiler/depTypeLikeFun.ts(127,5): error TS2322: Type '(ft: F[T]) => true' is not assignable to type '(ft: F[T]) => F[T]'.
28+
Type 'true' is not assignable to type 'F[T]'.
29+
Type 'true' is not assignable to type 'never'.
30+
31+
32+
==== tests/cases/compiler/depTypeLikeFun.ts (12 errors) ====
33+
type F = {
34+
t: number,
35+
f: boolean,
36+
}
37+
38+
type G = {
39+
a: number,
40+
b: boolean,
41+
c: string,
42+
}
43+
44+
type Complex<X extends "t" | "f"> = {
45+
a: { t: number, f: boolean }[X],
46+
b: { t: boolean, f: number }[X],
47+
}
48+
49+
function f1<X extends "t" | "f">(x: X): F[X] {
50+
if (x === "t") {
51+
// no error
52+
return 1;
53+
} else {
54+
// no error
55+
return true;
56+
}
57+
}
58+
59+
function f2<X extends "t" | "f">(x: X): F[X] {
60+
if (x === "t") {
61+
// error
62+
return true;
63+
~~~~~~~~~~~~
64+
!!! error TS2322: Type 'true' is not assignable to type 'number'.
65+
} else {
66+
// error
67+
return 1;
68+
~~~~~~~~~
69+
!!! error TS2322: Type '1' is not assignable to type 'boolean'.
70+
}
71+
}
72+
73+
function f3<X extends "a" | "b" | "c">(x: X): G[X] {
74+
if (x === "a") {
75+
// no error
76+
return 1;
77+
} else {
78+
if (x === "b") {
79+
// no error
80+
return true;
81+
} else {
82+
// no error
83+
return "z";
84+
}
85+
}
86+
}
87+
88+
function f4<X extends "t" | "f", Y extends "t" | "f">(x: X, y: Y): F[X] {
89+
if (y === "t") {
90+
// error
91+
return 1;
92+
~~~~~~~~~
93+
!!! error TS2322: Type '1' is not assignable to type 'F[X]'.
94+
!!! error TS2322: Type '1' is not assignable to type 'never'.
95+
} else {
96+
// error
97+
return true;
98+
~~~~~~~~~~~~
99+
!!! error TS2322: Type 'true' is not assignable to type 'F[X]'.
100+
!!! error TS2322: Type 'true' is not assignable to type 'never'.
101+
}
102+
}
103+
104+
function f5<T extends "t" | "f">(str: T, ft: F[T]): F[T] {
105+
if (str === "t") {
106+
// error
107+
const n: number = ft;
108+
~
109+
!!! error TS2322: Type 'F[T]' is not assignable to type 'number'.
110+
!!! error TS2322: Type 'number | boolean' is not assignable to type 'number'.
111+
!!! error TS2322: Type 'false' is not assignable to type 'number'.
112+
// no error
113+
return 1;
114+
} else {
115+
// no error
116+
return true;
117+
}
118+
}
119+
120+
declare var obj: F;
121+
function f6<T extends "t" | "f">(str: T, str2: T): F[T] {
122+
if (str === "t") {
123+
// error
124+
obj[str2] = 2;
125+
~~~~~~~~~
126+
!!! error TS2322: Type '2' is not assignable to type 'F[T]'.
127+
!!! error TS2322: Type '2' is not assignable to type 'never'.
128+
// no error
129+
return 1;
130+
} else {
131+
// no error
132+
return true;
133+
}
134+
}
135+
136+
class C7<X extends "t" | "f"> {
137+
f7(x: X): F[X] {
138+
if (x === "t") {
139+
// error
140+
return 1;
141+
~~~~~~~~~
142+
!!! error TS2322: Type '1' is not assignable to type 'F[X]'.
143+
!!! error TS2322: Type '1' is not assignable to type 'never'.
144+
} else {
145+
// error
146+
return true;
147+
~~~~~~~~~~~~
148+
!!! error TS2322: Type 'true' is not assignable to type 'F[X]'.
149+
!!! error TS2322: Type 'true' is not assignable to type 'never'.
150+
}
151+
}
152+
}
153+
154+
function f8<X extends "t" | "f">(x: X): Complex<X> {
155+
if (x === "t") {
156+
// no error
157+
return { a: 1, b: true };
158+
} else {
159+
// no error
160+
return { a: true, b: 1 };
161+
}
162+
}
163+
164+
function f9<X extends "t" | "f">(x: X): Complex<X> {
165+
if (x === "t") {
166+
// error
167+
return { a: true, b: 1 };
168+
~~~~~~~~~~~~~~~~~~~~~~~~~
169+
!!! error TS2322: Type '{ a: true; b: 1; }' is not assignable to type 'Complex<X>'.
170+
!!! error TS2322: Types of property 'a' are incompatible.
171+
!!! error TS2322: Type 'true' is not assignable to type 'number'.
172+
} else {
173+
// error
174+
return { a: 1, b: true };
175+
~~~~~~~~~~~~~~~~~~~~~~~~~
176+
!!! error TS2322: Type '{ a: 1; b: true; }' is not assignable to type 'Complex<X>'.
177+
!!! error TS2322: Types of property 'a' are incompatible.
178+
!!! error TS2322: Type '1' is not assignable to type 'boolean'.
179+
}
180+
}
181+
182+
function f10<T extends "t" | "f">(str: T): (ft: F[T]) => F[T] {
183+
if (str === "t") {
184+
// error
185+
return (ft: number) => {
186+
~~~~~~~~~~~~~~~~~~~~~~~~
187+
return 1;
188+
~~~~~~~~~~~~~~~~~
189+
};
190+
~~~~~~
191+
!!! error TS2322: Type '(ft: number) => 1' is not assignable to type '(ft: F[T]) => F[T]'.
192+
!!! error TS2322: Types of parameters 'ft' and 'ft' are incompatible.
193+
!!! error TS2322: Type 'F[T]' is not assignable to type 'number'.
194+
!!! error TS2322: Type 'number | boolean' is not assignable to type 'number'.
195+
!!! error TS2322: Type 'false' is not assignable to type 'number'.
196+
} else {
197+
// error
198+
return (ft: F[T]) => {
199+
~~~~~~~~~~~~~~~~~~~~~~
200+
return true;
201+
~~~~~~~~~~~~~~~~~~~~
202+
};
203+
~~~~~~
204+
!!! error TS2322: Type '(ft: F[T]) => true' is not assignable to type '(ft: F[T]) => F[T]'.
205+
!!! error TS2322: Type 'true' is not assignable to type 'F[T]'.
206+
!!! error TS2322: Type 'true' is not assignable to type 'never'.
207+
}
208+
}

0 commit comments

Comments
 (0)