From 20d2d3d1690b1fdbe9024d76c4ca1036f6cffd5b Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 16 Aug 2024 16:59:16 -0400 Subject: [PATCH 1/2] Report implict any error when widening null/undefined in presence of generic contextual return type --- src/compiler/checker.ts | 38 +++++++- src/compiler/diagnosticMessages.json | 2 +- ...torReturnTypeInferenceNonStrict.errors.txt | 4 +- .../reference/generatorTypeCheck62.errors.txt | 5 +- ...implicitAnyGenericTypeInference.errors.txt | 46 +++++++++ .../implicitAnyGenericTypeInference.js | 28 +++++- .../implicitAnyGenericTypeInference.symbols | 59 ++++++++++++ .../implicitAnyGenericTypeInference.types | 93 +++++++++++++++++++ .../implicitAnyGenericTypeInference.ts | 21 ++++- 9 files changed, 284 insertions(+), 12 deletions(-) create mode 100644 tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1673c6c032716..91b8556b8a267 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25513,7 +25513,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { case SyntaxKind.ArrowFunction: if (noImplicitAny && !(declaration as NamedDeclaration).name) { if (wideningKind === WideningKind.GeneratorYield) { - error(declaration, Diagnostics.Generator_implicitly_has_yield_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type_annotation, typeAsString); + error(declaration, Diagnostics.Generator_implicitly_has_yield_type_0_Consider_supplying_a_return_type_annotation, typeAsString); } else { error(declaration, Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); @@ -25535,12 +25535,40 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { errorOrSuggestion(noImplicitAny, declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString); } + function reportErrorsFromWideningWithContextualSignature(declaration: FunctionLikeDeclaration, wideningKind: WideningKind) { + const signature = getContextualSignatureForFunctionLikeDeclaration(declaration); + if (!signature) { + return true; + } + let returnType = getReturnTypeOfSignature(signature); + const flags = getFunctionFlags(declaration); + switch (wideningKind) { + case WideningKind.FunctionReturn: + if (flags & FunctionFlags.Generator) { + returnType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, returnType, !!(flags & FunctionFlags.Async)) ?? returnType; + } + else if (flags & FunctionFlags.Async) { + returnType = getAwaitedTypeNoAlias(returnType) ?? returnType; + } + return isGenericType(returnType); + case WideningKind.GeneratorYield: + const yieldType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Yield, returnType, !!(flags & FunctionFlags.Async)); + return !!yieldType && isGenericType(yieldType); + case WideningKind.GeneratorNext: + const nextType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Next, returnType, !!(flags & FunctionFlags.Async)); + return !!nextType && isGenericType(nextType); + } + return false; + } + function reportErrorsFromWidening(declaration: Declaration, type: Type, wideningKind?: WideningKind) { addLazyDiagnostic(() => { - if (noImplicitAny && getObjectFlags(type) & ObjectFlags.ContainsWideningType && (!wideningKind || !getContextualSignatureForFunctionLikeDeclaration(declaration as FunctionLikeDeclaration))) { - // Report implicit any error within type if possible, otherwise report error on declaration - if (!reportWideningErrorsInType(type)) { - reportImplicitAny(declaration, type, wideningKind); + if (noImplicitAny && getObjectFlags(type) & ObjectFlags.ContainsWideningType) { + if (!wideningKind || isFunctionLikeDeclaration(declaration) && reportErrorsFromWideningWithContextualSignature(declaration, wideningKind)) { + // Report implicit any error within type if possible, otherwise report error on declaration + if (!reportWideningErrorsInType(type)) { + reportImplicitAny(declaration, type, wideningKind); + } } } }); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 36213fbc314e8..43f4a3eac49f4 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -6626,7 +6626,7 @@ "category": "Error", "code": 7024 }, - "Generator implicitly has yield type '{0}' because it does not yield any values. Consider supplying a return type annotation.": { + "Generator implicitly has yield type '{0}'. Consider supplying a return type annotation.": { "category": "Error", "code": 7025 }, diff --git a/tests/baselines/reference/generatorReturnTypeInferenceNonStrict.errors.txt b/tests/baselines/reference/generatorReturnTypeInferenceNonStrict.errors.txt index 6367bd3d18754..9b69d36b6ed24 100644 --- a/tests/baselines/reference/generatorReturnTypeInferenceNonStrict.errors.txt +++ b/tests/baselines/reference/generatorReturnTypeInferenceNonStrict.errors.txt @@ -4,7 +4,7 @@ generatorReturnTypeInferenceNonStrict.ts(74,15): error TS7057: 'yield' expressio generatorReturnTypeInferenceNonStrict.ts(79,11): error TS7055: 'g301', which lacks return-type annotation, implicitly has an 'any' yield type. generatorReturnTypeInferenceNonStrict.ts(89,11): error TS7055: 'g303', which lacks return-type annotation, implicitly has an 'any' yield type. generatorReturnTypeInferenceNonStrict.ts(126,11): error TS7055: 'g310', which lacks return-type annotation, implicitly has an 'any' yield type. -generatorReturnTypeInferenceNonStrict.ts(131,10): error TS7025: Generator implicitly has yield type 'any' because it does not yield any values. Consider supplying a return type annotation. +generatorReturnTypeInferenceNonStrict.ts(131,10): error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. ==== generatorReturnTypeInferenceNonStrict.ts (7 errors) ==== @@ -152,7 +152,7 @@ generatorReturnTypeInferenceNonStrict.ts(131,10): error TS7025: Generator implic function* g311() { // Generator yield* (function*() { ~~~~~~~~ -!!! error TS7025: Generator implicitly has yield type 'any' because it does not yield any values. Consider supplying a return type annotation. +!!! error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. const y: string = yield; })(); } diff --git a/tests/baselines/reference/generatorTypeCheck62.errors.txt b/tests/baselines/reference/generatorTypeCheck62.errors.txt index 6599e8fdd0109..0086af9d41938 100644 --- a/tests/baselines/reference/generatorTypeCheck62.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck62.errors.txt @@ -12,9 +12,10 @@ generatorTypeCheck62.ts(32,62): error TS2345: Argument of type '(state: State) = Type 'IteratorReturnResult' is not assignable to type 'IteratorResult'. Type 'IteratorReturnResult' is not assignable to type 'IteratorReturnResult'. Type 'State' is not assignable to type 'void'. +generatorTypeCheck62.ts(32,62): error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. -==== generatorTypeCheck62.ts (2 errors) ==== +==== generatorTypeCheck62.ts (3 errors) ==== export interface StrategicState { lastStrategyApplied?: string; } @@ -63,6 +64,8 @@ generatorTypeCheck62.ts(32,62): error TS2345: Argument of type '(state: State) = !!! error TS2345: Type 'IteratorReturnResult' is not assignable to type 'IteratorResult'. !!! error TS2345: Type 'IteratorReturnResult' is not assignable to type 'IteratorReturnResult'. !!! error TS2345: Type 'State' is not assignable to type 'void'. + ~~~~~~~~ +!!! error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. yield ; return state; // `return`/`TReturn` isn't supported by `strategy`, so this should error. }); diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt b/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt new file mode 100644 index 0000000000000..fd2703e3f7374 --- /dev/null +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt @@ -0,0 +1,46 @@ +implicitAnyGenericTypeInference.ts(10,4): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. +implicitAnyGenericTypeInference.ts(13,4): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. +implicitAnyGenericTypeInference.ts(16,4): error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. +implicitAnyGenericTypeInference.ts(19,4): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. +implicitAnyGenericTypeInference.ts(22,4): error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. +implicitAnyGenericTypeInference.ts(25,4): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. + + +==== implicitAnyGenericTypeInference.ts (6 errors) ==== + interface Comparer { + compareTo(x: T, y: U): U; + } + + var c: Comparer; + c = { compareTo: (x, y) => { return y; } }; + var r = c.compareTo(1, ''); + + declare function f1(cb: () => T): void; + f1(() => null); + ~~~~~~~~~~ +!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. + + declare function f2(cb: () => PromiseLike): void; + f2(async () => null); + ~~~~~~~~~~~~~~~~ +!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. + + declare function f3(cb: () => Generator): void; + f3(function* () { yield null; }); + ~~~~~~~~ +!!! error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. + + declare function f4(cb: () => Generator): void; + f4(function* () { return null; }); + ~~~~~~~~ +!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. + + declare function f5(cb: () => AsyncGenerator): void; + f5(async function* () { yield null; }); + ~~~~~ +!!! error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. + + declare function f6(cb: () => AsyncGenerator): void; + f6(async function* () { return null; }); + ~~~~~ +!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. \ No newline at end of file diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.js b/tests/baselines/reference/implicitAnyGenericTypeInference.js index 0f683cef999a7..6f98c792250e3 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.js +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.js @@ -7,9 +7,33 @@ interface Comparer { var c: Comparer; c = { compareTo: (x, y) => { return y; } }; -var r = c.compareTo(1, ''); +var r = c.compareTo(1, ''); + +declare function f1(cb: () => T): void; +f1(() => null); + +declare function f2(cb: () => PromiseLike): void; +f2(async () => null); + +declare function f3(cb: () => Generator): void; +f3(function* () { yield null; }); + +declare function f4(cb: () => Generator): void; +f4(function* () { return null; }); + +declare function f5(cb: () => AsyncGenerator): void; +f5(async function* () { yield null; }); + +declare function f6(cb: () => AsyncGenerator): void; +f6(async function* () { return null; }); //// [implicitAnyGenericTypeInference.js] var c; -c = { compareTo: function (x, y) { return y; } }; +c = { compareTo: (x, y) => { return y; } }; var r = c.compareTo(1, ''); +f1(() => null); +f2(async () => null); +f3(function* () { yield null; }); +f4(function* () { return null; }); +f5(async function* () { yield null; }); +f6(async function* () { return null; }); diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.symbols b/tests/baselines/reference/implicitAnyGenericTypeInference.symbols index 7b2eaa1eb3332..5b7f6d7dccc2b 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.symbols +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.symbols @@ -32,3 +32,62 @@ var r = c.compareTo(1, ''); >c : Symbol(c, Decl(implicitAnyGenericTypeInference.ts, 4, 3)) >compareTo : Symbol(Comparer.compareTo, Decl(implicitAnyGenericTypeInference.ts, 0, 23)) +declare function f1(cb: () => T): void; +>f1 : Symbol(f1, Decl(implicitAnyGenericTypeInference.ts, 6, 27)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 8, 20)) +>cb : Symbol(cb, Decl(implicitAnyGenericTypeInference.ts, 8, 23)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 8, 20)) + +f1(() => null); +>f1 : Symbol(f1, Decl(implicitAnyGenericTypeInference.ts, 6, 27)) + +declare function f2(cb: () => PromiseLike): void; +>f2 : Symbol(f2, Decl(implicitAnyGenericTypeInference.ts, 9, 15)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 11, 20)) +>cb : Symbol(cb, Decl(implicitAnyGenericTypeInference.ts, 11, 23)) +>PromiseLike : Symbol(PromiseLike, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 11, 20)) + +f2(async () => null); +>f2 : Symbol(f2, Decl(implicitAnyGenericTypeInference.ts, 9, 15)) + +declare function f3(cb: () => Generator): void; +>f3 : Symbol(f3, Decl(implicitAnyGenericTypeInference.ts, 12, 21)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 14, 20)) +>cb : Symbol(cb, Decl(implicitAnyGenericTypeInference.ts, 14, 23)) +>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 14, 20)) + +f3(function* () { yield null; }); +>f3 : Symbol(f3, Decl(implicitAnyGenericTypeInference.ts, 12, 21)) + +declare function f4(cb: () => Generator): void; +>f4 : Symbol(f4, Decl(implicitAnyGenericTypeInference.ts, 15, 33)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 17, 20)) +>cb : Symbol(cb, Decl(implicitAnyGenericTypeInference.ts, 17, 23)) +>Generator : Symbol(Generator, Decl(lib.es2015.generator.d.ts, --, --)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 17, 20)) + +f4(function* () { return null; }); +>f4 : Symbol(f4, Decl(implicitAnyGenericTypeInference.ts, 15, 33)) + +declare function f5(cb: () => AsyncGenerator): void; +>f5 : Symbol(f5, Decl(implicitAnyGenericTypeInference.ts, 18, 34)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 20, 20)) +>cb : Symbol(cb, Decl(implicitAnyGenericTypeInference.ts, 20, 23)) +>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 20, 20)) + +f5(async function* () { yield null; }); +>f5 : Symbol(f5, Decl(implicitAnyGenericTypeInference.ts, 18, 34)) + +declare function f6(cb: () => AsyncGenerator): void; +>f6 : Symbol(f6, Decl(implicitAnyGenericTypeInference.ts, 21, 39)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 23, 20)) +>cb : Symbol(cb, Decl(implicitAnyGenericTypeInference.ts, 23, 23)) +>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --)) +>T : Symbol(T, Decl(implicitAnyGenericTypeInference.ts, 23, 20)) + +f6(async function* () { return null; }); +>f6 : Symbol(f6, Decl(implicitAnyGenericTypeInference.ts, 21, 39)) + diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.types b/tests/baselines/reference/implicitAnyGenericTypeInference.types index f297981afc45e..3675c67fed393 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.types +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.types @@ -1,5 +1,9 @@ //// [tests/cases/compiler/implicitAnyGenericTypeInference.ts] //// +=== Performance Stats === +Type Count: 1,000 +Instantiation count: 2,500 + === implicitAnyGenericTypeInference.ts === interface Comparer { compareTo(x: T, y: U): U; @@ -27,6 +31,7 @@ c = { compareTo: (x, y) => { return y; } }; >(x, y) => { return y; } : (x: any, y: U) => U > : ^ ^^ ^^^^^^^ ^^^^^^^^^ >x : any +> : ^^^ >y : U > : ^ >y : U @@ -48,3 +53,91 @@ var r = c.compareTo(1, ''); >'' : "" > : ^^ +declare function f1(cb: () => T): void; +>f1 : (cb: () => T) => void +> : ^ ^^ ^^ ^^^^^ +>cb : () => T +> : ^^^^^^ + +f1(() => null); +>f1(() => null) : void +> : ^^^^ +>f1 : (cb: () => T) => void +> : ^ ^^ ^^ ^^^^^ +>() => null : () => any +> : ^^^^^^^^^ + +declare function f2(cb: () => PromiseLike): void; +>f2 : (cb: () => PromiseLike) => void +> : ^ ^^ ^^ ^^^^^ +>cb : () => PromiseLike +> : ^^^^^^ + +f2(async () => null); +>f2(async () => null) : void +> : ^^^^ +>f2 : (cb: () => PromiseLike) => void +> : ^ ^^ ^^ ^^^^^ +>async () => null : () => Promise +> : ^^^^^^^^^^^^^^^^^^ + +declare function f3(cb: () => Generator): void; +>f3 : (cb: () => Generator) => void +> : ^ ^^ ^^ ^^^^^ +>cb : () => Generator +> : ^^^^^^ + +f3(function* () { yield null; }); +>f3(function* () { yield null; }) : void +> : ^^^^ +>f3 : (cb: () => Generator) => void +> : ^ ^^ ^^ ^^^^^ +>function* () { yield null; } : () => Generator +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>yield null : any +> : ^^^ + +declare function f4(cb: () => Generator): void; +>f4 : (cb: () => Generator) => void +> : ^ ^^ ^^ ^^^^^ +>cb : () => Generator +> : ^^^^^^ + +f4(function* () { return null; }); +>f4(function* () { return null; }) : void +> : ^^^^ +>f4 : (cb: () => Generator) => void +> : ^ ^^ ^^ ^^^^^ +>function* () { return null; } : () => Generator +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +declare function f5(cb: () => AsyncGenerator): void; +>f5 : (cb: () => AsyncGenerator) => void +> : ^ ^^ ^^ ^^^^^ +>cb : () => AsyncGenerator +> : ^^^^^^ + +f5(async function* () { yield null; }); +>f5(async function* () { yield null; }) : void +> : ^^^^ +>f5 : (cb: () => AsyncGenerator) => void +> : ^ ^^ ^^ ^^^^^ +>async function* () { yield null; } : () => AsyncGenerator +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>yield null : any +> : ^^^ + +declare function f6(cb: () => AsyncGenerator): void; +>f6 : (cb: () => AsyncGenerator) => void +> : ^ ^^ ^^ ^^^^^ +>cb : () => AsyncGenerator +> : ^^^^^^ + +f6(async function* () { return null; }); +>f6(async function* () { return null; }) : void +> : ^^^^ +>f6 : (cb: () => AsyncGenerator) => void +> : ^ ^^ ^^ ^^^^^ +>async function* () { return null; } : () => AsyncGenerator +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + diff --git a/tests/cases/compiler/implicitAnyGenericTypeInference.ts b/tests/cases/compiler/implicitAnyGenericTypeInference.ts index 7199287a96f09..e0c5b036b29ef 100644 --- a/tests/cases/compiler/implicitAnyGenericTypeInference.ts +++ b/tests/cases/compiler/implicitAnyGenericTypeInference.ts @@ -1,4 +1,5 @@ // @noimplicitany: true +// @target: esnext interface Comparer { compareTo(x: T, y: U): U; @@ -6,4 +7,22 @@ interface Comparer { var c: Comparer; c = { compareTo: (x, y) => { return y; } }; -var r = c.compareTo(1, ''); \ No newline at end of file +var r = c.compareTo(1, ''); + +declare function f1(cb: () => T): void; +f1(() => null); + +declare function f2(cb: () => PromiseLike): void; +f2(async () => null); + +declare function f3(cb: () => Generator): void; +f3(function* () { yield null; }); + +declare function f4(cb: () => Generator): void; +f4(function* () { return null; }); + +declare function f5(cb: () => AsyncGenerator): void; +f5(async function* () { yield null; }); + +declare function f6(cb: () => AsyncGenerator): void; +f6(async function* () { return null; }); \ No newline at end of file From 9db2c215a6fa9e1a5e9120103fee6d5e801a8538 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 25 Sep 2024 17:53:15 -0400 Subject: [PATCH 2/2] Add tests for then/catch --- src/compiler/checker.ts | 4 +- ...implicitAnyGenericTypeInference.errors.txt | 12 +++++- .../implicitAnyGenericTypeInference.js | 9 +++- .../implicitAnyGenericTypeInference.symbols | 17 ++++++++ .../implicitAnyGenericTypeInference.types | 41 +++++++++++++++++++ .../implicitAnyGenericTypeInference.ts | 6 ++- 6 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 91b8556b8a267..740e7bed54dd0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25535,7 +25535,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { errorOrSuggestion(noImplicitAny, declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString); } - function reportErrorsFromWideningWithContextualSignature(declaration: FunctionLikeDeclaration, wideningKind: WideningKind) { + function shouldReportErrorsFromWideningWithContextualSignature(declaration: FunctionLikeDeclaration, wideningKind: WideningKind) { const signature = getContextualSignatureForFunctionLikeDeclaration(declaration); if (!signature) { return true; @@ -25564,7 +25564,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function reportErrorsFromWidening(declaration: Declaration, type: Type, wideningKind?: WideningKind) { addLazyDiagnostic(() => { if (noImplicitAny && getObjectFlags(type) & ObjectFlags.ContainsWideningType) { - if (!wideningKind || isFunctionLikeDeclaration(declaration) && reportErrorsFromWideningWithContextualSignature(declaration, wideningKind)) { + if (!wideningKind || isFunctionLikeDeclaration(declaration) && shouldReportErrorsFromWideningWithContextualSignature(declaration, wideningKind)) { // Report implicit any error within type if possible, otherwise report error on declaration if (!reportWideningErrorsInType(type)) { reportImplicitAny(declaration, type, wideningKind); diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt b/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt index fd2703e3f7374..cf72d48df180c 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.errors.txt @@ -4,9 +4,11 @@ implicitAnyGenericTypeInference.ts(16,4): error TS7025: Generator implicitly has implicitAnyGenericTypeInference.ts(19,4): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. implicitAnyGenericTypeInference.ts(22,4): error TS7025: Generator implicitly has yield type 'any'. Consider supplying a return type annotation. implicitAnyGenericTypeInference.ts(25,4): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. +implicitAnyGenericTypeInference.ts(28,25): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. +implicitAnyGenericTypeInference.ts(29,24): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. -==== implicitAnyGenericTypeInference.ts (6 errors) ==== +==== implicitAnyGenericTypeInference.ts (8 errors) ==== interface Comparer { compareTo(x: T, y: U): U; } @@ -43,4 +45,12 @@ implicitAnyGenericTypeInference.ts(25,4): error TS7011: Function expression, whi declare function f6(cb: () => AsyncGenerator): void; f6(async function* () { return null; }); ~~~~~ +!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. + + // https://github.com/microsoft/TypeScript/issues/44913 + Promise.resolve().catch(e => null); + ~~~~~~~~~ +!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. + Promise.resolve().then(v => null); + ~~~~~~~~~ !!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type. \ No newline at end of file diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.js b/tests/baselines/reference/implicitAnyGenericTypeInference.js index 6f98c792250e3..951e90552243c 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.js +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.js @@ -25,7 +25,11 @@ declare function f5(cb: () => AsyncGenerator): void; f5(async function* () { yield null; }); declare function f6(cb: () => AsyncGenerator): void; -f6(async function* () { return null; }); +f6(async function* () { return null; }); + +// https://github.com/microsoft/TypeScript/issues/44913 +Promise.resolve().catch(e => null); +Promise.resolve().then(v => null); //// [implicitAnyGenericTypeInference.js] var c; @@ -37,3 +41,6 @@ f3(function* () { yield null; }); f4(function* () { return null; }); f5(async function* () { yield null; }); f6(async function* () { return null; }); +// https://github.com/microsoft/TypeScript/issues/44913 +Promise.resolve().catch(e => null); +Promise.resolve().then(v => null); diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.symbols b/tests/baselines/reference/implicitAnyGenericTypeInference.symbols index 5b7f6d7dccc2b..d6579b6fd01aa 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.symbols +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.symbols @@ -91,3 +91,20 @@ declare function f6(cb: () => AsyncGenerator): void; f6(async function* () { return null; }); >f6 : Symbol(f6, Decl(implicitAnyGenericTypeInference.ts, 21, 39)) +// https://github.com/microsoft/TypeScript/issues/44913 +Promise.resolve().catch(e => null); +>Promise.resolve().catch : Symbol(Promise.catch, Decl(lib.es5.d.ts, --, --)) +>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.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, --, --)) +>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) +>catch : Symbol(Promise.catch, Decl(lib.es5.d.ts, --, --)) +>e : Symbol(e, Decl(implicitAnyGenericTypeInference.ts, 27, 24)) + +Promise.resolve().then(v => null); +>Promise.resolve().then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --)) +>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.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, --, --)) +>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) +>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --)) +>v : Symbol(v, Decl(implicitAnyGenericTypeInference.ts, 28, 23)) + diff --git a/tests/baselines/reference/implicitAnyGenericTypeInference.types b/tests/baselines/reference/implicitAnyGenericTypeInference.types index 3675c67fed393..61c257b6f9858 100644 --- a/tests/baselines/reference/implicitAnyGenericTypeInference.types +++ b/tests/baselines/reference/implicitAnyGenericTypeInference.types @@ -141,3 +141,44 @@ f6(async function* () { return null; }); >async function* () { return null; } : () => AsyncGenerator > : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// https://github.com/microsoft/TypeScript/issues/44913 +Promise.resolve().catch(e => null); +>Promise.resolve().catch(e => null) : Promise +> : ^^^^^^^^^^^^ +>Promise.resolve().catch : (onrejected?: (reason: any) => TResult | PromiseLike) => Promise +> : ^ ^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>Promise.resolve() : Promise +> : ^^^^^^^^^^^^^ +>Promise.resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^ +>Promise : PromiseConstructor +> : ^^^^^^^^^^^^^^^^^^ +>resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^ +>catch : (onrejected?: (reason: any) => TResult | PromiseLike) => Promise +> : ^ ^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>e => null : (e: any) => any +> : ^ ^^^^^^^^^^^^^ +>e : any +> : ^^^ + +Promise.resolve().then(v => null); +>Promise.resolve().then(v => null) : Promise +> : ^^^^^^^^^^^^ +>Promise.resolve().then : (onfulfilled?: (value: void) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise +> : ^ ^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>Promise.resolve() : Promise +> : ^^^^^^^^^^^^^ +>Promise.resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^ +>Promise : PromiseConstructor +> : ^^^^^^^^^^^^^^^^^^ +>resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^ +>then : (onfulfilled?: (value: void) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike) => Promise +> : ^ ^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>v => null : (v: void) => any +> : ^ ^^^^^^^^^^^^^^ +>v : void +> : ^^^^ + diff --git a/tests/cases/compiler/implicitAnyGenericTypeInference.ts b/tests/cases/compiler/implicitAnyGenericTypeInference.ts index e0c5b036b29ef..55f999742b2e8 100644 --- a/tests/cases/compiler/implicitAnyGenericTypeInference.ts +++ b/tests/cases/compiler/implicitAnyGenericTypeInference.ts @@ -25,4 +25,8 @@ declare function f5(cb: () => AsyncGenerator): void; f5(async function* () { yield null; }); declare function f6(cb: () => AsyncGenerator): void; -f6(async function* () { return null; }); \ No newline at end of file +f6(async function* () { return null; }); + +// https://github.com/microsoft/TypeScript/issues/44913 +Promise.resolve().catch(e => null); +Promise.resolve().then(v => null); \ No newline at end of file