-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Make any[]
the effective rest type when any
is spread
#50034
Changes from all commits
413e114
0227999
10607e4
1d8fb6f
1c1506a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//// [tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts] //// | ||
|
||
=== homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/29919 | ||
|
||
type FuncParams<T> = T extends (...args: infer P) => any ? P : never; | ||
>FuncParams : Symbol(FuncParams, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 16)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 16)) | ||
>args : Symbol(args, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 32)) | ||
>P : Symbol(P, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 46)) | ||
>P : Symbol(P, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 46)) | ||
|
||
type Stringify<T> = { | ||
>Stringify : Symbol(Stringify, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 69)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 3, 15)) | ||
|
||
[K in keyof T]: string; | ||
>K : Symbol(K, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 4, 3)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 3, 15)) | ||
|
||
}; | ||
type Optional<T> = { | ||
>Optional : Symbol(Optional, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 5, 2)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 6, 14)) | ||
|
||
[K in keyof T]?: T[K]; | ||
>K : Symbol(K, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 7, 3)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 6, 14)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 6, 14)) | ||
>K : Symbol(K, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 7, 3)) | ||
|
||
}; | ||
|
||
function doOptionalStuff<T>(func: T, ...params: Optional<FuncParams<T>>) {} | ||
>doOptionalStuff : Symbol(doOptionalStuff, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 8, 2)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 10, 25)) | ||
>func : Symbol(func, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 10, 28)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 10, 25)) | ||
>params : Symbol(params, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 10, 36)) | ||
>Optional : Symbol(Optional, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 5, 2)) | ||
>FuncParams : Symbol(FuncParams, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 10, 25)) | ||
|
||
function doStringStuff<T>(func: T, ...params: Stringify<FuncParams<T>>) {} | ||
>doStringStuff : Symbol(doStringStuff, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 10, 75)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 12, 23)) | ||
>func : Symbol(func, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 12, 26)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 12, 23)) | ||
>params : Symbol(params, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 12, 34)) | ||
>Stringify : Symbol(Stringify, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 2, 69)) | ||
>FuncParams : Symbol(FuncParams, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts, 12, 23)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//// [tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts] //// | ||
|
||
=== homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/29919 | ||
|
||
type FuncParams<T> = T extends (...args: infer P) => any ? P : never; | ||
>FuncParams : FuncParams<T> | ||
>args : P | ||
|
||
type Stringify<T> = { | ||
>Stringify : Stringify<T> | ||
|
||
[K in keyof T]: string; | ||
}; | ||
type Optional<T> = { | ||
>Optional : Optional<T> | ||
|
||
[K in keyof T]?: T[K]; | ||
}; | ||
|
||
function doOptionalStuff<T>(func: T, ...params: Optional<FuncParams<T>>) {} | ||
>doOptionalStuff : <T>(func: T, ...params: Optional<FuncParams<T>>) => void | ||
>func : T | ||
>params : Optional<FuncParams<T>> | ||
|
||
function doStringStuff<T>(func: T, ...params: Stringify<FuncParams<T>>) {} | ||
>doStringStuff : <T>(func: T, ...params: Stringify<FuncParams<T>>) => void | ||
>func : T | ||
>params : Stringify<FuncParams<T>> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,7 +98,7 @@ type T18 = ReturnType<Function>; // Error | |
>T18 : any | ||
|
||
type T19<T extends any[]> = ReturnType<(x: string, ...args: T) => T[]>; // T[] | ||
>T19 : T[] | ||
>T19 : T19<T> | ||
>x : string | ||
>args : T | ||
|
||
|
@@ -123,12 +123,12 @@ type U15 = InstanceType<typeof Abstract>; // Abstract | |
>Abstract : typeof Abstract | ||
|
||
type U16<T extends any[]> = InstanceType<new (x: string, ...args: T) => T[]>; // T[] | ||
>U16 : T[] | ||
>U16 : U16<T> | ||
>x : string | ||
>args : T | ||
|
||
type U17<T extends any[]> = InstanceType<abstract new (x: string, ...args: T) => T[]>; // T[] | ||
>U17 : T[] | ||
>U17 : U17<T> | ||
>x : string | ||
>args : T | ||
|
||
|
@@ -490,6 +490,6 @@ const result = invoker('test', true)({ test: (a: boolean) => 123 }) | |
>123 : 123 | ||
|
||
type Foo2<A extends any[]> = ReturnType<(...args: A) => string>; | ||
>Foo2 : string | ||
>Foo2 : Foo2<A> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is caused by pretty similar stuff to the one described here. The only~ difference here is that we get:
and there is not a lot of logic related to handling type params in the |
||
>args : A | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/29919 | ||
|
||
type FuncParams<T> = T extends (...args: infer P) => any ? P : never; | ||
type Stringify<T> = { | ||
[K in keyof T]: string; | ||
}; | ||
type Optional<T> = { | ||
[K in keyof T]?: T[K]; | ||
}; | ||
|
||
function doOptionalStuff<T>(func: T, ...params: Optional<FuncParams<T>>) {} | ||
|
||
function doStringStuff<T>(func: T, ...params: Stringify<FuncParams<T>>) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those 3 changes here are basically caused by the same issue.
Because
target
/source
is nowany[]
and notany
when we start comparing the signatures within the conditional type we don't pass theisTypeAssignable
check here. Because of that the conditional type can't be "simplified" here to thetrueType
and thus the displayed type changed.This check reaches the
structuredTypeRelatedToWorker
with types like:I've expected this to be satisfied somewhere around this check but actually the
target
is not an array - it's a tuple and there is no logic supporting tuple targets here. The question is if this is deliberate or just an accidental omission?I've played around with this more though and I've noticed 2 additional problems here though:
EDIT:// I know now that it's not how it's supposed to work, tuples might come with required elements and stuff andany[]
is not assignable to[string, ...any[]]
. I understand why the error is reported for this case but in the presence ofany
in the type system it feels weird. Intuitively I've expectedany[]
to be assignable to all arrays and tuples, similarly to howanyFunctionType
is always assignable, regardless of the parameters count etc, see the check hereany[]
doesn't guarantee thatT
here is unknown and notany[]
. I don't understand why as I've expected the latter.