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

Avoid creating rest elements with errorType when any is spread #57116

Merged
merged 4 commits into from Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Expand Up @@ -16868,7 +16868,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const type = elementTypes[i];
const flags = target.elementFlags[i];
if (flags & ElementFlags.Variadic) {
if (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type)) {
if (type.flags & TypeFlags.Any) {
addElement(type, ElementFlags.Rest, target.labeledElementDeclarations?.[i]);
}
else if (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type)) {
// Generic variadic elements stay as they are.
addElement(type, ElementFlags.Variadic, target.labeledElementDeclarations?.[i]);
}
Expand Down
Expand Up @@ -24,10 +24,10 @@ declare const itNum: Iterable<number>

;(function(a, ...rest) {})('', true, ...itNum)
>(function(a, ...rest) {})('', true, ...itNum) : void
>(function(a, ...rest) {}) : (a: string, rest_0: boolean, ...rest_1: any[]) => void
>function(a, ...rest) {} : (a: string, rest_0: boolean, ...rest_1: any[]) => void
>(function(a, ...rest) {}) : (a: string, rest_0: boolean, ...rest_1: Iterable<number>[]) => void
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test targets es5 where Iterable doesn't exist. This change shows how now an unresolved type continues to be displayed with its alias name~. Just like here:

type Test = {
  foo: Thing; // even though it's unresolved and errors, it is still displayed as `Thing`
};

And actually, this matches the display of those IIFEs above this one. So it turns out that this one was going through createNormalizedTupleType that accidentally lost this display value.

>function(a, ...rest) {} : (a: string, rest_0: boolean, ...rest_1: Iterable<number>[]) => void
>a : string
>rest : [boolean, ...any[]]
>rest : [boolean, ...Iterable<number>[]]
>'' : ""
>true : true
>...itNum : Iterable<number>
Expand Down Expand Up @@ -60,8 +60,8 @@ const res3 = fn1(true, ..."hello");
>"hello" : "hello"

const res4 = fn1(true, ...itNum);
>res4 : readonly [true, ...any[]]
>fn1(true, ...itNum) : readonly [true, ...any[]]
>res4 : readonly [true, ...Iterable<number>[]]
>fn1(true, ...itNum) : readonly [true, ...Iterable<number>[]]
>fn1 : <const T extends readonly unknown[]>(...args: T) => T
>true : true
>...itNum : Iterable<number>
Expand Down Expand Up @@ -95,8 +95,8 @@ const p3 = foo(true, ..."hello");
>"hello" : "hello"

const p4 = foo(true, ...itNum);
>p4 : [boolean, ...any[]]
>foo(true, ...itNum) : [boolean, ...any[]]
>p4 : [boolean, ...Iterable<number>[]]
>foo(true, ...itNum) : [boolean, ...Iterable<number>[]]
>foo : <T extends unknown[]>(...args: T) => T
>true : true
>...itNum : Iterable<number>
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/variadicTuples1.errors.txt
Expand Up @@ -540,4 +540,6 @@ variadicTuples1.ts(411,7): error TS2322: Type '[boolean, false]' is not assignab

type ToStringLength1<T extends any[]> = `${T['length']}`;
type ToStringLength2<T extends any[]> = `${[...T]['length']}`;

type AnyArr = [...any];

3 changes: 3 additions & 0 deletions tests/baselines/reference/variadicTuples1.js
Expand Up @@ -421,6 +421,8 @@ type U3 = [...[string, number], boolean];

type ToStringLength1<T extends any[]> = `${T['length']}`;
type ToStringLength2<T extends any[]> = `${[...T]['length']}`;

type AnyArr = [...any];


//// [variadicTuples1.js]
Expand Down Expand Up @@ -830,3 +832,4 @@ type U2 = [...[string, ...Numbers], boolean];
type U3 = [...[string, number], boolean];
type ToStringLength1<T extends any[]> = `${T['length']}`;
type ToStringLength2<T extends any[]> = `${[...T]['length']}`;
type AnyArr = [...any];
3 changes: 3 additions & 0 deletions tests/baselines/reference/variadicTuples1.symbols
Expand Up @@ -1416,3 +1416,6 @@ type ToStringLength2<T extends any[]> = `${[...T]['length']}`;
>T : Symbol(T, Decl(variadicTuples1.ts, 419, 21))
>T : Symbol(T, Decl(variadicTuples1.ts, 419, 21))

type AnyArr = [...any];
>AnyArr : Symbol(AnyArr, Decl(variadicTuples1.ts, 419, 62))

7 changes: 5 additions & 2 deletions tests/baselines/reference/variadicTuples1.types
Expand Up @@ -883,7 +883,7 @@ type T17 = DropFirst<[]>;
>T17 : unknown[]

type T18 = DropFirst<any>;
>T18 : unknown[] | any[]
>T18 : any[] | unknown[]

type T19 = DropFirst<never>;
>T19 : never
Expand Down Expand Up @@ -943,7 +943,7 @@ type T37 = DropLast<[]>; // unknown[], maybe should be []
>T37 : []

type T38 = DropLast<any>;
>T38 : unknown[] | any[]
>T38 : any[] | unknown[]

type T39 = DropLast<never>;
>T39 : never
Expand Down Expand Up @@ -1455,3 +1455,6 @@ type ToStringLength1<T extends any[]> = `${T['length']}`;
type ToStringLength2<T extends any[]> = `${[...T]['length']}`;
>ToStringLength2 : `${[...T]["length"]}`

type AnyArr = [...any];
>AnyArr : any[]

2 changes: 2 additions & 0 deletions tests/cases/conformance/types/tuple/variadicTuples1.ts
Expand Up @@ -421,3 +421,5 @@ type U3 = [...[string, number], boolean];

type ToStringLength1<T extends any[]> = `${T['length']}`;
type ToStringLength2<T extends any[]> = `${[...T]['length']}`;

type AnyArr = [...any];