Skip to content

Commit 4b18a27

Browse files
committed
Add tests
1 parent 40ac892 commit 4b18a27

9 files changed

+1645
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(14,22): error TS2345: Argument of type '"value"' is not assignable to parameter of type 'XX extends XX ? "value" : never'.
2+
Type '"value"' is not assignable to type 'never'.
3+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(32,20): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
4+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(33,21): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
5+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(37,24): error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'.
6+
Type 'null' is not assignable to type 'null extends T | null ? any : never'.
7+
Type 'null' is not assignable to type 'never'.
8+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(43,5): error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'T extends T ? { x: T; y: T; } : never'.
9+
Type '{ x: T; y: T; }' is not assignable to type 'never'.
10+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(50,5): error TS2322: Type 'string' is not assignable to type 'Foo<T>'.
11+
Type 'string' is not assignable to type '"a"'.
12+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(64,11): error TS2322: Type '[T] extends [string] ? { y: number; } : { a: number; b: number; }' is not assignable to type '[T] extends [number] ? [T] extends [string] ? { y: number; } : { a: number; } : [T] extends [string] ? { y: number; } : { b: number; }'.
13+
Type '{ y: number; } | { a: number; b: number; }' is not assignable to type '[T] extends [number] ? [T] extends [string] ? { y: number; } : { a: number; } : [T] extends [string] ? { y: number; } : { b: number; }'.
14+
Type '{ y: number; }' is not assignable to type '[T] extends [number] ? [T] extends [string] ? { y: number; } : { a: number; } : [T] extends [string] ? { y: number; } : { b: number; }'.
15+
Type '{ y: number; }' is not assignable to type '([T] extends [string] ? { y: number; } : { a: number; }) & ([T] extends [string] ? { y: number; } : { b: number; })'.
16+
Type '{ y: number; }' is not assignable to type '[T] extends [string] ? { y: number; } : { a: number; }'.
17+
Type '{ y: number; }' is not assignable to type '{ y: number; } & { a: number; }'.
18+
Property 'a' is missing in type '{ y: number; }' but required in type '{ a: number; }'.
19+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(111,5): error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot<Q>'.
20+
Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot<Q>'.
21+
Type '(arg: any) => any' is not assignable to type 'never'.
22+
Type 'Q' is not assignable to type 'never'.
23+
Type '(arg: any) => any' is not assignable to type 'never'.
24+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(131,1): error TS2322: Type 'true' is not assignable to type 'false'.
25+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(132,1): error TS2322: Type 'false' is not assignable to type 'true'.
26+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(136,1): error TS2322: Type 'false' is not assignable to type 'true'.
27+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(137,1): error TS2322: Type 'true' is not assignable to type 'false'.
28+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(147,5): error TS2322: Type 'T extends "a" ? true : false' is not assignable to type 'false'.
29+
Type 'boolean' is not assignable to type 'false'.
30+
Type 'true' is not assignable to type 'false'.
31+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(148,5): error TS2322: Type 'T extends "b" ? true : false' is not assignable to type 'true'.
32+
Type 'boolean' is not assignable to type 'true'.
33+
Type 'false' is not assignable to type 'true'.
34+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(152,5): error TS2322: Type 'false' is not assignable to type 'T extends "a" ? true : false'.
35+
Type 'false' is not assignable to type 'never'.
36+
Type 'false' is not assignable to type 'true'.
37+
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(153,5): error TS2322: Type 'true' is not assignable to type 'T extends "b" ? true : false'.
38+
Type 'true' is not assignable to type 'never'.
39+
Type 'true' is not assignable to type 'false'.
40+
41+
42+
==== tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts (16 errors) ====
43+
// #29505
44+
45+
export type FilterPropsByType<T, TT> = {
46+
[K in keyof T]: T[K] extends TT ? K : never
47+
}[keyof T];
48+
49+
function select<
50+
T extends string | number,
51+
TList extends object,
52+
TValueProp extends FilterPropsByType<TList, T>
53+
>(property: T, list: TList[], valueProp: TValueProp) {}
54+
55+
<XX extends string>(x: XX, tipos: { value: XX }[]) => {
56+
select(x, tipos, "value");
57+
~~~~~~~
58+
!!! error TS2345: Argument of type '"value"' is not assignable to parameter of type 'XX extends XX ? "value" : never'.
59+
!!! error TS2345: Type '"value"' is not assignable to type 'never'.
60+
};
61+
62+
// #29662
63+
64+
declare function onlyNullablePlease<T extends null extends T ? any : never>(
65+
value: T
66+
): void;
67+
68+
declare function onlyNullablePlease2<
69+
T extends [null] extends [T] ? any : never
70+
>(value: T): void;
71+
72+
declare var z: string | null;
73+
onlyNullablePlease(z); // works as expected
74+
onlyNullablePlease2(z); // works as expected
75+
76+
declare var y: string;
77+
onlyNullablePlease(y); // error as expected
78+
~
79+
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
80+
onlyNullablePlease2(y); // error as expected
81+
~
82+
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
83+
84+
<T>(t: T) => {
85+
var x: T | null = Math.random() > 0.5 ? null : t;
86+
onlyNullablePlease(x); // should work
87+
~
88+
!!! error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'.
89+
!!! error TS2345: Type 'null' is not assignable to type 'null extends T | null ? any : never'.
90+
!!! error TS2345: Type 'null' is not assignable to type 'never'.
91+
onlyNullablePlease2(x); // should work
92+
};
93+
94+
<T>(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) => {
95+
t1 = t2; // OK
96+
t2 = t1; // should fail
97+
~~
98+
!!! error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'T extends T ? { x: T; y: T; } : never'.
99+
!!! error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'never'.
100+
};
101+
102+
type Foo<T> = T extends true ? string : "a";
103+
104+
<T>(x: Foo<T>, s: string) => {
105+
x = "a"; // Currently an error, should be ok
106+
x = s; // Error
107+
~
108+
!!! error TS2322: Type 'string' is not assignable to type 'Foo<T>'.
109+
!!! error TS2322: Type 'string' is not assignable to type '"a"'.
110+
};
111+
112+
// #26933
113+
114+
type Distributive<T> = T extends { a: number } ? { a: number } : { b: number };
115+
<T>() => {
116+
const o = { a: 1, b: 2 };
117+
const x: [T] extends [string]
118+
? { y: number }
119+
: { a: number; b: number } = undefined!;
120+
// Simple case: OK
121+
const o1: [T] extends [number] ? { a: number } : { b: number } = o;
122+
// Simple case where source happens to be a conditional type: also OK
123+
const x1: [T] extends [number]
124+
~~
125+
!!! error TS2322: Type '[T] extends [string] ? { y: number; } : { a: number; b: number; }' is not assignable to type '[T] extends [number] ? [T] extends [string] ? { y: number; } : { a: number; } : [T] extends [string] ? { y: number; } : { b: number; }'.
126+
!!! error TS2322: Type '{ y: number; } | { a: number; b: number; }' is not assignable to type '[T] extends [number] ? [T] extends [string] ? { y: number; } : { a: number; } : [T] extends [string] ? { y: number; } : { b: number; }'.
127+
!!! error TS2322: Type '{ y: number; }' is not assignable to type '[T] extends [number] ? [T] extends [string] ? { y: number; } : { a: number; } : [T] extends [string] ? { y: number; } : { b: number; }'.
128+
!!! error TS2322: Type '{ y: number; }' is not assignable to type '([T] extends [string] ? { y: number; } : { a: number; }) & ([T] extends [string] ? { y: number; } : { b: number; })'.
129+
!!! error TS2322: Type '{ y: number; }' is not assignable to type '[T] extends [string] ? { y: number; } : { a: number; }'.
130+
!!! error TS2322: Type '{ y: number; }' is not assignable to type '{ y: number; } & { a: number; }'.
131+
!!! error TS2322: Property 'a' is missing in type '{ y: number; }' but required in type '{ a: number; }'.
132+
!!! related TS2728 tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts:65:53: 'a' is declared here.
133+
? ([T] extends [string] ? { y: number } : { a: number })
134+
: ([T] extends [string] ? { y: number } : { b: number }) = x;
135+
// Infer type parameters: no good
136+
const o2: [T] extends [[infer U]] ? U : { b: number } = o;
137+
138+
// The next 4 are arguable - if you choose to ignore the `never` distribution case,
139+
// then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types
140+
// look approximately like the sum of their branches, but the `never` case bucks that.
141+
// There's an argument for the result of dumping `never` into a distributive conditional
142+
// being not `never`, but instead the intersection of the branches - a much more precise bound
143+
// on that "impossible" input.
144+
145+
// Distributive where T might instantiate to never: no good
146+
const o3: Distributive<T> = o;
147+
// Distributive where T & string might instantiate to never: also no good
148+
const o4: Distributive<T & string> = o;
149+
// Distributive where {a: T} cannot instantiate to never: OK
150+
const o5: Distributive<{ a: T }> = o;
151+
// Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good
152+
const o6: Distributive<[T] extends [never] ? { a: number } : never> = o;
153+
};
154+
155+
type Wrapped<T> = { ___secret: T };
156+
type Unwrap<T> = T extends Wrapped<infer U> ? U : T;
157+
158+
declare function set<T, K extends keyof T>(
159+
obj: T,
160+
key: K,
161+
value: Unwrap<T[K]>
162+
): Unwrap<T[K]>;
163+
164+
class Foo2 {
165+
prop!: Wrapped<string>;
166+
167+
method() {
168+
set(this, "prop", "hi"); // <-- type error
169+
}
170+
}
171+
172+
set(new Foo2(), "prop", "hi"); // <-- typechecks
173+
174+
type InferBecauseWhyNot<T> = [T] extends [(p: infer P1) => any]
175+
? P1 | T
176+
: never;
177+
178+
<Q extends (arg: any) => any>(x: Q): InferBecauseWhyNot<Q> => {
179+
return x;
180+
~~~~~~~~~
181+
!!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot<Q>'.
182+
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot<Q>'.
183+
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'never'.
184+
!!! error TS2322: Type 'Q' is not assignable to type 'never'.
185+
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'never'.
186+
};
187+
188+
type InferBecauseWhyNotDistributive<T> = T extends (p: infer P1) => any
189+
? P1 | T
190+
: never;
191+
192+
<Q extends (arg: any) => any>(
193+
x: Q
194+
): InferBecauseWhyNotDistributive<Q> => {
195+
return x; // should fail
196+
};
197+
198+
let t: true;
199+
let f: false;
200+
201+
let a: "a" extends "a" ? true : false = undefined!;
202+
let b: "a" extends "b" ? true : false = undefined!;
203+
204+
t = a;
205+
f = a; // !!! error TS2322: Type 'true' is not assignable to type 'false'.
206+
~
207+
!!! error TS2322: Type 'true' is not assignable to type 'false'.
208+
t = b; // !!! error TS2322: Type 'false' is not assignable to type 'true'.
209+
~
210+
!!! error TS2322: Type 'false' is not assignable to type 'true'.
211+
f = b;
212+
213+
a = true;
214+
a = false; // !!! error TS2322: Type 'false' is not assignable to type 'true'.
215+
~
216+
!!! error TS2322: Type 'false' is not assignable to type 'true'.
217+
b = true; // !!! error TS2322: Type 'true' is not assignable to type 'false'.
218+
~
219+
!!! error TS2322: Type 'true' is not assignable to type 'false'.
220+
b = false;
221+
222+
// #23132
223+
224+
<T extends "a">() => {
225+
let a: T extends "a" ? true : false = undefined!;
226+
let b: T extends "b" ? true : false = undefined!;
227+
228+
t = a;
229+
f = a; // !!! error TS2322: Type 'T extends "a" ? true : false' is not assignable to type 'false'.
230+
~
231+
!!! error TS2322: Type 'T extends "a" ? true : false' is not assignable to type 'false'.
232+
!!! error TS2322: Type 'boolean' is not assignable to type 'false'.
233+
!!! error TS2322: Type 'true' is not assignable to type 'false'.
234+
t = b; // !!! error TS2322: Type 'T extends "b" ? true : false' is not assignable to type 'true'.
235+
~
236+
!!! error TS2322: Type 'T extends "b" ? true : false' is not assignable to type 'true'.
237+
!!! error TS2322: Type 'boolean' is not assignable to type 'true'.
238+
!!! error TS2322: Type 'false' is not assignable to type 'true'.
239+
f = b;
240+
241+
a = true;
242+
a = false; // !!! error TS2322: Type 'false' is not assignable to type 'T extends "a" ? true : false'.
243+
~
244+
!!! error TS2322: Type 'false' is not assignable to type 'T extends "a" ? true : false'.
245+
!!! error TS2322: Type 'false' is not assignable to type 'never'.
246+
!!! error TS2322: Type 'false' is not assignable to type 'true'.
247+
b = true; // !!! error TS2322: Type 'true' is not assignable to type 'T extends "b" ? true : false'.
248+
~
249+
!!! error TS2322: Type 'true' is not assignable to type 'T extends "b" ? true : false'.
250+
!!! error TS2322: Type 'true' is not assignable to type 'never'.
251+
!!! error TS2322: Type 'true' is not assignable to type 'false'.
252+
b = false;
253+
};
254+

0 commit comments

Comments
 (0)