Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experiment] Check for extended empty tuples in isTupleLikeType #52551

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, emptyArray) as ObjectType as GenericType;
emptyGenericType.instantiations = new Map<string, TypeReference>();

const emptyReadonlyTupleType = createTupleType(emptyArray, emptyArray, /*readonly*/ true);

const anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, emptyArray);
// The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated
// in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes.
Expand Down Expand Up @@ -22902,7 +22904,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function isTupleLikeType(type: Type): boolean {
return isTupleType(type) || !!getPropertyOfType(type, "0" as __String);
return isTupleType(type) || !!getPropertyOfType(type, "0" as __String) || isTypeAssignableTo(type, emptyReadonlyTupleType);
}

function isArrayOrTupleLikeType(type: Type): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ paired.reduce(function (a1, a2) {
>{} : {}

} , []);
>[] : undefined[]
>[] : []

paired.reduce((b1, b2) => {
>paired.reduce((b1, b2) => { return b1.concat({});} , []) : any
Expand All @@ -38,7 +38,7 @@ paired.reduce((b1, b2) => {
>{} : {}

} , []);
>[] : undefined[]
>[] : []

paired.reduce((b3, b4) => b3.concat({}), []);
>paired.reduce((b3, b4) => b3.concat({}), []) : any
Expand All @@ -53,7 +53,7 @@ paired.reduce((b3, b4) => b3.concat({}), []);
>b3 : any
>concat : any
>{} : {}
>[] : undefined[]
>[] : []

paired.map((c1) => c1.count);
>paired.map((c1) => c1.count) : any[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/assignmentCompatability46.ts(3,4): error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'never'.
tests/cases/compiler/assignmentCompatability46.ts(3,4): error TS2345: Argument of type '[number, number, number]' is not assignable to parameter of type 'never'.
tests/cases/compiler/assignmentCompatability46.ts(4,4): error TS2345: Argument of type '{ a: number; b: number; }' is not assignable to parameter of type 'never'.


Expand All @@ -7,7 +7,7 @@ tests/cases/compiler/assignmentCompatability46.ts(4,4): error TS2345: Argument o

fn([1, 2, 3])
~~~~~~~~~
!!! error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'never'.
!!! error TS2345: Argument of type '[number, number, number]' is not assignable to parameter of type 'never'.
fn({ a: 1, b: 2 })
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ a: number; b: number; }' is not assignable to parameter of type 'never'.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentCompatability46.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare function fn(x: never): void;
fn([1, 2, 3])
>fn([1, 2, 3]) : void
>fn : (x: never) => void
>[1, 2, 3] : number[]
>[1, 2, 3] : [number, number, number]
>1 : 1
>2 : 2
>3 : 3
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/assignmentNestedInLiterals.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var target, x, y;
>y : any

target = [x = 1, y = x];
>target = [x = 1, y = x] : number[]
>target = [x = 1, y = x] : [number, number]
>target : any
>[x = 1, y = x] : number[]
>[x = 1, y = x] : [number, number]
>x = 1 : 1
>x : any
>1 : 1
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/bigintWithoutLib.types
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bigIntArray = new BigInt64Array([1n, 2n, 3n]);
>bigIntArray : BigInt64Array
>new BigInt64Array([1n, 2n, 3n]) : any
>BigInt64Array : any
>[1n, 2n, 3n] : bigint[]
>[1n, 2n, 3n] : [bigint, bigint, bigint]
>1n : 1n
>2n : 2n
>3n : 3n
Expand All @@ -135,7 +135,7 @@ bigIntArray = new BigInt64Array([1, 2, 3]);
>bigIntArray : BigInt64Array
>new BigInt64Array([1, 2, 3]) : any
>BigInt64Array : any
>[1, 2, 3] : number[]
>[1, 2, 3] : [number, number, number]
>1 : 1
>2 : 2
>3 : 3
Expand Down Expand Up @@ -205,7 +205,7 @@ bigUintArray = new BigUint64Array([1n, 2n, 3n]);
>bigUintArray : BigUint64Array
>new BigUint64Array([1n, 2n, 3n]) : any
>BigUint64Array : any
>[1n, 2n, 3n] : bigint[]
>[1n, 2n, 3n] : [bigint, bigint, bigint]
>1n : 1n
>2n : 2n
>3n : 3n
Expand All @@ -215,7 +215,7 @@ bigUintArray = new BigUint64Array([1, 2, 3]);
>bigUintArray : BigUint64Array
>new BigUint64Array([1, 2, 3]) : any
>BigUint64Array : any
>[1, 2, 3] : number[]
>[1, 2, 3] : [number, number, number]
>1 : 1
>2 : 2
>3 : 3
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/castExpressionParentheses.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare var a;
(<any>[1,3,]);
>(<any>[1,3,]) : any
><any>[1,3,] : any
>[1,3,] : number[]
>[1,3,] : [number, number]
>1 : 1
>3 : 3

Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/conditionalTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ function zeroOf<T extends number | string | boolean>(value: T) {

return <ZeroOf<T>>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false);
><ZeroOf<T>>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false) : ZeroOf<T>
>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false) : false | "" | 0
>typeof value === "number" ? 0 : typeof value === "string" ? "" : false : false | "" | 0
>(typeof value === "number" ? 0 : typeof value === "string" ? "" : false) : false | 0 | ""
>typeof value === "number" ? 0 : typeof value === "string" ? "" : false : false | 0 | ""
>typeof value === "number" : boolean
>typeof value : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>value : T
Expand Down Expand Up @@ -476,11 +476,11 @@ function f21<T extends number | string>(x: T, y: ZeroOf<T>) {

let z1: number | string = y;
>z1 : string | number
>y : "" | 0
>y : 0 | ""

let z2: 0 | "" = y;
>z2 : "" | 0
>y : "" | 0
>z2 : 0 | ""
>y : 0 | ""

x = y; // Error
>x = y : ZeroOf<T>
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/constructorOverloads2.types
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var f4 = new Foo([f1,f2,f3]);
>f4 : Foo
>new Foo([f1,f2,f3]) : Foo
>Foo : typeof Foo
>[f1,f2,f3] : Foo[]
>[f1,f2,f3] : [Foo, Foo, Foo]
>f1 : Foo
>f2 : Foo
>f3 : Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/constructorOverloads3.types
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var f4 = new Foo([f1,f2,f3]);
>f4 : Foo
>new Foo([f1,f2,f3]) : Foo
>Foo : typeof Foo
>[f1,f2,f3] : Foo[]
>[f1,f2,f3] : [Foo, Foo, Foo]
>f1 : Foo
>f2 : Foo
>f3 : Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/constructorOverloads6.types
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var f4 = new Foo([f1,f2,f3]);
>f4 : Foo
>new Foo([f1,f2,f3]) : Foo
>Foo : typeof Foo
>[f1,f2,f3] : Foo[]
>[f1,f2,f3] : [Foo, Foo, Foo]
>f1 : Foo
>f2 : Foo
>f3 : Foo
Expand Down
22 changes: 11 additions & 11 deletions tests/baselines/reference/controlFlowArrayErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function f2() {
>x : any

x = [];
>x = [] : undefined[]
>x = [] : []
>x : any
>[] : undefined[]
>[] : []

let y = x; // Implicit any[] error
>y : any[]
Expand Down Expand Up @@ -81,16 +81,16 @@ function f4() {
>x : any

x = [5, "hello"]; // Non-evolving array
>x = [5, "hello"] : (string | number)[]
>x = [5, "hello"] : [number, string]
>x : any
>[5, "hello"] : (string | number)[]
>[5, "hello"] : [number, string]
>5 : 5
>"hello" : "hello"

x.push(true); // Error
>x.push(true) : number
>x.push : (...items: (string | number)[]) => number
>x : (string | number)[]
>x : [number, string]
>push : (...items: (string | number)[]) => number
>true : true
}
Expand Down Expand Up @@ -123,9 +123,9 @@ function f6() {
>cond : () => boolean

x = [];
>x = [] : undefined[]
>x = [] : []
>x : any
>[] : undefined[]
>[] : []

x.push(5);
>x.push(5) : number
Expand All @@ -143,18 +143,18 @@ function f6() {
}
else {
x = [true]; // Non-evolving array
>x = [true] : boolean[]
>x = [true] : [boolean]
>x : any
>[true] : boolean[]
>[true] : [boolean]
>true : true
}
x; // boolean[] | (string | number)[]
>x : (string | number)[] | boolean[]
>x : (string | number)[] | [boolean]

x.push(99); // Error
>x.push(99) : number
>x.push : ((...items: (string | number)[]) => number) | ((...items: boolean[]) => number)
>x : (string | number)[] | boolean[]
>x : (string | number)[] | [boolean]
>push : ((...items: (string | number)[]) => number) | ((...items: boolean[]) => number)
>99 : 99
}
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/controlFlowArrays.types
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ function f3() {
>x : any

x = [];
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []

x.push(5, "hello");
>x.push(5, "hello") : number
Expand Down Expand Up @@ -130,9 +130,9 @@ function f5() {
>cond : () => boolean

x = [];
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []

x.push(5);
>x.push(5) : number
Expand All @@ -143,9 +143,9 @@ function f5() {
}
else {
x = [];
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []

x.push("hello");
>x.push("hello") : number
Expand Down Expand Up @@ -175,9 +175,9 @@ function f6() {
}
else {
x = [];
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []

x.push("hello");
>x.push("hello") : number
Expand All @@ -202,9 +202,9 @@ function f7() {
>cond : () => boolean

x = [];
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []

while (cond()) {
>cond() : boolean
Expand Down Expand Up @@ -387,9 +387,9 @@ function f12() {
>x : any

x = [];
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []

if (x.length === 0) { // x.length ok on implicit any[]
>x.length === 0 : boolean
Expand Down Expand Up @@ -513,9 +513,9 @@ function f16() {
>(x = [], x).push : (...items: any[]) => number
>(x = [], x) : any[]
>x = [], x : any[]
>x = [] : never[]
>x = [] : []
>x : any
>[] : never[]
>[] : []
>x : any[]
>push : (...items: any[]) => number
>5 : 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare var x11: number, y11: string;
declare var a11: undefined, b11: undefined, c11: undefined;
declare var a2: number, b2: string, x12: number, c2: boolean;
declare var x13: number, y13: string;
declare var a3: (string | number)[], b3: {
declare var a3: [number, string], b3: {
x: number;
y: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ var [x13, y13] = [1, "hello"];
>"hello" : "hello"

var [a3, b3] = [[x13, y13], { x: x13, y: y13 }];
>a3 : (string | number)[]
>a3 : [number, string]
>b3 : { x: number; y: string; }
>[[x13, y13], { x: x13, y: y13 }] : [(string | number)[], { x: number; y: string; }]
>[x13, y13] : (string | number)[]
>[[x13, y13], { x: x13, y: y13 }] : [[number, string], { x: number; y: string; }]
>[x13, y13] : [number, string]
>x13 : number
>y13 : string
>{ x: x13, y: y13 } : { x: number; y: string; }
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/declarationsAndAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ function f13() {
>"hello" : "hello"

var [a, b] = [[x, y], { x: x, y: y }];
>a : (string | number)[]
>a : [number, string]
>b : { x: number; y: string; }
>[[x, y], { x: x, y: y }] : [(string | number)[], { x: number; y: string; }]
>[x, y] : (string | number)[]
>[[x, y], { x: x, y: y }] : [[number, string], { x: number; y: string; }]
>[x, y] : [number, string]
>x : number
>y : string
>{ x: x, y: y } : { x: number; y: string; }
Expand Down
Loading