Skip to content

Commit 5bef1aa

Browse files
jack-williamsweswigham
authored andcommitted
Add regressions for conditional types that affect parameter variance (#30146)
1 parent e383b0d commit 5bef1aa

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

tests/baselines/reference/variance.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [variance.ts]
2+
// Test cases for parameter variances affected by conditional types.
3+
4+
// Repro from #30047
5+
6+
interface Foo<T> {
7+
prop: T extends unknown ? true : false;
8+
}
9+
10+
const foo = { prop: true } as const;
11+
const x: Foo<1> = foo;
12+
const y: Foo<number> = foo;
13+
const z: Foo<number> = x;
14+
15+
16+
// Repro from #30118
17+
18+
class Bar<T extends string> {
19+
private static instance: Bar<string>[];
20+
21+
cast(_name: ([T] extends [string] ? string : string)) { }
22+
23+
pushThis() {
24+
Bar.instance.push(this);
25+
}
26+
}
27+
28+
29+
//// [variance.js]
30+
"use strict";
31+
// Test cases for parameter variances affected by conditional types.
32+
var foo = { prop: true };
33+
var x = foo;
34+
var y = foo;
35+
var z = x;
36+
// Repro from #30118
37+
var Bar = /** @class */ (function () {
38+
function Bar() {
39+
}
40+
Bar.prototype.cast = function (_name) { };
41+
Bar.prototype.pushThis = function () {
42+
Bar.instance.push(this);
43+
};
44+
return Bar;
45+
}());
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
=== tests/cases/conformance/types/conditional/variance.ts ===
2+
// Test cases for parameter variances affected by conditional types.
3+
4+
// Repro from #30047
5+
6+
interface Foo<T> {
7+
>Foo : Symbol(Foo, Decl(variance.ts, 0, 0))
8+
>T : Symbol(T, Decl(variance.ts, 4, 14))
9+
10+
prop: T extends unknown ? true : false;
11+
>prop : Symbol(Foo.prop, Decl(variance.ts, 4, 18))
12+
>T : Symbol(T, Decl(variance.ts, 4, 14))
13+
}
14+
15+
const foo = { prop: true } as const;
16+
>foo : Symbol(foo, Decl(variance.ts, 8, 5))
17+
>prop : Symbol(prop, Decl(variance.ts, 8, 13))
18+
19+
const x: Foo<1> = foo;
20+
>x : Symbol(x, Decl(variance.ts, 9, 5))
21+
>Foo : Symbol(Foo, Decl(variance.ts, 0, 0))
22+
>foo : Symbol(foo, Decl(variance.ts, 8, 5))
23+
24+
const y: Foo<number> = foo;
25+
>y : Symbol(y, Decl(variance.ts, 10, 5))
26+
>Foo : Symbol(Foo, Decl(variance.ts, 0, 0))
27+
>foo : Symbol(foo, Decl(variance.ts, 8, 5))
28+
29+
const z: Foo<number> = x;
30+
>z : Symbol(z, Decl(variance.ts, 11, 5))
31+
>Foo : Symbol(Foo, Decl(variance.ts, 0, 0))
32+
>x : Symbol(x, Decl(variance.ts, 9, 5))
33+
34+
35+
// Repro from #30118
36+
37+
class Bar<T extends string> {
38+
>Bar : Symbol(Bar, Decl(variance.ts, 11, 25))
39+
>T : Symbol(T, Decl(variance.ts, 16, 10))
40+
41+
private static instance: Bar<string>[];
42+
>instance : Symbol(Bar.instance, Decl(variance.ts, 16, 29))
43+
>Bar : Symbol(Bar, Decl(variance.ts, 11, 25))
44+
45+
cast(_name: ([T] extends [string] ? string : string)) { }
46+
>cast : Symbol(Bar.cast, Decl(variance.ts, 17, 41))
47+
>_name : Symbol(_name, Decl(variance.ts, 19, 7))
48+
>T : Symbol(T, Decl(variance.ts, 16, 10))
49+
50+
pushThis() {
51+
>pushThis : Symbol(Bar.pushThis, Decl(variance.ts, 19, 59))
52+
53+
Bar.instance.push(this);
54+
>Bar.instance.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
55+
>Bar.instance : Symbol(Bar.instance, Decl(variance.ts, 16, 29))
56+
>Bar : Symbol(Bar, Decl(variance.ts, 11, 25))
57+
>instance : Symbol(Bar.instance, Decl(variance.ts, 16, 29))
58+
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
59+
>this : Symbol(Bar, Decl(variance.ts, 11, 25))
60+
}
61+
}
62+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== tests/cases/conformance/types/conditional/variance.ts ===
2+
// Test cases for parameter variances affected by conditional types.
3+
4+
// Repro from #30047
5+
6+
interface Foo<T> {
7+
prop: T extends unknown ? true : false;
8+
>prop : T extends unknown ? true : false
9+
>true : true
10+
>false : false
11+
}
12+
13+
const foo = { prop: true } as const;
14+
>foo : { readonly prop: true; }
15+
>{ prop: true } as const : { readonly prop: true; }
16+
>{ prop: true } : { readonly prop: true; }
17+
>prop : true
18+
>true : true
19+
20+
const x: Foo<1> = foo;
21+
>x : Foo<1>
22+
>foo : { readonly prop: true; }
23+
24+
const y: Foo<number> = foo;
25+
>y : Foo<number>
26+
>foo : { readonly prop: true; }
27+
28+
const z: Foo<number> = x;
29+
>z : Foo<number>
30+
>x : Foo<1>
31+
32+
33+
// Repro from #30118
34+
35+
class Bar<T extends string> {
36+
>Bar : Bar<T>
37+
38+
private static instance: Bar<string>[];
39+
>instance : Bar<string>[]
40+
41+
cast(_name: ([T] extends [string] ? string : string)) { }
42+
>cast : (_name: [T] extends [string] ? string : string) => void
43+
>_name : [T] extends [string] ? string : string
44+
45+
pushThis() {
46+
>pushThis : () => void
47+
48+
Bar.instance.push(this);
49+
>Bar.instance.push(this) : number
50+
>Bar.instance.push : (...items: Bar<string>[]) => number
51+
>Bar.instance : Bar<string>[]
52+
>Bar : typeof Bar
53+
>instance : Bar<string>[]
54+
>push : (...items: Bar<string>[]) => number
55+
>this : this
56+
}
57+
}
58+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @strict: true
2+
3+
// Test cases for parameter variances affected by conditional types.
4+
5+
// Repro from #30047
6+
7+
interface Foo<T> {
8+
prop: T extends unknown ? true : false;
9+
}
10+
11+
const foo = { prop: true } as const;
12+
const x: Foo<1> = foo;
13+
const y: Foo<number> = foo;
14+
const z: Foo<number> = x;
15+
16+
17+
// Repro from #30118
18+
19+
class Bar<T extends string> {
20+
private static instance: Bar<string>[];
21+
22+
cast(_name: ([T] extends [string] ? string : string)) { }
23+
24+
pushThis() {
25+
Bar.instance.push(this);
26+
}
27+
}

0 commit comments

Comments
 (0)