From 413e11435d165e51a089fffb5c76419e2535cc74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 25 Jul 2022 12:36:21 +0200 Subject: [PATCH 1/2] Make `any[]` the effective rest type when `any` is spread --- src/compiler/checker.ts | 2 +- tests/baselines/reference/inferTypes1.types | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index abf3e9d9fe0e7..fe26a3e3f78c6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32571,7 +32571,7 @@ namespace ts { if (signatureHasRestParameter(signature)) { const restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]); if (!isTupleType(restType)) { - return restType; + return isTypeAny(restType) ? anyArrayType : restType; } if (restType.target.hasRestElement) { return sliceTupleType(restType, restType.target.fixedLength); diff --git a/tests/baselines/reference/inferTypes1.types b/tests/baselines/reference/inferTypes1.types index 964ae6c5c6bcd..cbf4a66a85ba3 100644 --- a/tests/baselines/reference/inferTypes1.types +++ b/tests/baselines/reference/inferTypes1.types @@ -96,7 +96,7 @@ type T18 = ReturnType; // Error >T18 : any type T19 = ReturnType<(x: string, ...args: T) => T[]>; // T[] ->T19 : T[] +>T19 : T19 >x : string >args : T @@ -121,12 +121,12 @@ type U15 = InstanceType; // Abstract >Abstract : typeof Abstract type U16 = InstanceType T[]>; // T[] ->U16 : T[] +>U16 : U16 >x : string >args : T type U17 = InstanceType T[]>; // T[] ->U17 : T[] +>U17 : U17 >x : string >args : T @@ -490,6 +490,6 @@ const result = invoker('test', true)({ test: (a: boolean) => 123 }) >123 : 123 type Foo2 = ReturnType<(...args: A) => string>; ->Foo2 : string +>Foo2 : Foo2 >args : A From 1c1506a4ab2c44883f5de59fe39d3b4454af7422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 19 Jan 2024 00:48:52 +0100 Subject: [PATCH 2/2] add a test case --- ...ypesAppliedToParametersUsedAsRest1.symbols | 54 +++++++++++++++++++ ...dTypesAppliedToParametersUsedAsRest1.types | 30 +++++++++++ ...ppedTypesAppliedToParametersUsedAsRest1.ts | 16 ++++++ 3 files changed, 100 insertions(+) create mode 100644 tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.symbols create mode 100644 tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.types create mode 100644 tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts diff --git a/tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.symbols b/tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.symbols new file mode 100644 index 0000000000000..c21b8088b1f4e --- /dev/null +++ b/tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.symbols @@ -0,0 +1,54 @@ +//// [tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts] //// + +=== homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts === +// https://github.com/microsoft/TypeScript/issues/29919 + +type FuncParams = 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 = { +>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 = { +>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(func: T, ...params: Optional>) {} +>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(func: T, ...params: Stringify>) {} +>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)) + diff --git a/tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.types b/tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.types new file mode 100644 index 0000000000000..6ba932fdbada1 --- /dev/null +++ b/tests/baselines/reference/homomorphicMappedTypesAppliedToParametersUsedAsRest1.types @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts] //// + +=== homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts === +// https://github.com/microsoft/TypeScript/issues/29919 + +type FuncParams = T extends (...args: infer P) => any ? P : never; +>FuncParams : FuncParams +>args : P + +type Stringify = { +>Stringify : Stringify + + [K in keyof T]: string; +}; +type Optional = { +>Optional : Optional + + [K in keyof T]?: T[K]; +}; + +function doOptionalStuff(func: T, ...params: Optional>) {} +>doOptionalStuff : (func: T, ...params: Optional>) => void +>func : T +>params : Optional> + +function doStringStuff(func: T, ...params: Stringify>) {} +>doStringStuff : (func: T, ...params: Stringify>) => void +>func : T +>params : Stringify> + diff --git a/tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts b/tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts new file mode 100644 index 0000000000000..8ddcbaf58d6e7 --- /dev/null +++ b/tests/cases/compiler/homomorphicMappedTypesAppliedToParametersUsedAsRest1.ts @@ -0,0 +1,16 @@ +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/29919 + +type FuncParams = T extends (...args: infer P) => any ? P : never; +type Stringify = { + [K in keyof T]: string; +}; +type Optional = { + [K in keyof T]?: T[K]; +}; + +function doOptionalStuff(func: T, ...params: Optional>) {} + +function doStringStuff(func: T, ...params: Stringify>) {}