From 25b59148ce4b6fe26fa49957dfda3dcf795d10c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Sun, 18 Aug 2024 08:53:31 +0200 Subject: [PATCH 01/11] contextual type --- src/compiler/checker.ts | 35 ++++++++++--------- .../contextualSignatureWithExtraParameters.ts | 34 ++++++++++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 tests/cases/compiler/contextualSignatureWithExtraParameters.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 770e1a7c1c60a..64d178d09589e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -24675,8 +24675,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } } } - const targetLen = getParameterCount(target); - for (let i = 0; i < targetLen; i++) { + const sourceLen = getParameterCount(source); + for (let i = 0; i < sourceLen; i++) { const s = getTypeAtPosition(source, i); const t = getTypeAtPosition(target, i); const related = compareTypes(t, s); @@ -32257,22 +32257,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return getContextualCallSignature(type, node); } let signatureList: Signature[] | undefined; - const types = (type as UnionType).types; + const types = (type as UnionType).types + .map(type => ({type, signature: getContextualCallSignature(type, node)})) + .filter(type => type.signature) + .sort((t1, t2) => t1.signature!.parameters.length - t2.signature!.parameters.length) for (const current of types) { - const signature = getContextualCallSignature(current, node); - if (signature) { - if (!signatureList) { - // This signature will contribute to contextual union signature - signatureList = [signature]; - } - else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { - // Signatures aren't identical, do not use - return undefined; - } - else { - // Use this signature for contextual union signature - signatureList.push(signature); - } + const signature = current.signature!; + if (!signatureList) { + // This signature will contribute to contextual union signature + signatureList = [signature]; + } + else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { + // Signatures aren't identical, do not use + return undefined; + } + else { + // Use this signature for contextual union signature + signatureList.push(signature); } } // Result is union of signatures collected (return type is union of return types of this signature set) diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts new file mode 100644 index 0000000000000..20e5cc63ed97a --- /dev/null +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -0,0 +1,34 @@ +// https://github.com/microsoft/TypeScript/issues/59309 +function f1( + cb: ((item: number) => void) | ((item: number, extra: string) => void), + ) {} + + f1((item) => {}); + +function f2( + arr: T[], + cb: ((item: T) => void) | ((item: T, extra: unknown) => void), + ) {} + +f2([1, 2, 3], (item) => {}); + +export interface AsyncResultCallback { + (err?: E | null, result?: T): void; +} + +export interface AsyncResultIterator { + (item: T, callback: AsyncResultCallback): void; +} +export interface AsyncResultIteratorPromise { + (item: T): Promise; +} + +declare function mapLimit( + arr: T[], + limit: number, + iterator: AsyncResultIteratorPromise | AsyncResultIterator, +): Promise; + +mapLimit([1,2,3], 3, async (n) => { + return n ** 2; +}); \ No newline at end of file From ed2bfa49b30486ba71807c85c4584ec7346c9c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Sun, 18 Aug 2024 09:55:22 +0200 Subject: [PATCH 02/11] fix getContextualSignature --- src/compiler/checker.ts | 6 +- .../contextualSignatureWithExtraParameters.js | 87 +++++++++++ ...extualSignatureWithExtraParameters.symbols | 117 +++++++++++++++ ...ntextualSignatureWithExtraParameters.types | 136 ++++++++++++++++++ 4 files changed, 343 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/contextualSignatureWithExtraParameters.js create mode 100644 tests/baselines/reference/contextualSignatureWithExtraParameters.symbols create mode 100644 tests/baselines/reference/contextualSignatureWithExtraParameters.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0a4c023e24643..2a3a625e839e2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -24829,8 +24829,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } } } - const sourceLen = getParameterCount(source); - for (let i = 0; i < sourceLen; i++) { + const targetLen = getParameterCount(target); + for (let i = 0; i < targetLen; i++) { const s = getTypeAtPosition(source, i); const t = getTypeAtPosition(target, i); const related = compareTypes(t, s); @@ -32433,7 +32433,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // This signature will contribute to contextual union signature signatureList = [signature]; } - else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { + else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesSubtypeOf)) { // Signatures aren't identical, do not use return undefined; } diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.js b/tests/baselines/reference/contextualSignatureWithExtraParameters.js new file mode 100644 index 0000000000000..967af3cde6981 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.js @@ -0,0 +1,87 @@ +//// [tests/cases/compiler/contextualSignatureWithExtraParameters.ts] //// + +//// [contextualSignatureWithExtraParameters.ts] +// https://github.com/microsoft/TypeScript/issues/59309 +function f1( + cb: ((item: number) => void) | ((item: number, extra: string) => void), + ) {} + + f1((item) => {}); + +function f2( + arr: T[], + cb: ((item: T) => void) | ((item: T, extra: unknown) => void), + ) {} + +f2([1, 2, 3], (item) => {}); + +export interface AsyncResultCallback { + (err?: E | null, result?: T): void; +} + +export interface AsyncResultIterator { + (item: T, callback: AsyncResultCallback): void; +} +export interface AsyncResultIteratorPromise { + (item: T): Promise; +} + +declare function mapLimit( + arr: T[], + limit: number, + iterator: AsyncResultIteratorPromise | AsyncResultIterator, +): Promise; + +mapLimit([1,2,3], 3, async (n) => { + return n ** 2; +}); + +//// [contextualSignatureWithExtraParameters.js] +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); + return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +// https://github.com/microsoft/TypeScript/issues/59309 +function f1(cb) { } +f1(function (item) { }); +function f2(arr, cb) { } +f2([1, 2, 3], function (item) { }); +mapLimit([1, 2, 3], 3, function (n) { return __awaiter(void 0, void 0, void 0, function () { + return __generator(this, function (_a) { + return [2 /*return*/, Math.pow(n, 2)]; + }); +}); }); diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols new file mode 100644 index 0000000000000..75ddd62804cd0 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols @@ -0,0 +1,117 @@ +//// [tests/cases/compiler/contextualSignatureWithExtraParameters.ts] //// + +=== contextualSignatureWithExtraParameters.ts === +// https://github.com/microsoft/TypeScript/issues/59309 +function f1( +>f1 : Symbol(f1, Decl(contextualSignatureWithExtraParameters.ts, 0, 0)) + + cb: ((item: number) => void) | ((item: number, extra: string) => void), +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 1, 12)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 2, 10)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 2, 37)) +>extra : Symbol(extra, Decl(contextualSignatureWithExtraParameters.ts, 2, 50)) + + ) {} + + f1((item) => {}); +>f1 : Symbol(f1, Decl(contextualSignatureWithExtraParameters.ts, 0, 0)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 5, 6)) + +function f2( +>f2 : Symbol(f2, Decl(contextualSignatureWithExtraParameters.ts, 5, 19)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 7, 12)) + + arr: T[], +>arr : Symbol(arr, Decl(contextualSignatureWithExtraParameters.ts, 7, 15)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 7, 12)) + + cb: ((item: T) => void) | ((item: T, extra: unknown) => void), +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 8, 13)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 9, 10)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 7, 12)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 9, 32)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 7, 12)) +>extra : Symbol(extra, Decl(contextualSignatureWithExtraParameters.ts, 9, 40)) + + ) {} + +f2([1, 2, 3], (item) => {}); +>f2 : Symbol(f2, Decl(contextualSignatureWithExtraParameters.ts, 5, 19)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 12, 15)) + +export interface AsyncResultCallback { +>AsyncResultCallback : Symbol(AsyncResultCallback, Decl(contextualSignatureWithExtraParameters.ts, 12, 28)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 14, 37)) +>E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 14, 39)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + (err?: E | null, result?: T): void; +>err : Symbol(err, Decl(contextualSignatureWithExtraParameters.ts, 15, 5)) +>E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 14, 39)) +>result : Symbol(result, Decl(contextualSignatureWithExtraParameters.ts, 15, 20)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 14, 37)) +} + +export interface AsyncResultIterator { +>AsyncResultIterator : Symbol(AsyncResultIterator, Decl(contextualSignatureWithExtraParameters.ts, 16, 1)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 18, 37)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 18, 39)) +>E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 18, 42)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + (item: T, callback: AsyncResultCallback): void; +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 19, 5)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 18, 37)) +>callback : Symbol(callback, Decl(contextualSignatureWithExtraParameters.ts, 19, 13)) +>AsyncResultCallback : Symbol(AsyncResultCallback, Decl(contextualSignatureWithExtraParameters.ts, 12, 28)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 18, 39)) +>E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 18, 42)) +} +export interface AsyncResultIteratorPromise { +>AsyncResultIteratorPromise : Symbol(AsyncResultIteratorPromise, Decl(contextualSignatureWithExtraParameters.ts, 20, 1)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 21, 44)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 21, 46)) + + (item: T): Promise; +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 22, 5)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 21, 44)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 21, 46)) +} + +declare function mapLimit( +>mapLimit : Symbol(mapLimit, Decl(contextualSignatureWithExtraParameters.ts, 23, 1)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 25, 26)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 25, 28)) +>E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 25, 31)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + arr: T[], +>arr : Symbol(arr, Decl(contextualSignatureWithExtraParameters.ts, 25, 43)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 25, 26)) + + limit: number, +>limit : Symbol(limit, Decl(contextualSignatureWithExtraParameters.ts, 26, 13)) + + iterator: AsyncResultIteratorPromise | AsyncResultIterator, +>iterator : Symbol(iterator, Decl(contextualSignatureWithExtraParameters.ts, 27, 18)) +>AsyncResultIteratorPromise : Symbol(AsyncResultIteratorPromise, Decl(contextualSignatureWithExtraParameters.ts, 20, 1)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 25, 26)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 25, 28)) +>AsyncResultIterator : Symbol(AsyncResultIterator, Decl(contextualSignatureWithExtraParameters.ts, 16, 1)) +>T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 25, 26)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 25, 28)) +>E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 25, 31)) + +): Promise; +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) +>R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 25, 28)) + +mapLimit([1,2,3], 3, async (n) => { +>mapLimit : Symbol(mapLimit, Decl(contextualSignatureWithExtraParameters.ts, 23, 1)) +>n : Symbol(n, Decl(contextualSignatureWithExtraParameters.ts, 31, 28)) + + return n ** 2; +>n : Symbol(n, Decl(contextualSignatureWithExtraParameters.ts, 31, 28)) + +}); diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types new file mode 100644 index 0000000000000..accdb16e23a7f --- /dev/null +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -0,0 +1,136 @@ +//// [tests/cases/compiler/contextualSignatureWithExtraParameters.ts] //// + +=== contextualSignatureWithExtraParameters.ts === +// https://github.com/microsoft/TypeScript/issues/59309 +function f1( +>f1 : (cb: ((item: number) => void) | ((item: number, extra: string) => void)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((item: number) => void) | ((item: number, extra: string) => void), +>cb : ((item: number) => void) | ((item: number, extra: string) => void) +> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^ ^^ ^^^^^ ^ +>item : number +> : ^^^^^^ +>item : number +> : ^^^^^^ +>extra : string +> : ^^^^^^ + + ) {} + + f1((item) => {}); +>f1((item) => {}) : void +> : ^^^^ +>f1 : (cb: ((item: number) => void) | ((item: number, extra: string) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(item) => {} : (item: number) => void +> : ^ ^^^^^^^^^^^^^^^^^ +>item : number +> : ^^^^^^ + +function f2( +>f2 : (arr: T[], cb: ((item: T) => void) | ((item: T, extra: unknown) => void)) => void +> : ^ ^^ ^^ ^^ ^^ ^^^^^^^^^ + + arr: T[], +>arr : T[] +> : ^^^ + + cb: ((item: T) => void) | ((item: T, extra: unknown) => void), +>cb : ((item: T) => void) | ((item: T, extra: unknown) => void) +> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^ ^^ ^^^^^ ^ +>item : T +> : ^ +>item : T +> : ^ +>extra : unknown +> : ^^^^^^^ + + ) {} + +f2([1, 2, 3], (item) => {}); +>f2([1, 2, 3], (item) => {}) : void +> : ^^^^ +>f2 : (arr: T[], cb: ((item: T) => void) | ((item: T, extra: unknown) => void)) => void +> : ^ ^^ ^^ ^^ ^^ ^^^^^^^^^ +>[1, 2, 3] : number[] +> : ^^^^^^^^ +>1 : 1 +> : ^ +>2 : 2 +> : ^ +>3 : 3 +> : ^ +>(item) => {} : (item: number) => void +> : ^ ^^^^^^^^^^^^^^^^^ +>item : number +> : ^^^^^^ + +export interface AsyncResultCallback { + (err?: E | null, result?: T): void; +>err : E +> : ^ +>result : T +> : ^ +} + +export interface AsyncResultIterator { + (item: T, callback: AsyncResultCallback): void; +>item : T +> : ^ +>callback : AsyncResultCallback +> : ^^^^^^^^^^^^^^^^^^^^^^^^^ +} +export interface AsyncResultIteratorPromise { + (item: T): Promise; +>item : T +> : ^ +} + +declare function mapLimit( +>mapLimit : (arr: T[], limit: number, iterator: AsyncResultIteratorPromise | AsyncResultIterator) => Promise +> : ^ ^^ ^^ ^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^^ + + arr: T[], +>arr : T[] +> : ^^^ + + limit: number, +>limit : number +> : ^^^^^^ + + iterator: AsyncResultIteratorPromise | AsyncResultIterator, +>iterator : AsyncResultIteratorPromise | AsyncResultIterator +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +): Promise; + +mapLimit([1,2,3], 3, async (n) => { +>mapLimit([1,2,3], 3, async (n) => { return n ** 2;}) : Promise +> : ^^^^^^^^^^^^^^^^^ +>mapLimit : (arr: T[], limit: number, iterator: AsyncResultIteratorPromise | AsyncResultIterator) => Promise +> : ^ ^^ ^^ ^^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^^ +>[1,2,3] : number[] +> : ^^^^^^^^ +>1 : 1 +> : ^ +>2 : 2 +> : ^ +>3 : 3 +> : ^ +>3 : 3 +> : ^ +>async (n) => { return n ** 2;} : (n: number) => Promise +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>n : number +> : ^^^^^^ + + return n ** 2; +>n ** 2 : number +> : ^^^^^^ +>n : number +> : ^^^^^^ +>2 : 2 +> : ^ + +}); From 2e530afc4f2373ee3b833245043280f0746c2482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Sun, 18 Aug 2024 10:28:08 +0200 Subject: [PATCH 03/11] format --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2a3a625e839e2..6abf8680c2aba 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32424,9 +32424,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } let signatureList: Signature[] | undefined; const types = (type as UnionType).types - .map(type => ({type, signature: getContextualCallSignature(type, node)})) + .map(type => ({ type, signature: getContextualCallSignature(type, node) })) .filter(type => type.signature) - .sort((t1, t2) => t1.signature!.parameters.length - t2.signature!.parameters.length) + .sort((t1, t2) => t1.signature!.parameters.length - t2.signature!.parameters.length); for (const current of types) { const signature = current.signature!; if (!signatureList) { From ac64f641e6fcdb9b0094b1bed556eee328ee5766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 07:11:13 +0200 Subject: [PATCH 04/11] simplify getContextualSignature --- src/compiler/checker.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6abf8680c2aba..c8cb532b065d4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32422,30 +32422,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (!(type.flags & TypeFlags.Union)) { return getContextualCallSignature(type, node); } - let signatureList: Signature[] | undefined; - const types = (type as UnionType).types - .map(type => ({ type, signature: getContextualCallSignature(type, node) })) - .filter(type => type.signature) - .sort((t1, t2) => t1.signature!.parameters.length - t2.signature!.parameters.length); - for (const current of types) { - const signature = current.signature!; - if (!signatureList) { - // This signature will contribute to contextual union signature - signatureList = [signature]; - } - else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesSubtypeOf)) { + + const signatures = mapDefined((type as UnionType).types, t => getContextualCallSignature(t, node)); + signatures.sort((s1, s2) => s1.parameters.length - s2.parameters.length); + if (!signatures.length) { + return; + } + + for (let i = 1; i < signatures.length; i++) { + if (!compareSignaturesIdentical(signatures[0], signatures[i], /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesSubtypeOf)) { // Signatures aren't identical, do not use return undefined; } - else { - // Use this signature for contextual union signature - signatureList.push(signature); - } } // Result is union of signatures collected (return type is union of return types of this signature set) - if (signatureList) { - return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList); - } + return signatures.length === 1 ? signatures[0] : createUnionSignature(signatures[0], signatures); } function checkGrammarRegularExpressionLiteral(node: RegularExpressionLiteral) { From 6eaf5e36bf63372ce4f4b9c9e2ec31d775c892e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 07:40:46 +0200 Subject: [PATCH 05/11] sort signatures by getMinArgumentCount --- src/compiler/checker.ts | 4 +- .../contextualSignatureWithExtraParameters.js | 34 ++++- ...extualSignatureWithExtraParameters.symbols | 71 +++++++++- ...ntextualSignatureWithExtraParameters.types | 121 +++++++++++++++++- .../contextualSignatureWithExtraParameters.ts | 25 +++- 5 files changed, 244 insertions(+), 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c8cb532b065d4..b040c5535c858 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32424,11 +32424,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } const signatures = mapDefined((type as UnionType).types, t => getContextualCallSignature(t, node)); - signatures.sort((s1, s2) => s1.parameters.length - s2.parameters.length); + signatures.sort((s1, s2) => getMinArgumentCount(s1) - getMinArgumentCount(s2)); if (!signatures.length) { return; } - + for (let i = 1; i < signatures.length; i++) { if (!compareSignaturesIdentical(signatures[0], signatures[i], /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesSubtypeOf)) { // Signatures aren't identical, do not use diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.js b/tests/baselines/reference/contextualSignatureWithExtraParameters.js index 967af3cde6981..4201c70639ca6 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.js +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.js @@ -6,7 +6,7 @@ function f1( cb: ((item: number) => void) | ((item: number, extra: string) => void), ) {} - f1((item) => {}); +f1((item) => {}); function f2( arr: T[], @@ -34,7 +34,28 @@ declare function mapLimit( mapLimit([1,2,3], 3, async (n) => { return n ** 2; -}); +}); + +function f3( + cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) + ) {} + +f3((a) => {}); +f3((a, b) => {}); + +function f4( + cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) + ) {} + +f4((a) => {}); +f4((a, b) => {}); + +function f5( + cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) + ) {} + +f5((a) => {}); +f5((a, b) => {}); //// [contextualSignatureWithExtraParameters.js] "use strict"; @@ -85,3 +106,12 @@ mapLimit([1, 2, 3], 3, function (n) { return __awaiter(void 0, void 0, void 0, f return [2 /*return*/, Math.pow(n, 2)]; }); }); }); +function f3(cb) { } +f3(function (a) { }); +f3(function (a, b) { }); +function f4(cb) { } +f4(function (a) { }); +f4(function (a, b) { }); +function f5(cb) { } +f5(function (a) { }); +f5(function (a, b) { }); diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols index 75ddd62804cd0..2d64f9897a4e7 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols @@ -13,12 +13,12 @@ function f1( ) {} - f1((item) => {}); +f1((item) => {}); >f1 : Symbol(f1, Decl(contextualSignatureWithExtraParameters.ts, 0, 0)) ->item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 5, 6)) +>item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 5, 4)) function f2( ->f2 : Symbol(f2, Decl(contextualSignatureWithExtraParameters.ts, 5, 19)) +>f2 : Symbol(f2, Decl(contextualSignatureWithExtraParameters.ts, 5, 17)) >T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 7, 12)) arr: T[], @@ -36,7 +36,7 @@ function f2( ) {} f2([1, 2, 3], (item) => {}); ->f2 : Symbol(f2, Decl(contextualSignatureWithExtraParameters.ts, 5, 19)) +>f2 : Symbol(f2, Decl(contextualSignatureWithExtraParameters.ts, 5, 17)) >item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 12, 15)) export interface AsyncResultCallback { @@ -115,3 +115,66 @@ mapLimit([1,2,3], 3, async (n) => { >n : Symbol(n, Decl(contextualSignatureWithExtraParameters.ts, 31, 28)) }); + +function f3( +>f3 : Symbol(f3, Decl(contextualSignatureWithExtraParameters.ts, 33, 3)) + + cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 35, 12)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 36, 10)) +>b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 36, 20)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 36, 46)) +>rest : Symbol(rest, Decl(contextualSignatureWithExtraParameters.ts, 36, 56)) + + ) {} + +f3((a) => {}); +>f3 : Symbol(f3, Decl(contextualSignatureWithExtraParameters.ts, 33, 3)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 39, 4)) + +f3((a, b) => {}); +>f3 : Symbol(f3, Decl(contextualSignatureWithExtraParameters.ts, 33, 3)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 40, 4)) +>b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 40, 6)) + +function f4( +>f4 : Symbol(f4, Decl(contextualSignatureWithExtraParameters.ts, 40, 17)) + + cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 42, 12)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 43, 10)) +>b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 43, 20)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 43, 45)) +>rest : Symbol(rest, Decl(contextualSignatureWithExtraParameters.ts, 43, 55)) + + ) {} + +f4((a) => {}); +>f4 : Symbol(f4, Decl(contextualSignatureWithExtraParameters.ts, 40, 17)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 46, 4)) + +f4((a, b) => {}); +>f4 : Symbol(f4, Decl(contextualSignatureWithExtraParameters.ts, 40, 17)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 47, 4)) +>b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 47, 6)) + +function f5( +>f5 : Symbol(f5, Decl(contextualSignatureWithExtraParameters.ts, 47, 17)) + + cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 49, 12)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 50, 10)) +>b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 50, 20)) +>rest : Symbol(rest, Decl(contextualSignatureWithExtraParameters.ts, 50, 45)) + + ) {} + +f5((a) => {}); +>f5 : Symbol(f5, Decl(contextualSignatureWithExtraParameters.ts, 47, 17)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 53, 4)) + +f5((a, b) => {}); +>f5 : Symbol(f5, Decl(contextualSignatureWithExtraParameters.ts, 47, 17)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 54, 4)) +>b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 54, 6)) + diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types index accdb16e23a7f..c3367f139eadd 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.types +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -18,7 +18,7 @@ function f1( ) {} - f1((item) => {}); +f1((item) => {}); >f1((item) => {}) : void > : ^^^^ >f1 : (cb: ((item: number) => void) | ((item: number, extra: string) => void)) => void @@ -134,3 +134,122 @@ mapLimit([1,2,3], 3, async (n) => { > : ^ }); + +function f3( +>f3 : (cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) +>cb : ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) +> : ^^ ^^ ^^ ^^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^^ ^^^^^ ^ +>a : string +> : ^^^^^^ +>b : string +> : ^^^^^^ +>a : string +> : ^^^^^^ +>rest : string[] +> : ^^^^^^^^ + + ) {} + +f3((a) => {}); +>f3((a) => {}) : void +> : ^^^^ +>f3 : (cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(a) => {} : (a: string) => void +> : ^ ^^^^^^^^^^^^^^^^^ +>a : string +> : ^^^^^^ + +f3((a, b) => {}); +>f3((a, b) => {}) : void +> : ^^^^ +>f3 : (cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(a, b) => {} : (a: string, b: string) => void +> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ +>a : string +> : ^^^^^^ +>b : string +> : ^^^^^^ + +function f4( +>f4 : (cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) +>cb : ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) +> : ^^ ^^ ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^^ ^^^^^ ^ +>a : string +> : ^^^^^^ +>b : string +> : ^^^^^^ +>a : string +> : ^^^^^^ +>rest : string[] +> : ^^^^^^^^ + + ) {} + +f4((a) => {}); +>f4((a) => {}) : void +> : ^^^^ +>f4 : (cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(a) => {} : (a: string) => void +> : ^ ^^^^^^^^^^^^^^^^^ +>a : string +> : ^^^^^^ + +f4((a, b) => {}); +>f4((a, b) => {}) : void +> : ^^^^ +>f4 : (cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(a, b) => {} : (a: string, b: string) => void +> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ +>a : string +> : ^^^^^^ +>b : string +> : ^^^^^^ + +function f5( +>f5 : (cb: ((a: string, b: string) => void) | ((...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) +>cb : ((a: string, b: string) => void) | ((...rest: string[]) => void) +> : ^^ ^^ ^^ ^^ ^^^^^ ^^^^^^^^^ ^^ ^^^^^ ^ +>a : string +> : ^^^^^^ +>b : string +> : ^^^^^^ +>rest : string[] +> : ^^^^^^^^ + + ) {} + +f5((a) => {}); +>f5((a) => {}) : void +> : ^^^^ +>f5 : (cb: ((a: string, b: string) => void) | ((...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(a) => {} : (a: string) => void +> : ^ ^^^^^^^^^^^^^^^^^ +>a : string +> : ^^^^^^ + +f5((a, b) => {}); +>f5((a, b) => {}) : void +> : ^^^^ +>f5 : (cb: ((a: string, b: string) => void) | ((...rest: string[]) => void)) => void +> : ^ ^^ ^^^^^^^^^ +>(a, b) => {} : (a: string, b: string) => void +> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ +>a : string +> : ^^^^^^ +>b : string +> : ^^^^^^ + diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts index 20e5cc63ed97a..14566f83087f1 100644 --- a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -3,7 +3,7 @@ function f1( cb: ((item: number) => void) | ((item: number, extra: string) => void), ) {} - f1((item) => {}); +f1((item) => {}); function f2( arr: T[], @@ -31,4 +31,25 @@ declare function mapLimit( mapLimit([1,2,3], 3, async (n) => { return n ** 2; -}); \ No newline at end of file +}); + +function f3( + cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) + ) {} + +f3((a) => {}); +f3((a, b) => {}); + +function f4( + cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) + ) {} + +f4((a) => {}); +f4((a, b) => {}); + +function f5( + cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) + ) {} + +f5((a) => {}); +f5((a, b) => {}); \ No newline at end of file From f69d6e19df9c6ac1c16e9a3e6bfa3428b5176ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 09:24:07 +0200 Subject: [PATCH 06/11] use test file options --- .../contextualSignatureWithExtraParameters.js | 117 ------------------ ...ntextualSignatureWithExtraParameters.types | 20 +-- .../contextualSignatureWithExtraParameters.ts | 3 + 3 files changed, 13 insertions(+), 127 deletions(-) delete mode 100644 tests/baselines/reference/contextualSignatureWithExtraParameters.js diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.js b/tests/baselines/reference/contextualSignatureWithExtraParameters.js deleted file mode 100644 index 4201c70639ca6..0000000000000 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.js +++ /dev/null @@ -1,117 +0,0 @@ -//// [tests/cases/compiler/contextualSignatureWithExtraParameters.ts] //// - -//// [contextualSignatureWithExtraParameters.ts] -// https://github.com/microsoft/TypeScript/issues/59309 -function f1( - cb: ((item: number) => void) | ((item: number, extra: string) => void), - ) {} - -f1((item) => {}); - -function f2( - arr: T[], - cb: ((item: T) => void) | ((item: T, extra: unknown) => void), - ) {} - -f2([1, 2, 3], (item) => {}); - -export interface AsyncResultCallback { - (err?: E | null, result?: T): void; -} - -export interface AsyncResultIterator { - (item: T, callback: AsyncResultCallback): void; -} -export interface AsyncResultIteratorPromise { - (item: T): Promise; -} - -declare function mapLimit( - arr: T[], - limit: number, - iterator: AsyncResultIteratorPromise | AsyncResultIterator, -): Promise; - -mapLimit([1,2,3], 3, async (n) => { - return n ** 2; -}); - -function f3( - cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) - ) {} - -f3((a) => {}); -f3((a, b) => {}); - -function f4( - cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) - ) {} - -f4((a) => {}); -f4((a, b) => {}); - -function f5( - cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) - ) {} - -f5((a) => {}); -f5((a, b) => {}); - -//// [contextualSignatureWithExtraParameters.js] -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); - return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -Object.defineProperty(exports, "__esModule", { value: true }); -// https://github.com/microsoft/TypeScript/issues/59309 -function f1(cb) { } -f1(function (item) { }); -function f2(arr, cb) { } -f2([1, 2, 3], function (item) { }); -mapLimit([1, 2, 3], 3, function (n) { return __awaiter(void 0, void 0, void 0, function () { - return __generator(this, function (_a) { - return [2 /*return*/, Math.pow(n, 2)]; - }); -}); }); -function f3(cb) { } -f3(function (a) { }); -f3(function (a, b) { }); -function f4(cb) { } -f4(function (a) { }); -f4(function (a, b) { }); -function f5(cb) { } -f5(function (a) { }); -f5(function (a, b) { }); diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types index c3367f139eadd..7c217570593c3 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.types +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -68,10 +68,10 @@ f2([1, 2, 3], (item) => {}); export interface AsyncResultCallback { (err?: E | null, result?: T): void; ->err : E -> : ^ ->result : T -> : ^ +>err : E | null | undefined +> : ^^^^^^^^^^^^^^^^^^^^ +>result : T | undefined +> : ^^^^^^^^^^^^^ } export interface AsyncResultIterator { @@ -144,8 +144,8 @@ function f3( > : ^^ ^^ ^^ ^^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^^ ^^^^^ ^ >a : string > : ^^^^^^ ->b : string -> : ^^^^^^ +>b : string | undefined +> : ^^^^^^^^^^^^^^^^^^ >a : string > : ^^^^^^ >rest : string[] @@ -168,12 +168,12 @@ f3((a, b) => {}); > : ^^^^ >f3 : (cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void)) => void > : ^ ^^ ^^^^^^^^^ ->(a, b) => {} : (a: string, b: string) => void -> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ +>(a, b) => {} : (a: string, b: string | undefined) => void +> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >a : string > : ^^^^^^ ->b : string -> : ^^^^^^ +>b : string | undefined +> : ^^^^^^^^^^^^^^^^^^ function f4( >f4 : (cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void)) => void diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts index 14566f83087f1..9beafc409aa9e 100644 --- a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -1,3 +1,6 @@ +// @strict: true +// @noEmit: true + // https://github.com/microsoft/TypeScript/issues/59309 function f1( cb: ((item: number) => void) | ((item: number, extra: string) => void), From 8b0b8556833fbb7fca3ffc6d5de754f65305b9d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 11:48:07 +0200 Subject: [PATCH 07/11] special case async function --- src/compiler/checker.ts | 5 +++- ...extualSignatureWithExtraParameters.symbols | 15 +++++++++++ ...ntextualSignatureWithExtraParameters.types | 26 +++++++++++++++++++ .../contextualSignatureWithExtraParameters.ts | 8 +++++- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b040c5535c858..8b1be4e52e2a5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32423,7 +32423,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return getContextualCallSignature(type, node); } - const signatures = mapDefined((type as UnionType).types, t => getContextualCallSignature(t, node)); + let signatures = mapDefined((type as UnionType).types, t => getContextualCallSignature(t, node)); + if (hasSyntacticModifier(node, ModifierFlags.Async)) { + signatures = signatures.filter(s => !!getAwaitedTypeOfPromise(getReturnTypeOfSignature(s))); + } signatures.sort((s1, s2) => getMinArgumentCount(s1) - getMinArgumentCount(s2)); if (!signatures.length) { return; diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols index 2d64f9897a4e7..781dfbecd55da 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols @@ -178,3 +178,18 @@ f5((a, b) => {}); >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 54, 4)) >b : Symbol(b, Decl(contextualSignatureWithExtraParameters.ts, 54, 6)) +function f6( +>f6 : Symbol(f6, Decl(contextualSignatureWithExtraParameters.ts, 54, 17)) + + cb: ((a: string) => string) | ((a: number) => Promise) +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 56, 12)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 57, 8)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 57, 34)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) + +) {} + +f6(async (a) => {return 0}); +>f6 : Symbol(f6, Decl(contextualSignatureWithExtraParameters.ts, 54, 17)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 60, 10)) + diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types index 7c217570593c3..7e57e72099bb6 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.types +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -253,3 +253,29 @@ f5((a, b) => {}); >b : string > : ^^^^^^ +function f6( +>f6 : (cb: ((a: string) => string) | ((a: number) => Promise)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((a: string) => string) | ((a: number) => Promise) +>cb : ((a: string) => string) | ((a: number) => Promise) +> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^ +>a : string +> : ^^^^^^ +>a : number +> : ^^^^^^ + +) {} + +f6(async (a) => {return 0}); +>f6(async (a) => {return 0}) : void +> : ^^^^ +>f6 : (cb: ((a: string) => string) | ((a: number) => Promise)) => void +> : ^ ^^ ^^^^^^^^^ +>async (a) => {return 0} : (a: number) => Promise +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>a : number +> : ^^^^^^ +>0 : 0 +> : ^ + diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts index 9beafc409aa9e..173fec070aab0 100644 --- a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -55,4 +55,10 @@ function f5( ) {} f5((a) => {}); -f5((a, b) => {}); \ No newline at end of file +f5((a, b) => {}); + +function f6( + cb: ((a: string) => string) | ((a: number) => Promise) +) {} + +f6(async (a) => {return 0}); \ No newline at end of file From 8a3b095b108a788d69687a674e7bad3c07be41ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 12:51:25 +0200 Subject: [PATCH 08/11] fix logic for async functions --- src/compiler/checker.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8b1be4e52e2a5..bdb0015b46d06 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32424,8 +32424,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } let signatures = mapDefined((type as UnionType).types, t => getContextualCallSignature(t, node)); - if (hasSyntacticModifier(node, ModifierFlags.Async)) { - signatures = signatures.filter(s => !!getAwaitedTypeOfPromise(getReturnTypeOfSignature(s))); + const functionFlags = getFunctionFlags(node); + if (functionFlags === FunctionFlags.Async) { + signatures = filter(signatures, s => !!getAwaitedTypeOfPromise(getReturnTypeOfSignature(s))); } signatures.sort((s1, s2) => getMinArgumentCount(s1) - getMinArgumentCount(s2)); if (!signatures.length) { From dcb8f5d9d0d01d6b9fc0ec24abc916d6b8d572b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 13:41:06 +0200 Subject: [PATCH 09/11] special case generators --- src/compiler/checker.ts | 5 +- ...ualSignatureWithExtraParameters.errors.txt | 89 +++++++++++++++++++ ...extualSignatureWithExtraParameters.symbols | 33 +++++++ ...ntextualSignatureWithExtraParameters.types | 61 +++++++++++++ .../contextualSignatureWithExtraParameters.ts | 15 +++- 5 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bdb0015b46d06..101947565e916 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -32425,7 +32425,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { let signatures = mapDefined((type as UnionType).types, t => getContextualCallSignature(t, node)); const functionFlags = getFunctionFlags(node); - if (functionFlags === FunctionFlags.Async) { + if (functionFlags & FunctionFlags.Generator) { + signatures = filter(signatures, s => checkGeneratorInstantiationAssignabilityToReturnType(getReturnTypeOfSignature(s), functionFlags, /*errorNode*/ undefined)); + } + else if (functionFlags & FunctionFlags.Async) { signatures = filter(signatures, s => !!getAwaitedTypeOfPromise(getReturnTypeOfSignature(s))); } signatures.sort((s1, s2) => getMinArgumentCount(s1) - getMinArgumentCount(s2)); diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt b/tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt new file mode 100644 index 0000000000000..c02789c92b9ca --- /dev/null +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt @@ -0,0 +1,89 @@ +error TS2318: Cannot find global type 'AsyncIterableIterator'. +error TS2318: Cannot find global type 'IterableIterator'. +contextualSignatureWithExtraParameters.ts(64,49): error TS2552: Cannot find name 'Generator'. Did you mean 'Enumerator'? +contextualSignatureWithExtraParameters.ts(70,49): error TS2583: Cannot find name 'AsyncGenerator'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2018' or later. + + +!!! error TS2318: Cannot find global type 'AsyncIterableIterator'. +!!! error TS2318: Cannot find global type 'IterableIterator'. +==== contextualSignatureWithExtraParameters.ts (2 errors) ==== + // https://github.com/microsoft/TypeScript/issues/59309 + function f1( + cb: ((item: number) => void) | ((item: number, extra: string) => void), + ) {} + + f1((item) => {}); + + function f2( + arr: T[], + cb: ((item: T) => void) | ((item: T, extra: unknown) => void), + ) {} + + f2([1, 2, 3], (item) => {}); + + export interface AsyncResultCallback { + (err?: E | null, result?: T): void; + } + + export interface AsyncResultIterator { + (item: T, callback: AsyncResultCallback): void; + } + export interface AsyncResultIteratorPromise { + (item: T): Promise; + } + + declare function mapLimit( + arr: T[], + limit: number, + iterator: AsyncResultIteratorPromise | AsyncResultIterator, + ): Promise; + + mapLimit([1,2,3], 3, async (n) => { + return n ** 2; + }); + + function f3( + cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) + ) {} + + f3((a) => {}); + f3((a, b) => {}); + + function f4( + cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) + ) {} + + f4((a) => {}); + f4((a, b) => {}); + + function f5( + cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) + ) {} + + f5((a) => {}); + f5((a, b) => {}); + + function f6( + cb: ((a: string) => string) | ((a: number) => Promise) + ) {} + + f6(async (a) => {return 0}); + + function f7( + cb: ((a: string) => string) | ((a: number) => Generator) + ~~~~~~~~~ +!!! error TS2552: Cannot find name 'Generator'. Did you mean 'Enumerator'? +!!! related TS2728 lib.scripthost.d.ts:--:--: 'Enumerator' is declared here. + ) {} + + f7(function* generator(a) {yield 0}); + + function f8( + cb: ((a: string) => string) | ((a: number) => AsyncGenerator) + ~~~~~~~~~~~~~~ +!!! error TS2583: Cannot find name 'AsyncGenerator'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2018' or later. + ) {} + + f8(async function* generator(a) {yield 0}); + + \ No newline at end of file diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols index 781dfbecd55da..1d922b961b202 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols @@ -193,3 +193,36 @@ f6(async (a) => {return 0}); >f6 : Symbol(f6, Decl(contextualSignatureWithExtraParameters.ts, 54, 17)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 60, 10)) +function f7( +>f7 : Symbol(f7, Decl(contextualSignatureWithExtraParameters.ts, 60, 28)) + + cb: ((a: string) => string) | ((a: number) => Generator) +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 62, 12)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 63, 8)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 63, 34)) +>Generator : Symbol(Generator) + +) {} + +f7(function* generator(a) {yield 0}); +>f7 : Symbol(f7, Decl(contextualSignatureWithExtraParameters.ts, 60, 28)) +>generator : Symbol(generator, Decl(contextualSignatureWithExtraParameters.ts, 66, 3)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 66, 23)) + +function f8( +>f8 : Symbol(f8, Decl(contextualSignatureWithExtraParameters.ts, 66, 37)) + + cb: ((a: string) => string) | ((a: number) => AsyncGenerator) +>cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 68, 12)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 69, 8)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 69, 34)) +>AsyncGenerator : Symbol(AsyncGenerator) + +) {} + +f8(async function* generator(a) {yield 0}); +>f8 : Symbol(f8, Decl(contextualSignatureWithExtraParameters.ts, 66, 37)) +>generator : Symbol(generator, Decl(contextualSignatureWithExtraParameters.ts, 72, 3)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 72, 29)) + + diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types index 7e57e72099bb6..120f51c7687b8 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.types +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -279,3 +279,64 @@ f6(async (a) => {return 0}); >0 : 0 > : ^ +function f7( +>f7 : (cb: ((a: string) => string) | ((a: number) => Generator)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((a: string) => string) | ((a: number) => Generator) +>cb : ((a: string) => string) | ((a: number) => Generator) +> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^ +>a : string +> : ^^^^^^ +>a : number +> : ^^^^^^ + +) {} + +f7(function* generator(a) {yield 0}); +>f7(function* generator(a) {yield 0}) : void +> : ^^^^ +>f7 : (cb: ((a: string) => string) | ((a: number) => Generator)) => void +> : ^ ^^ ^^^^^^^^^ +>function* generator(a) {yield 0} : (a: number) => {} +> : ^ ^^^^^^^^^^^^^^^ +>generator : (a: number) => {} +> : ^ ^^^^^^^^^^^^^^^ +>a : number +> : ^^^^^^ +>yield 0 : any +> : ^^^ +>0 : 0 +> : ^ + +function f8( +>f8 : (cb: ((a: string) => string) | ((a: number) => AsyncGenerator)) => void +> : ^ ^^ ^^^^^^^^^ + + cb: ((a: string) => string) | ((a: number) => AsyncGenerator) +>cb : ((a: string) => string) | ((a: number) => AsyncGenerator) +> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^ +>a : string +> : ^^^^^^ +>a : number +> : ^^^^^^ + +) {} + +f8(async function* generator(a) {yield 0}); +>f8(async function* generator(a) {yield 0}) : void +> : ^^^^ +>f8 : (cb: ((a: string) => string) | ((a: number) => AsyncGenerator)) => void +> : ^ ^^ ^^^^^^^^^ +>async function* generator(a) {yield 0} : (a: number) => {} +> : ^ ^^^^^^^^^^^^^^^ +>generator : (a: number) => {} +> : ^ ^^^^^^^^^^^^^^^ +>a : number +> : ^^^^^^ +>yield 0 : any +> : ^^^ +>0 : 0 +> : ^ + + diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts index 173fec070aab0..88e9b376b235e 100644 --- a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -61,4 +61,17 @@ function f6( cb: ((a: string) => string) | ((a: number) => Promise) ) {} -f6(async (a) => {return 0}); \ No newline at end of file +f6(async (a) => {return 0}); + +function f7( + cb: ((a: string) => string) | ((a: number) => Generator) +) {} + +f7(function* generator(a) {yield 0}); + +function f8( + cb: ((a: string) => string) | ((a: number) => AsyncGenerator) +) {} + +f8(async function* generator(a) {yield 0}); + From 4fd6bbdbd7eebf306e9c357e8b09a629d7a25558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 15:21:58 +0200 Subject: [PATCH 10/11] update tests --- ...ualSignatureWithExtraParameters.errors.txt | 89 ------------------- ...extualSignatureWithExtraParameters.symbols | 16 ++-- ...ntextualSignatureWithExtraParameters.types | 24 ++--- .../contextualSignatureWithExtraParameters.ts | 2 + 4 files changed, 22 insertions(+), 109 deletions(-) delete mode 100644 tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt b/tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt deleted file mode 100644 index c02789c92b9ca..0000000000000 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.errors.txt +++ /dev/null @@ -1,89 +0,0 @@ -error TS2318: Cannot find global type 'AsyncIterableIterator'. -error TS2318: Cannot find global type 'IterableIterator'. -contextualSignatureWithExtraParameters.ts(64,49): error TS2552: Cannot find name 'Generator'. Did you mean 'Enumerator'? -contextualSignatureWithExtraParameters.ts(70,49): error TS2583: Cannot find name 'AsyncGenerator'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2018' or later. - - -!!! error TS2318: Cannot find global type 'AsyncIterableIterator'. -!!! error TS2318: Cannot find global type 'IterableIterator'. -==== contextualSignatureWithExtraParameters.ts (2 errors) ==== - // https://github.com/microsoft/TypeScript/issues/59309 - function f1( - cb: ((item: number) => void) | ((item: number, extra: string) => void), - ) {} - - f1((item) => {}); - - function f2( - arr: T[], - cb: ((item: T) => void) | ((item: T, extra: unknown) => void), - ) {} - - f2([1, 2, 3], (item) => {}); - - export interface AsyncResultCallback { - (err?: E | null, result?: T): void; - } - - export interface AsyncResultIterator { - (item: T, callback: AsyncResultCallback): void; - } - export interface AsyncResultIteratorPromise { - (item: T): Promise; - } - - declare function mapLimit( - arr: T[], - limit: number, - iterator: AsyncResultIteratorPromise | AsyncResultIterator, - ): Promise; - - mapLimit([1,2,3], 3, async (n) => { - return n ** 2; - }); - - function f3( - cb: ((a: string, b?: string) => void) | ((a: string, ...rest: string[]) => void) - ) {} - - f3((a) => {}); - f3((a, b) => {}); - - function f4( - cb: ((a: string, b: string) => void) | ((a: string, ...rest: string[]) => void) - ) {} - - f4((a) => {}); - f4((a, b) => {}); - - function f5( - cb: ((a: string, b: string) => void) | ((...rest: string[]) => void) - ) {} - - f5((a) => {}); - f5((a, b) => {}); - - function f6( - cb: ((a: string) => string) | ((a: number) => Promise) - ) {} - - f6(async (a) => {return 0}); - - function f7( - cb: ((a: string) => string) | ((a: number) => Generator) - ~~~~~~~~~ -!!! error TS2552: Cannot find name 'Generator'. Did you mean 'Enumerator'? -!!! related TS2728 lib.scripthost.d.ts:--:--: 'Enumerator' is declared here. - ) {} - - f7(function* generator(a) {yield 0}); - - function f8( - cb: ((a: string) => string) | ((a: number) => AsyncGenerator) - ~~~~~~~~~~~~~~ -!!! error TS2583: Cannot find name 'AsyncGenerator'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2018' or later. - ) {} - - f8(async function* generator(a) {yield 0}); - - \ No newline at end of file diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols index 1d922b961b202..044f28d466f25 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols @@ -43,7 +43,7 @@ export interface AsyncResultCallback { >AsyncResultCallback : Symbol(AsyncResultCallback, Decl(contextualSignatureWithExtraParameters.ts, 12, 28)) >T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 14, 37)) >E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 14, 39)) ->Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) (err?: E | null, result?: T): void; >err : Symbol(err, Decl(contextualSignatureWithExtraParameters.ts, 15, 5)) @@ -57,7 +57,7 @@ export interface AsyncResultIterator { >T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 18, 37)) >R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 18, 39)) >E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 18, 42)) ->Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) (item: T, callback: AsyncResultCallback): void; >item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 19, 5)) @@ -75,7 +75,7 @@ export interface AsyncResultIteratorPromise { (item: T): Promise; >item : Symbol(item, Decl(contextualSignatureWithExtraParameters.ts, 22, 5)) >T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 21, 44)) ->Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) >R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 21, 46)) } @@ -84,7 +84,7 @@ declare function mapLimit( >T : Symbol(T, Decl(contextualSignatureWithExtraParameters.ts, 25, 26)) >R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 25, 28)) >E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 25, 31)) ->Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) arr: T[], >arr : Symbol(arr, Decl(contextualSignatureWithExtraParameters.ts, 25, 43)) @@ -104,7 +104,7 @@ declare function mapLimit( >E : Symbol(E, Decl(contextualSignatureWithExtraParameters.ts, 25, 31)) ): Promise; ->Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) >R : Symbol(R, Decl(contextualSignatureWithExtraParameters.ts, 25, 28)) mapLimit([1,2,3], 3, async (n) => { @@ -185,7 +185,7 @@ function f6( >cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 56, 12)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 57, 8)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 57, 34)) ->Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) ) {} @@ -200,7 +200,7 @@ function f7( >cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 62, 12)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 63, 8)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 63, 34)) ->Generator : Symbol(Generator) +>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --)) ) {} @@ -216,7 +216,7 @@ function f8( >cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 68, 12)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 69, 8)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 69, 34)) ->AsyncGenerator : Symbol(AsyncGenerator) +>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --)) ) {} diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types index 120f51c7687b8..4e062037ed057 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.types +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -298,14 +298,14 @@ f7(function* generator(a) {yield 0}); > : ^^^^ >f7 : (cb: ((a: string) => string) | ((a: number) => Generator)) => void > : ^ ^^ ^^^^^^^^^ ->function* generator(a) {yield 0} : (a: number) => {} -> : ^ ^^^^^^^^^^^^^^^ ->generator : (a: number) => {} -> : ^ ^^^^^^^^^^^^^^^ +>function* generator(a) {yield 0} : (a: number) => Generator +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>generator : (a: number) => Generator +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >a : number > : ^^^^^^ ->yield 0 : any -> : ^^^ +>yield 0 : unknown +> : ^^^^^^^ >0 : 0 > : ^ @@ -328,14 +328,14 @@ f8(async function* generator(a) {yield 0}); > : ^^^^ >f8 : (cb: ((a: string) => string) | ((a: number) => AsyncGenerator)) => void > : ^ ^^ ^^^^^^^^^ ->async function* generator(a) {yield 0} : (a: number) => {} -> : ^ ^^^^^^^^^^^^^^^ ->generator : (a: number) => {} -> : ^ ^^^^^^^^^^^^^^^ +>async function* generator(a) {yield 0} : (a: number) => AsyncGenerator +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>generator : (a: number) => AsyncGenerator +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >a : number > : ^^^^^^ ->yield 0 : any -> : ^^^ +>yield 0 : unknown +> : ^^^^^^^ >0 : 0 > : ^ diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts index 88e9b376b235e..d67af592f9cfb 100644 --- a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -1,4 +1,6 @@ // @strict: true +// @target: esnext +// @lib: esnext // @noEmit: true // https://github.com/microsoft/TypeScript/issues/59309 From 381e4e08c8295e551dd50b538f92ff9845933d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mar=C5=A1=C3=A1lek?= Date: Mon, 19 Aug 2024 15:33:06 +0200 Subject: [PATCH 11/11] update tests --- ...extualSignatureWithExtraParameters.symbols | 9 ++--- ...ntextualSignatureWithExtraParameters.types | 36 +++++++++---------- .../contextualSignatureWithExtraParameters.ts | 4 +-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols index 044f28d466f25..0016764e832c5 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.symbols @@ -181,7 +181,7 @@ f5((a, b) => {}); function f6( >f6 : Symbol(f6, Decl(contextualSignatureWithExtraParameters.ts, 54, 17)) - cb: ((a: string) => string) | ((a: number) => Promise) + cb: ((a: string) => string) | ((a: "a" | "b") => Promise<"a" | "b">) >cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 56, 12)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 57, 8)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 57, 34)) @@ -189,12 +189,13 @@ function f6( ) {} -f6(async (a) => {return 0}); +f6(async (a) => a); >f6 : Symbol(f6, Decl(contextualSignatureWithExtraParameters.ts, 54, 17)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 60, 10)) +>a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 60, 10)) function f7( ->f7 : Symbol(f7, Decl(contextualSignatureWithExtraParameters.ts, 60, 28)) +>f7 : Symbol(f7, Decl(contextualSignatureWithExtraParameters.ts, 60, 19)) cb: ((a: string) => string) | ((a: number) => Generator) >cb : Symbol(cb, Decl(contextualSignatureWithExtraParameters.ts, 62, 12)) @@ -205,7 +206,7 @@ function f7( ) {} f7(function* generator(a) {yield 0}); ->f7 : Symbol(f7, Decl(contextualSignatureWithExtraParameters.ts, 60, 28)) +>f7 : Symbol(f7, Decl(contextualSignatureWithExtraParameters.ts, 60, 19)) >generator : Symbol(generator, Decl(contextualSignatureWithExtraParameters.ts, 66, 3)) >a : Symbol(a, Decl(contextualSignatureWithExtraParameters.ts, 66, 23)) diff --git a/tests/baselines/reference/contextualSignatureWithExtraParameters.types b/tests/baselines/reference/contextualSignatureWithExtraParameters.types index 4e062037ed057..7b4eea10094be 100644 --- a/tests/baselines/reference/contextualSignatureWithExtraParameters.types +++ b/tests/baselines/reference/contextualSignatureWithExtraParameters.types @@ -254,30 +254,30 @@ f5((a, b) => {}); > : ^^^^^^ function f6( ->f6 : (cb: ((a: string) => string) | ((a: number) => Promise)) => void -> : ^ ^^ ^^^^^^^^^ +>f6 : (cb: ((a: string) => string) | ((a: "a" | "b") => Promise<"a" | "b">)) => void +> : ^ ^^ ^^^^^^^^^ - cb: ((a: string) => string) | ((a: number) => Promise) ->cb : ((a: string) => string) | ((a: number) => Promise) -> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^ + cb: ((a: string) => string) | ((a: "a" | "b") => Promise<"a" | "b">) +>cb : ((a: string) => string) | ((a: "a" | "b") => Promise<"a" | "b">) +> : ^^ ^^ ^^^^^ ^^^^^^ ^^ ^^^^^ ^ >a : string > : ^^^^^^ ->a : number -> : ^^^^^^ +>a : "a" | "b" +> : ^^^^^^^^^ ) {} -f6(async (a) => {return 0}); ->f6(async (a) => {return 0}) : void -> : ^^^^ ->f6 : (cb: ((a: string) => string) | ((a: number) => Promise)) => void -> : ^ ^^ ^^^^^^^^^ ->async (a) => {return 0} : (a: number) => Promise -> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->a : number -> : ^^^^^^ ->0 : 0 -> : ^ +f6(async (a) => a); +>f6(async (a) => a) : void +> : ^^^^ +>f6 : (cb: ((a: string) => string) | ((a: "a" | "b") => Promise<"a" | "b">)) => void +> : ^ ^^ ^^^^^^^^^ +>async (a) => a : (a: "a" | "b") => Promise<"a" | "b"> +> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>a : "a" | "b" +> : ^^^^^^^^^ +>a : "a" | "b" +> : ^^^^^^^^^ function f7( >f7 : (cb: ((a: string) => string) | ((a: number) => Generator)) => void diff --git a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts index d67af592f9cfb..a5382c0c4dc96 100644 --- a/tests/cases/compiler/contextualSignatureWithExtraParameters.ts +++ b/tests/cases/compiler/contextualSignatureWithExtraParameters.ts @@ -60,10 +60,10 @@ f5((a) => {}); f5((a, b) => {}); function f6( - cb: ((a: string) => string) | ((a: number) => Promise) + cb: ((a: string) => string) | ((a: "a" | "b") => Promise<"a" | "b">) ) {} -f6(async (a) => {return 0}); +f6(async (a) => a); function f7( cb: ((a: string) => string) | ((a: number) => Generator)