Skip to content

Commit 032cd32

Browse files
committed
Add regression test
1 parent efc15ae commit 032cd32

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// [typeInferenceLiteralUnion.ts]
2+
// Repro from #10901
3+
/**
4+
* Administrivia: JavaScript primitive types and Date
5+
*/
6+
export type Primitive = number | string | boolean | Date;
7+
8+
/**
9+
* Administrivia: anything with a valueOf(): number method is comparable, so we allow it in numeric operations
10+
*/
11+
interface Numeric {
12+
valueOf(): number;
13+
}
14+
15+
// Not very useful, but meets Numeric
16+
class NumCoercible {
17+
public a: number;
18+
19+
constructor(a: number) {
20+
this.a = a;
21+
}
22+
public valueOf() {
23+
return this.a;
24+
}
25+
}
26+
27+
/**
28+
* Return the min and max simultaneously.
29+
*/
30+
export function extent<T extends Numeric>(array: Array<T | Primitive>): [T | Primitive, T | Primitive] | [undefined, undefined] {
31+
return [undefined, undefined];
32+
}
33+
34+
35+
let extentMixed: [Primitive | NumCoercible, Primitive | NumCoercible] | [undefined, undefined];
36+
extentMixed = extent([new NumCoercible(10), 13, '12', true]);
37+
38+
39+
//// [typeInferenceLiteralUnion.js]
40+
"use strict";
41+
// Not very useful, but meets Numeric
42+
var NumCoercible = (function () {
43+
function NumCoercible(a) {
44+
this.a = a;
45+
}
46+
NumCoercible.prototype.valueOf = function () {
47+
return this.a;
48+
};
49+
return NumCoercible;
50+
}());
51+
/**
52+
* Return the min and max simultaneously.
53+
*/
54+
function extent(array) {
55+
return [undefined, undefined];
56+
}
57+
exports.extent = extent;
58+
var extentMixed;
59+
extentMixed = extent([new NumCoercible(10), 13, '12', true]);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
=== tests/cases/compiler/typeInferenceLiteralUnion.ts ===
2+
// Repro from #10901
3+
/**
4+
* Administrivia: JavaScript primitive types and Date
5+
*/
6+
export type Primitive = number | string | boolean | Date;
7+
>Primitive : Symbol(Primitive, Decl(typeInferenceLiteralUnion.ts, 0, 0))
8+
>Date : Symbol(Date, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
9+
10+
/**
11+
* Administrivia: anything with a valueOf(): number method is comparable, so we allow it in numeric operations
12+
*/
13+
interface Numeric {
14+
>Numeric : Symbol(Numeric, Decl(typeInferenceLiteralUnion.ts, 4, 57))
15+
16+
valueOf(): number;
17+
>valueOf : Symbol(Numeric.valueOf, Decl(typeInferenceLiteralUnion.ts, 9, 19))
18+
}
19+
20+
// Not very useful, but meets Numeric
21+
class NumCoercible {
22+
>NumCoercible : Symbol(NumCoercible, Decl(typeInferenceLiteralUnion.ts, 11, 1))
23+
24+
public a: number;
25+
>a : Symbol(NumCoercible.a, Decl(typeInferenceLiteralUnion.ts, 14, 20))
26+
27+
constructor(a: number) {
28+
>a : Symbol(a, Decl(typeInferenceLiteralUnion.ts, 17, 16))
29+
30+
this.a = a;
31+
>this.a : Symbol(NumCoercible.a, Decl(typeInferenceLiteralUnion.ts, 14, 20))
32+
>this : Symbol(NumCoercible, Decl(typeInferenceLiteralUnion.ts, 11, 1))
33+
>a : Symbol(NumCoercible.a, Decl(typeInferenceLiteralUnion.ts, 14, 20))
34+
>a : Symbol(a, Decl(typeInferenceLiteralUnion.ts, 17, 16))
35+
}
36+
public valueOf() {
37+
>valueOf : Symbol(NumCoercible.valueOf, Decl(typeInferenceLiteralUnion.ts, 19, 5))
38+
39+
return this.a;
40+
>this.a : Symbol(NumCoercible.a, Decl(typeInferenceLiteralUnion.ts, 14, 20))
41+
>this : Symbol(NumCoercible, Decl(typeInferenceLiteralUnion.ts, 11, 1))
42+
>a : Symbol(NumCoercible.a, Decl(typeInferenceLiteralUnion.ts, 14, 20))
43+
}
44+
}
45+
46+
/**
47+
* Return the min and max simultaneously.
48+
*/
49+
export function extent<T extends Numeric>(array: Array<T | Primitive>): [T | Primitive, T | Primitive] | [undefined, undefined] {
50+
>extent : Symbol(extent, Decl(typeInferenceLiteralUnion.ts, 23, 1))
51+
>T : Symbol(T, Decl(typeInferenceLiteralUnion.ts, 28, 23))
52+
>Numeric : Symbol(Numeric, Decl(typeInferenceLiteralUnion.ts, 4, 57))
53+
>array : Symbol(array, Decl(typeInferenceLiteralUnion.ts, 28, 42))
54+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
55+
>T : Symbol(T, Decl(typeInferenceLiteralUnion.ts, 28, 23))
56+
>Primitive : Symbol(Primitive, Decl(typeInferenceLiteralUnion.ts, 0, 0))
57+
>T : Symbol(T, Decl(typeInferenceLiteralUnion.ts, 28, 23))
58+
>Primitive : Symbol(Primitive, Decl(typeInferenceLiteralUnion.ts, 0, 0))
59+
>T : Symbol(T, Decl(typeInferenceLiteralUnion.ts, 28, 23))
60+
>Primitive : Symbol(Primitive, Decl(typeInferenceLiteralUnion.ts, 0, 0))
61+
62+
return [undefined, undefined];
63+
>undefined : Symbol(undefined)
64+
>undefined : Symbol(undefined)
65+
}
66+
67+
68+
let extentMixed: [Primitive | NumCoercible, Primitive | NumCoercible] | [undefined, undefined];
69+
>extentMixed : Symbol(extentMixed, Decl(typeInferenceLiteralUnion.ts, 33, 3))
70+
>Primitive : Symbol(Primitive, Decl(typeInferenceLiteralUnion.ts, 0, 0))
71+
>NumCoercible : Symbol(NumCoercible, Decl(typeInferenceLiteralUnion.ts, 11, 1))
72+
>Primitive : Symbol(Primitive, Decl(typeInferenceLiteralUnion.ts, 0, 0))
73+
>NumCoercible : Symbol(NumCoercible, Decl(typeInferenceLiteralUnion.ts, 11, 1))
74+
75+
extentMixed = extent([new NumCoercible(10), 13, '12', true]);
76+
>extentMixed : Symbol(extentMixed, Decl(typeInferenceLiteralUnion.ts, 33, 3))
77+
>extent : Symbol(extent, Decl(typeInferenceLiteralUnion.ts, 23, 1))
78+
>NumCoercible : Symbol(NumCoercible, Decl(typeInferenceLiteralUnion.ts, 11, 1))
79+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=== tests/cases/compiler/typeInferenceLiteralUnion.ts ===
2+
// Repro from #10901
3+
/**
4+
* Administrivia: JavaScript primitive types and Date
5+
*/
6+
export type Primitive = number | string | boolean | Date;
7+
>Primitive : Primitive
8+
>Date : Date
9+
10+
/**
11+
* Administrivia: anything with a valueOf(): number method is comparable, so we allow it in numeric operations
12+
*/
13+
interface Numeric {
14+
>Numeric : Numeric
15+
16+
valueOf(): number;
17+
>valueOf : () => number
18+
}
19+
20+
// Not very useful, but meets Numeric
21+
class NumCoercible {
22+
>NumCoercible : NumCoercible
23+
24+
public a: number;
25+
>a : number
26+
27+
constructor(a: number) {
28+
>a : number
29+
30+
this.a = a;
31+
>this.a = a : number
32+
>this.a : number
33+
>this : this
34+
>a : number
35+
>a : number
36+
}
37+
public valueOf() {
38+
>valueOf : () => number
39+
40+
return this.a;
41+
>this.a : number
42+
>this : this
43+
>a : number
44+
}
45+
}
46+
47+
/**
48+
* Return the min and max simultaneously.
49+
*/
50+
export function extent<T extends Numeric>(array: Array<T | Primitive>): [T | Primitive, T | Primitive] | [undefined, undefined] {
51+
>extent : <T extends Numeric>(array: (string | number | boolean | Date | T)[]) => [string | number | boolean | Date | T, string | number | boolean | Date | T] | [undefined, undefined]
52+
>T : T
53+
>Numeric : Numeric
54+
>array : (string | number | boolean | Date | T)[]
55+
>Array : T[]
56+
>T : T
57+
>Primitive : Primitive
58+
>T : T
59+
>Primitive : Primitive
60+
>T : T
61+
>Primitive : Primitive
62+
63+
return [undefined, undefined];
64+
>[undefined, undefined] : [undefined, undefined]
65+
>undefined : undefined
66+
>undefined : undefined
67+
}
68+
69+
70+
let extentMixed: [Primitive | NumCoercible, Primitive | NumCoercible] | [undefined, undefined];
71+
>extentMixed : [undefined, undefined] | [string | number | boolean | Date | NumCoercible, string | number | boolean | Date | NumCoercible]
72+
>Primitive : Primitive
73+
>NumCoercible : NumCoercible
74+
>Primitive : Primitive
75+
>NumCoercible : NumCoercible
76+
77+
extentMixed = extent([new NumCoercible(10), 13, '12', true]);
78+
>extentMixed = extent([new NumCoercible(10), 13, '12', true]) : [undefined, undefined] | [string | number | boolean | Date | NumCoercible, string | number | boolean | Date | NumCoercible]
79+
>extentMixed : [undefined, undefined] | [string | number | boolean | Date | NumCoercible, string | number | boolean | Date | NumCoercible]
80+
>extent([new NumCoercible(10), 13, '12', true]) : [undefined, undefined] | [string | number | boolean | Date | NumCoercible, string | number | boolean | Date | NumCoercible]
81+
>extent : <T extends Numeric>(array: (string | number | boolean | Date | T)[]) => [string | number | boolean | Date | T, string | number | boolean | Date | T] | [undefined, undefined]
82+
>[new NumCoercible(10), 13, '12', true] : (true | NumCoercible | 13 | "12")[]
83+
>new NumCoercible(10) : NumCoercible
84+
>NumCoercible : typeof NumCoercible
85+
>10 : 10
86+
>13 : 13
87+
>'12' : "12"
88+
>true : true
89+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Repro from #10901
2+
/**
3+
* Administrivia: JavaScript primitive types and Date
4+
*/
5+
export type Primitive = number | string | boolean | Date;
6+
7+
/**
8+
* Administrivia: anything with a valueOf(): number method is comparable, so we allow it in numeric operations
9+
*/
10+
interface Numeric {
11+
valueOf(): number;
12+
}
13+
14+
// Not very useful, but meets Numeric
15+
class NumCoercible {
16+
public a: number;
17+
18+
constructor(a: number) {
19+
this.a = a;
20+
}
21+
public valueOf() {
22+
return this.a;
23+
}
24+
}
25+
26+
/**
27+
* Return the min and max simultaneously.
28+
*/
29+
export function extent<T extends Numeric>(array: Array<T | Primitive>): [T | Primitive, T | Primitive] | [undefined, undefined] {
30+
return [undefined, undefined];
31+
}
32+
33+
34+
let extentMixed: [Primitive | NumCoercible, Primitive | NumCoercible] | [undefined, undefined];
35+
extentMixed = extent([new NumCoercible(10), 13, '12', true]);

0 commit comments

Comments
 (0)