From 3bee8ebc781851a606f30618258ad2d5bf3d4124 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 4 Mar 2023 11:23:33 +0100 Subject: [PATCH 1/7] Allow implicit return with explicit `undefined` return type --- src/compiler/checker.ts | 4 ++-- ...gReturnStatementsAndExpressions.errors.txt | 17 +++++++++----- ...nsMissingReturnStatementsAndExpressions.js | 14 ++++++++--- ...singReturnStatementsAndExpressions.symbols | 23 ++++++++++++------- ...issingReturnStatementsAndExpressions.types | 15 ++++++++---- ...nsMissingReturnStatementsAndExpressions.ts | 10 +++++--- .../fourslash/codeFixCorrectReturnValue6.ts | 4 +--- 7 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e52570b6f41b0..dd96b271194ff 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -35469,8 +35469,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const functionFlags = getFunctionFlags(func); const type = returnType && unwrapReturnType(returnType, functionFlags); - // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. - if (type && maybeTypeOfKind(type, TypeFlags.Any | TypeFlags.Void)) { + // Functions with an explicitly specified 'undefined, 'void' or 'any' return type don't need any return expressions. + if (type && maybeTypeOfKind(type, TypeFlags.Undefined | TypeFlags.Void | TypeFlags.Any)) { return; } diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt index efbdcb8290c56..965e878a6a9e3 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt @@ -1,8 +1,8 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(104,16): error TS2378: A 'get' accessor must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(126,15): error TS18050: The value 'undefined' cannot be used here. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): error TS1003: Identifier expected. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(108,16): error TS2378: A 'get' accessor must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(130,15): error TS18050: The value 'undefined' cannot be used here. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(131,5): error TS1003: Identifier expected. ==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (5 errors) ==== @@ -40,12 +40,12 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e return null; } - function f8(): void { + function f8(): any { // Fine since are typed any. return; } - function f9(): void { + function f9(): any { // Fine since we are typed any and return undefined return undefined; } @@ -112,6 +112,10 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e // Not okay; union does not contain void or any } + function f22(): undefined { + // Okay; implicitly returns undefined + } + class C { public get m1() { ~~ @@ -143,4 +147,5 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e } ~ !!! error TS1003: Identifier expected. - } \ No newline at end of file + } + \ No newline at end of file diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js index a0cfa3f9fcea3..847f8e875f6e1 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js @@ -31,12 +31,12 @@ function f7(): void { return null; } -function f8(): void { +function f8(): any { // Fine since are typed any. return; } -function f9(): void { +function f9(): any { // Fine since we are typed any and return undefined return undefined; } @@ -101,6 +101,10 @@ function f21(): number | string { // Not okay; union does not contain void or any } +function f22(): undefined { + // Okay; implicitly returns undefined +} + class C { public get m1() { // Errors; get accessors must return a value. @@ -126,7 +130,8 @@ class C { throw null; throw undefined. } -} +} + //// [functionsMissingReturnStatementsAndExpressions.js] function f1() { @@ -209,6 +214,9 @@ function f20() { function f21() { // Not okay; union does not contain void or any } +function f22() { + // Okay; implicitly returns undefined +} var C = /** @class */ (function () { function C() { } diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols index f1bde79daa863..6da04a25585ec 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols @@ -46,14 +46,14 @@ function f7(): void { return null; } -function f8(): void { +function f8(): any { >f8 : Symbol(f8, Decl(functionsMissingReturnStatementsAndExpressions.ts, 30, 1)) // Fine since are typed any. return; } -function f9(): void { +function f9(): any { >f9 : Symbol(f9, Decl(functionsMissingReturnStatementsAndExpressions.ts, 35, 1)) // Fine since we are typed any and return undefined @@ -152,37 +152,43 @@ function f21(): number | string { // Not okay; union does not contain void or any } +function f22(): undefined { +>f22 : Symbol(f22, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1)) + + // Okay; implicitly returns undefined +} + class C { ->C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1)) +>C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 104, 1)) public get m1() { ->m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 102, 9)) +>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 106, 9)) // Errors; get accessors must return a value. } public get m2() { ->m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 105, 5)) +>m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 109, 5)) // Permissible; returns undefined. return; } public get m3() { ->m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 5)) +>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 114, 5)) return "Okay, because this is a return expression."; } public get m4() { ->m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 114, 5)) +>m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 118, 5)) // Fine since this consists of a single throw statement. throw null; } public get m5() { ->m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 119, 5)) +>m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 123, 5)) // Not fine, since we can *only* consist of a single throw statement // if no return statements are present but we are a get accessor. @@ -191,3 +197,4 @@ class C { >undefined : Symbol(undefined) } } + diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types index 51a683fe691bc..7a050d0f3c3df 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types @@ -48,15 +48,15 @@ function f7(): void { >null : null } -function f8(): void { ->f8 : () => void +function f8(): any { +>f8 : () => any // Fine since are typed any. return; } -function f9(): void { ->f9 : () => void +function f9(): any { +>f9 : () => any // Fine since we are typed any and return undefined return undefined; @@ -159,6 +159,12 @@ function f21(): number | string { // Not okay; union does not contain void or any } +function f22(): undefined { +>f22 : () => undefined + + // Okay; implicitly returns undefined +} + class C { >C : C @@ -204,3 +210,4 @@ class C { } > : any } + diff --git a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts index 582940038c5e5..a2bbac39d2307 100644 --- a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts +++ b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts @@ -34,12 +34,12 @@ function f7(): void { return null; } -function f8(): void { +function f8(): any { // Fine since are typed any. return; } -function f9(): void { +function f9(): any { // Fine since we are typed any and return undefined return undefined; } @@ -104,6 +104,10 @@ function f21(): number | string { // Not okay; union does not contain void or any } +function f22(): undefined { + // Okay; implicitly returns undefined +} + class C { public get m1() { // Errors; get accessors must return a value. @@ -129,4 +133,4 @@ class C { throw null; throw undefined. } -} \ No newline at end of file +} diff --git a/tests/cases/fourslash/codeFixCorrectReturnValue6.ts b/tests/cases/fourslash/codeFixCorrectReturnValue6.ts index 693145f8c61b8..a7dbffae98cf3 100644 --- a/tests/cases/fourslash/codeFixCorrectReturnValue6.ts +++ b/tests/cases/fourslash/codeFixCorrectReturnValue6.ts @@ -4,6 +4,4 @@ //// undefined //// } -verify.codeFixAvailable([ - { description: 'Add a return statement' }, -]); +verify.not.codeFixAvailable(); From 17b43e5bb0fbba071ae2a0eeb774f28161148393 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 6 Mar 2023 22:08:22 +0100 Subject: [PATCH 2/7] Update tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt Co-authored-by: Daniel Rosenwasser --- ...unctionsMissingReturnStatementsAndExpressions.errors.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt index 965e878a6a9e3..4a17e4026744d 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt @@ -113,9 +113,13 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(131,5): e } function f22(): undefined { - // Okay; implicitly returns undefined + // Okay; return type allows implicit return of undefined } + function f23(): undefined | number { + // Okay; return type allows implicit return of undefined + } + class C { public get m1() { ~~ From b267f96c9a4e34ab9c72c7b49ccb436a0b6aef66 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 6 Mar 2023 22:55:32 +0100 Subject: [PATCH 3/7] Reflect feedback --- src/compiler/checker.ts | 4 ++-- src/compiler/diagnosticMessages.json | 4 ++-- src/services/codefixes/returnValueCorrect.ts | 4 ++-- ...gReturnStatementsAndExpressions.errors.txt | 23 +++++++++++-------- ...nsMissingReturnStatementsAndExpressions.js | 11 +++++++-- ...singReturnStatementsAndExpressions.symbols | 20 ++++++++++------ ...issingReturnStatementsAndExpressions.types | 8 ++++++- ...sMissingReturnStatementsAndExpressions2.js | 10 ++++++++ ...ingReturnStatementsAndExpressions2.symbols | 7 ++++++ ...ssingReturnStatementsAndExpressions2.types | 7 ++++++ ...nsMissingReturnStatementsAndExpressions.ts | 6 ++++- ...sMissingReturnStatementsAndExpressions2.ts | 8 +++++++ 12 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types create mode 100644 tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dd96b271194ff..540da622ac072 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -35489,9 +35489,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { else if (type && !hasExplicitReturn) { // minimal check: function has syntactic return type annotation and no explicit return statements in the body // this function does not conform to the specification. - error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); + error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value); } - else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) { + else if (type && strictNullChecks) { error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); } else if (compilerOptions.noImplicitReturns) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index fd5acd0cf83e1..13ecb6c614504 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1868,7 +1868,7 @@ "category": "Error", "code": 2354 }, - "A function whose declared type is neither 'void' nor 'any' must return a value.": { + "A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.": { "category": "Error", "code": 2355 }, @@ -6706,7 +6706,7 @@ "category": "Suggestion", "code": 80010 }, - + "Add missing 'super()' call": { "category": "Message", "code": 90001 diff --git a/src/services/codefixes/returnValueCorrect.ts b/src/services/codefixes/returnValueCorrect.ts index 1492e6377ce26..ee36ee1216e3b 100644 --- a/src/services/codefixes/returnValueCorrect.ts +++ b/src/services/codefixes/returnValueCorrect.ts @@ -51,7 +51,7 @@ const fixIdAddReturnStatement = "fixAddReturnStatement"; const fixRemoveBracesFromArrowFunctionBody = "fixRemoveBracesFromArrowFunctionBody"; const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen"; const errorCodes = [ - Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code, + Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code, Diagnostics.Type_0_is_not_assignable_to_type_1.code, Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code ]; @@ -213,7 +213,7 @@ function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number, const declaration = findAncestor(node.parent, isFunctionLikeDeclaration); switch (errorCode) { - case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code: + case Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code: if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined; return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /* isFunctionType */ false); case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code: diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt index 4a17e4026744d..1a58176ef5f37 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt @@ -1,14 +1,15 @@ -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(108,16): error TS2378: A 'get' accessor must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(130,15): error TS18050: The value 'undefined' cannot be used here. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(131,5): error TS1003: Identifier expected. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(112,16): error TS2378: A 'get' accessor must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(134,15): error TS18050: The value 'undefined' cannot be used here. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): error TS1003: Identifier expected. -==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (5 errors) ==== +==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (6 errors) ==== function f1(): string { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. // errors because there are no return statements } @@ -108,7 +109,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(131,5): e function f21(): number | string { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. // Not okay; union does not contain void or any } @@ -117,9 +118,11 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(131,5): e } function f23(): undefined | number { - // Okay; return type allows implicit return of undefined + ~~~~~~~~~~~~~~~~~~ +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. + // Error; because `undefined | number` becomes `number` without strictNullChecks. } - + class C { public get m1() { ~~ diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js index 847f8e875f6e1..4cc59397c5e06 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.js @@ -102,7 +102,11 @@ function f21(): number | string { } function f22(): undefined { - // Okay; implicitly returns undefined + // Okay; return type allows implicit return of undefined +} + +function f23(): undefined | number { + // Error; because `undefined | number` becomes `number` without strictNullChecks. } class C { @@ -215,7 +219,10 @@ function f21() { // Not okay; union does not contain void or any } function f22() { - // Okay; implicitly returns undefined + // Okay; return type allows implicit return of undefined +} +function f23() { + // Error; because `undefined | number` becomes `number` without strictNullChecks. } var C = /** @class */ (function () { function C() { diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols index 6da04a25585ec..c7048ff46c778 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.symbols @@ -155,40 +155,46 @@ function f21(): number | string { function f22(): undefined { >f22 : Symbol(f22, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1)) - // Okay; implicitly returns undefined + // Okay; return type allows implicit return of undefined +} + +function f23(): undefined | number { +>f23 : Symbol(f23, Decl(functionsMissingReturnStatementsAndExpressions.ts, 104, 1)) + + // Error; because `undefined | number` becomes `number` without strictNullChecks. } class C { ->C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 104, 1)) +>C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 108, 1)) public get m1() { ->m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 106, 9)) +>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 9)) // Errors; get accessors must return a value. } public get m2() { ->m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 109, 5)) +>m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 113, 5)) // Permissible; returns undefined. return; } public get m3() { ->m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 114, 5)) +>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 118, 5)) return "Okay, because this is a return expression."; } public get m4() { ->m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 118, 5)) +>m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 122, 5)) // Fine since this consists of a single throw statement. throw null; } public get m5() { ->m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 123, 5)) +>m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 127, 5)) // Not fine, since we can *only* consist of a single throw statement // if no return statements are present but we are a get accessor. diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types index 7a050d0f3c3df..787b43b81ae9d 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.types @@ -162,7 +162,13 @@ function f21(): number | string { function f22(): undefined { >f22 : () => undefined - // Okay; implicitly returns undefined + // Okay; return type allows implicit return of undefined +} + +function f23(): undefined | number { +>f23 : () => undefined | number + + // Error; because `undefined | number` becomes `number` without strictNullChecks. } class C { diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js new file mode 100644 index 0000000000000..613af8bc0530b --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js @@ -0,0 +1,10 @@ +//// [functionsMissingReturnStatementsAndExpressions2.ts] +function f1(): undefined | number { + // Okay; return type allows implicit return of undefined +} + + +//// [functionsMissingReturnStatementsAndExpressions2.js] +function f1() { + // Okay; return type allows implicit return of undefined +} diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols new file mode 100644 index 0000000000000..f407970032ca4 --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts === +function f1(): undefined | number { +>f1 : Symbol(f1, Decl(functionsMissingReturnStatementsAndExpressions2.ts, 0, 0)) + + // Okay; return type allows implicit return of undefined +} + diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types new file mode 100644 index 0000000000000..73c18a4631650 --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types @@ -0,0 +1,7 @@ +=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts === +function f1(): undefined | number { +>f1 : () => undefined | number + + // Okay; return type allows implicit return of undefined +} + diff --git a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts index a2bbac39d2307..cf5eb31ecad58 100644 --- a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts +++ b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts @@ -105,7 +105,11 @@ function f21(): number | string { } function f22(): undefined { - // Okay; implicitly returns undefined + // Okay; return type allows implicit return of undefined +} + +function f23(): undefined | number { + // Error; because `undefined | number` becomes `number` without strictNullChecks. } class C { diff --git a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts new file mode 100644 index 0000000000000..225a46a6d7872 --- /dev/null +++ b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts @@ -0,0 +1,8 @@ +// @allowUnreachableCode: true +// @strictNullChecks: true + +// @target: es5 + +function f1(): undefined | number { + // Okay; return type allows implicit return of undefined +} From e38cd2d6a251e76c07dc646634b491292ae7b583 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 6 Mar 2023 22:57:36 +0100 Subject: [PATCH 4/7] allowUnreachableCode is redundant --- .../compiler/functionsMissingReturnStatementsAndExpressions2.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts index 225a46a6d7872..003c03e083965 100644 --- a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts +++ b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts @@ -1,4 +1,3 @@ -// @allowUnreachableCode: true // @strictNullChecks: true // @target: es5 From 03c6cb53ce3015766117441d9cf2122daa3cbe22 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 6 Mar 2023 23:17:32 +0100 Subject: [PATCH 5/7] Revise the error --- src/compiler/checker.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/services/codefixes/returnValueCorrect.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 540da622ac072..0d1edcc3c1108 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -35489,7 +35489,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { else if (type && !hasExplicitReturn) { // minimal check: function has syntactic return type annotation and no explicit return statements in the body // this function does not conform to the specification. - error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value); + error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_undefined_is_also_allowed_with_strictNullChecks); } else if (type && strictNullChecks) { error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 13ecb6c614504..5c068ff05643e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1868,7 +1868,7 @@ "category": "Error", "code": 2354 }, - "A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.": { + "A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks.": { "category": "Error", "code": 2355 }, diff --git a/src/services/codefixes/returnValueCorrect.ts b/src/services/codefixes/returnValueCorrect.ts index ee36ee1216e3b..42248bc07a3ee 100644 --- a/src/services/codefixes/returnValueCorrect.ts +++ b/src/services/codefixes/returnValueCorrect.ts @@ -51,7 +51,7 @@ const fixIdAddReturnStatement = "fixAddReturnStatement"; const fixRemoveBracesFromArrowFunctionBody = "fixRemoveBracesFromArrowFunctionBody"; const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen"; const errorCodes = [ - Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code, + Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_undefined_is_also_allowed_with_strictNullChecks.code, Diagnostics.Type_0_is_not_assignable_to_type_1.code, Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code ]; @@ -213,7 +213,7 @@ function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number, const declaration = findAncestor(node.parent, isFunctionLikeDeclaration); switch (errorCode) { - case Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code: + case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_undefined_is_also_allowed_with_strictNullChecks.code: if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined; return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /* isFunctionType */ false); case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code: From 1eed97090141cf99988eca4faae2b71481b2dbed Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 6 Mar 2023 23:30:35 +0100 Subject: [PATCH 6/7] Update baselines --- .../reference/ParameterList5.errors.txt | 4 ++-- .../asyncFunctionDeclaration15_es5.errors.txt | 8 ++++---- .../asyncFunctionDeclaration15_es6.errors.txt | 8 ++++---- .../conflictingTypeAnnotatedVar.errors.txt | 8 ++++---- .../reference/controlFlowGenericTypes.errors.txt | 4 ++-- ...errorOnContextuallyTypedReturnType.errors.txt | 4 ++-- .../errorOnFunctionReturnType.errors.txt | 12 ++++++------ .../functionImplementationErrors.errors.txt | 4 ++-- ...singReturnStatementsAndExpressions.errors.txt | 12 ++++++------ .../reference/invalidReturnStatements.errors.txt | 16 ++++++++-------- .../jsdocFunction_missingReturn.errors.txt | 4 ++-- .../jsdocOuterTypeParameters2.errors.txt | 4 ++-- .../reference/methodInAmbientClass1.errors.txt | 4 ++-- .../reference/missingReturnStatement.errors.txt | 4 ++-- .../reference/missingReturnStatement1.errors.txt | 4 ++-- .../reference/multiLineErrors.errors.txt | 4 ++-- .../parseInvalidNonNullableTypes.errors.txt | 8 ++++---- .../parserErrorRecovery_Block3.errors.txt | 8 ++++---- .../reference/parserParameterList5.errors.txt | 4 ++-- .../reference/reachabilityChecks5.errors.txt | 4 ++-- .../reference/reachabilityChecks7.errors.txt | 4 ++-- .../reference/recursiveFunctionTypes.errors.txt | 8 ++++---- .../reference/returnTypeParameter.errors.txt | 4 ++-- .../baselines/reference/unknownType1.errors.txt | 4 ++-- 24 files changed, 74 insertions(+), 74 deletions(-) diff --git a/tests/baselines/reference/ParameterList5.errors.txt b/tests/baselines/reference/ParameterList5.errors.txt index 618ce74091527..6bea066828c56 100644 --- a/tests/baselines/reference/ParameterList5.errors.txt +++ b/tests/baselines/reference/ParameterList5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/ParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/ParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/ParameterList5.ts(1,16): error TS2369: A parameter property is only allowed in a constructor implementation. tests/cases/compiler/ParameterList5.ts(1,29): error TS2304: Cannot find name 'C'. @@ -6,7 +6,7 @@ tests/cases/compiler/ParameterList5.ts(1,29): error TS2304: Cannot find name 'C' ==== tests/cases/compiler/ParameterList5.ts (3 errors) ==== function A(): (public B) => C { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ~~~~~~~~ !!! error TS2369: A parameter property is only allowed in a constructor implementation. ~ diff --git a/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt index 7e3595ec6f4e0..c36ede56dee0b 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt +++ b/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt @@ -1,8 +1,8 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS1055: Type '{}' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. -tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(7,23): error TS1055: Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS1055: Type 'number' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. -tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(9,23): error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(10,23): error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. Construct signature return types 'Thenable' and 'PromiseLike' are incompatible. @@ -22,7 +22,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1 ~~~ !!! error TS1055: Type '{}' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. ~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. async function fn3(): any { } // error ~~~ !!! error TS1055: Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. @@ -30,7 +30,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1 ~~~~~~ !!! error TS1055: Type 'number' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. async function fn5(): PromiseLike { } // error ~~~~~~~~~~~~~~~~~ !!! error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. diff --git a/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt index 5b054a616893a..56428640489d8 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt +++ b/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt @@ -1,8 +1,8 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise<{}>'? -tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? -tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member. @@ -19,7 +19,7 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1 ~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise<{}>'? ~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. async function fn3(): any { } // error ~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? @@ -27,7 +27,7 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1 ~~~~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. async function fn5(): PromiseLike { } // error ~~~~~~~~~~~~~~~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? diff --git a/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt b/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt index 2ab30ece7a080..16ea00b0dcc48 100644 --- a/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt +++ b/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt @@ -1,10 +1,10 @@ tests/cases/compiler/conflictingTypeAnnotatedVar.ts(1,5): error TS2300: Duplicate identifier 'foo'. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,10): error TS2300: Duplicate identifier 'foo'. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,10): error TS2393: Duplicate function implementation. -tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,10): error TS2300: Duplicate identifier 'foo'. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,10): error TS2393: Duplicate function implementation. -tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== tests/cases/compiler/conflictingTypeAnnotatedVar.ts (7 errors) ==== @@ -17,11 +17,11 @@ tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A funct ~~~ !!! error TS2393: Duplicate function implementation. ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function foo(): number { } ~~~ !!! error TS2300: Duplicate identifier 'foo'. ~~~ !!! error TS2393: Duplicate function implementation. ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. \ No newline at end of file +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. \ No newline at end of file diff --git a/tests/baselines/reference/controlFlowGenericTypes.errors.txt b/tests/baselines/reference/controlFlowGenericTypes.errors.txt index 16ec73b1b4a65..47d97b9039910 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.errors.txt +++ b/tests/baselines/reference/controlFlowGenericTypes.errors.txt @@ -2,7 +2,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(49,15): error TS2 tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(55,15): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'Box'. tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'. Property 'foo' does not exist on type 'AA'. -tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'. Property 'foo' does not exist on type 'AA'. tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'. @@ -109,7 +109,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS1 const fn2 = (value: T): MyUnion => { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. value.foo; // Error ~~~ !!! error TS2339: Property 'foo' does not exist on type 'MyUnion'. diff --git a/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt b/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt index 403e79b874271..9c9c74205b76e 100644 --- a/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt +++ b/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(1,5): error TS2322: Type '() => void' is not assignable to type '() => boolean'. Type 'void' is not assignable to type 'boolean'. -tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== tests/cases/compiler/errorOnContextuallyTypedReturnType.ts (2 errors) ==== @@ -10,5 +10,5 @@ tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: !!! error TS2322: Type 'void' is not assignable to type 'boolean'. var n2: () => boolean = function ():boolean { }; // expect an error here ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. \ No newline at end of file diff --git a/tests/baselines/reference/errorOnFunctionReturnType.errors.txt b/tests/baselines/reference/errorOnFunctionReturnType.errors.txt index 90666bc7f5360..2ba353ce74d06 100644 --- a/tests/baselines/reference/errorOnFunctionReturnType.errors.txt +++ b/tests/baselines/reference/errorOnFunctionReturnType.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/jsdoc/foo.js(7,10): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/jsdoc/foo.js(7,10): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/jsdoc/foo.js(13,5): error TS2322: Type 'string' is not assignable to type 'number'. -tests/cases/conformance/jsdoc/foo.js(16,60): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/conformance/jsdoc/foo.js(21,20): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/jsdoc/foo.js(16,60): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/jsdoc/foo.js(21,20): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/jsdoc/foo.js(31,10): error TS2534: A function returning 'never' cannot have a reachable end point. tests/cases/conformance/jsdoc/foo.js(37,5): error TS2322: Type 'string' is not assignable to type 'never'. tests/cases/conformance/jsdoc/foo.js(40,56): error TS2534: A function returning 'never' cannot have a reachable end point. @@ -17,7 +17,7 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning /** @type {FunctionReturningPromise} */ function testPromise1() { ~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. console.log("Nope"); } @@ -30,14 +30,14 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning var testPromise3 = /** @type {FunctionReturningPromise} */ function() { ~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. console.log("test") } /** @type {FunctionReturningPromise} */ var testPromise4 = function() { ~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. console.log("test") } diff --git a/tests/baselines/reference/functionImplementationErrors.errors.txt b/tests/baselines/reference/functionImplementationErrors.errors.txt index cdd16bc902718..18cb52bd571af 100644 --- a/tests/baselines/reference/functionImplementationErrors.errors.txt +++ b/tests/baselines/reference/functionImplementationErrors.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/functions/functionImplementationErrors.ts(25,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/functions/functionImplementationErrors.ts(25,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/functions/functionImplementationErrors.ts(30,17): error TS2373: Parameter 'n' cannot reference identifier 'm' declared after it. tests/cases/conformance/functions/functionImplementationErrors.ts(35,17): error TS2373: Parameter 'n' cannot reference identifier 'm' declared after it. tests/cases/conformance/functions/functionImplementationErrors.ts(40,1): error TS2839: This condition will always return 'false' since JavaScript compares objects by reference, not value. @@ -31,7 +31,7 @@ tests/cases/conformance/functions/functionImplementationErrors.ts(40,1): error T // Function implemetnation with non -void return type annotation with no return function f5(): number { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. } var m; diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt index 1a58176ef5f37..df01c64504497 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt @@ -1,6 +1,6 @@ -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(112,16): error TS2378: A 'get' accessor must return a value. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(134,15): error TS18050: The value 'undefined' cannot be used here. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): error TS1003: Identifier expected. @@ -9,7 +9,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): e ==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (6 errors) ==== function f1(): string { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. // errors because there are no return statements } @@ -109,7 +109,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): e function f21(): number | string { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. // Not okay; union does not contain void or any } @@ -119,7 +119,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): e function f23(): undefined | number { ~~~~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. // Error; because `undefined | number` becomes `number` without strictNullChecks. } diff --git a/tests/baselines/reference/invalidReturnStatements.errors.txt b/tests/baselines/reference/invalidReturnStatements.errors.txt index a5d41452dbb9c..e07d08c6cd740 100644 --- a/tests/baselines/reference/invalidReturnStatements.errors.txt +++ b/tests/baselines/reference/invalidReturnStatements.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(4,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(5,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(4,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(5,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(16,22): error TS2739: Type '{ id: number; }' is missing the following properties from type 'D': name, dispose tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(18,22): error TS2741: Property 'name' is missing in type 'C' but required in type 'D'. @@ -10,16 +10,16 @@ tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(1 // all the following should be error function fn1(): number { } ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function fn2(): string { } ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function fn3(): boolean { } ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function fn4(): Date { } ~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function fn7(): any { } // should be valid: any includes void interface I { id: number } diff --git a/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt b/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt index 646e03fcd6f57..8b8f27f47111d 100644 --- a/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt +++ b/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt @@ -1,9 +1,9 @@ -/a.js(1,24): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +/a.js(1,24): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== /a.js (1 errors) ==== /** @type {function(): number} */ ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function f() {} \ No newline at end of file diff --git a/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt b/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt index a0a53680b66db..6c3b1a2113598 100644 --- a/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt +++ b/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt @@ -1,6 +1,6 @@ error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js' because it would overwrite input file. Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. -tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(1,14): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(1,14): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(7,35): error TS2339: Property 'foo' does not exist on type 'Bar'. @@ -9,7 +9,7 @@ tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(7,35): error TS2339: ==== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js (2 errors) ==== /** @return {T} */ ~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. const dedupingMixin = function(mixin) {}; /** @template T */ diff --git a/tests/baselines/reference/methodInAmbientClass1.errors.txt b/tests/baselines/reference/methodInAmbientClass1.errors.txt index c931cc0101c81..6879c4d95d624 100644 --- a/tests/baselines/reference/methodInAmbientClass1.errors.txt +++ b/tests/baselines/reference/methodInAmbientClass1.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/methodInAmbientClass1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/methodInAmbientClass1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/methodInAmbientClass1.ts(2,20): error TS1183: An implementation cannot be declared in ambient contexts. @@ -6,7 +6,7 @@ tests/cases/compiler/methodInAmbientClass1.ts(2,20): error TS1183: An implementa declare class Foo { fn(): boolean { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ~ !!! error TS1183: An implementation cannot be declared in ambient contexts. } diff --git a/tests/baselines/reference/missingReturnStatement.errors.txt b/tests/baselines/reference/missingReturnStatement.errors.txt index 8926d22be3d42..76054509bc616 100644 --- a/tests/baselines/reference/missingReturnStatement.errors.txt +++ b/tests/baselines/reference/missingReturnStatement.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/missingReturnStatement.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/missingReturnStatement.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== tests/cases/compiler/missingReturnStatement.ts (1 errors) ==== @@ -6,7 +6,7 @@ tests/cases/compiler/missingReturnStatement.ts(3,22): error TS2355: A function w export class Bug { public foo():string { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. } } } diff --git a/tests/baselines/reference/missingReturnStatement1.errors.txt b/tests/baselines/reference/missingReturnStatement1.errors.txt index 9d3cf78411edf..7f982ce1a4407 100644 --- a/tests/baselines/reference/missingReturnStatement1.errors.txt +++ b/tests/baselines/reference/missingReturnStatement1.errors.txt @@ -1,11 +1,11 @@ -tests/cases/compiler/missingReturnStatement1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/missingReturnStatement1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== tests/cases/compiler/missingReturnStatement1.ts (1 errors) ==== class Foo { foo(): number { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. //return 4; } } diff --git a/tests/baselines/reference/multiLineErrors.errors.txt b/tests/baselines/reference/multiLineErrors.errors.txt index 851425baef434..3a88b4686f7ef 100644 --- a/tests/baselines/reference/multiLineErrors.errors.txt +++ b/tests/baselines/reference/multiLineErrors.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/multiLineErrors.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/multiLineErrors.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/multiLineErrors.ts(21,1): error TS2322: Type 'A2' is not assignable to type 'A1'. The types of 'x.y' are incompatible between these types. Type 'string' is not assignable to type 'number'. @@ -15,7 +15,7 @@ tests/cases/compiler/multiLineErrors.ts(21,1): error TS2322: Type 'A2' is not as ~~~~~~~~~~~~~~ } ~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. { var x = 4; var y = 10; diff --git a/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt b/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt index a5b8dbef915bd..89d49919f5a96 100644 --- a/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt +++ b/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt @@ -4,9 +4,9 @@ tests/cases/compiler/parseInvalidNonNullableTypes.ts(9,16): error TS17019: '!' a tests/cases/compiler/parseInvalidNonNullableTypes.ts(10,16): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(12,16): error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(13,16): error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'number'? -tests/cases/compiler/parseInvalidNonNullableTypes.ts(15,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/parseInvalidNonNullableTypes.ts(15,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/parseInvalidNonNullableTypes.ts(15,16): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'string'? -tests/cases/compiler/parseInvalidNonNullableTypes.ts(16,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/parseInvalidNonNullableTypes.ts(16,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/parseInvalidNonNullableTypes.ts(16,16): error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(18,16): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'any'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(19,10): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number'? @@ -43,12 +43,12 @@ tests/cases/compiler/parseInvalidNonNullableTypes.ts(22,10): error TS17020: '!' function f7(): string! {} ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ~~~~~~~ !!! error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'string'? function f8(): !string {} ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ~~~~~~~ !!! error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string'? diff --git a/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt b/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt index e6033d05215d9..7739a7fa918d9 100644 --- a/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt +++ b/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt @@ -1,18 +1,18 @@ -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(2,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(2,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,5): error TS1128: Declaration or statement expected. -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts (3 errors) ==== class C { private a(): boolean { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. private b(): boolean { ~~~~~~~ !!! error TS1128: Declaration or statement expected. ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. } } \ No newline at end of file diff --git a/tests/baselines/reference/parserParameterList5.errors.txt b/tests/baselines/reference/parserParameterList5.errors.txt index 5f0899fa1a2a6..cebbdd296e5da 100644 --- a/tests/baselines/reference/parserParameterList5.errors.txt +++ b/tests/baselines/reference/parserParameterList5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,16): error TS2369: A parameter property is only allowed in a constructor implementation. tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,29): error TS2304: Cannot find name 'C'. @@ -6,7 +6,7 @@ tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.t ==== tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts (3 errors) ==== function A(): (public B) => C { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ~~~~~~~~ !!! error TS2369: A parameter property is only allowed in a constructor implementation. ~ diff --git a/tests/baselines/reference/reachabilityChecks5.errors.txt b/tests/baselines/reference/reachabilityChecks5.errors.txt index f8c7e6f4dbc1a..e565fb1683569 100644 --- a/tests/baselines/reference/reachabilityChecks5.errors.txt +++ b/tests/baselines/reference/reachabilityChecks5.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/reachabilityChecks5.ts(5,17): error TS7030: Not all code paths return a value. -tests/cases/compiler/reachabilityChecks5.ts(18,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/reachabilityChecks5.ts(18,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/reachabilityChecks5.ts(30,17): error TS7030: Not all code paths return a value. tests/cases/compiler/reachabilityChecks5.ts(40,17): error TS7030: Not all code paths return a value. tests/cases/compiler/reachabilityChecks5.ts(51,17): error TS7030: Not all code paths return a value. @@ -33,7 +33,7 @@ tests/cases/compiler/reachabilityChecks5.ts(122,13): error TS7027: Unreachable c function f3(x): number { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. while (x) { throw new Error(); } diff --git a/tests/baselines/reference/reachabilityChecks7.errors.txt b/tests/baselines/reference/reachabilityChecks7.errors.txt index 9ac2d0c1ad8ae..3bf25a5ab049b 100644 --- a/tests/baselines/reference/reachabilityChecks7.errors.txt +++ b/tests/baselines/reference/reachabilityChecks7.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/reachabilityChecks7.ts(13,16): error TS7030: Not all code paths return a value. -tests/cases/compiler/reachabilityChecks7.ts(17,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/reachabilityChecks7.ts(17,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. ==== tests/cases/compiler/reachabilityChecks7.ts (2 errors) ==== @@ -23,7 +23,7 @@ tests/cases/compiler/reachabilityChecks7.ts(17,22): error TS2355: A function who async function f4(): Promise { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. } diff --git a/tests/baselines/reference/recursiveFunctionTypes.errors.txt b/tests/baselines/reference/recursiveFunctionTypes.errors.txt index 53c9b34af7287..70c731217650e 100644 --- a/tests/baselines/reference/recursiveFunctionTypes.errors.txt +++ b/tests/baselines/reference/recursiveFunctionTypes.errors.txt @@ -2,8 +2,8 @@ tests/cases/compiler/recursiveFunctionTypes.ts(1,28): error TS2322: Type 'number tests/cases/compiler/recursiveFunctionTypes.ts(3,5): error TS2322: Type '() => typeof fn' is not assignable to type 'number'. tests/cases/compiler/recursiveFunctionTypes.ts(4,5): error TS2322: Type '() => typeof fn' is not assignable to type '() => number'. Type '() => typeof fn' is not assignable to type 'number'. -tests/cases/compiler/recursiveFunctionTypes.ts(11,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/compiler/recursiveFunctionTypes.ts(12,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/recursiveFunctionTypes.ts(11,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/recursiveFunctionTypes.ts(12,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/recursiveFunctionTypes.ts(17,5): error TS2322: Type '() => I' is not assignable to type 'number'. tests/cases/compiler/recursiveFunctionTypes.ts(22,5): error TS2345: Argument of type 'number' is not assignable to parameter of type '(t: typeof g) => void'. tests/cases/compiler/recursiveFunctionTypes.ts(25,1): error TS2322: Type 'number' is not assignable to type '() => any'. @@ -40,10 +40,10 @@ tests/cases/compiler/recursiveFunctionTypes.ts(43,4): error TS2769: No overload function f2(): typeof g2 { } ~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function g2(): typeof f2 { } ~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. interface I { } function f3(): I { return f3; } diff --git a/tests/baselines/reference/returnTypeParameter.errors.txt b/tests/baselines/reference/returnTypeParameter.errors.txt index 24b9e725de8fd..9f7cd47696863 100644 --- a/tests/baselines/reference/returnTypeParameter.errors.txt +++ b/tests/baselines/reference/returnTypeParameter.errors.txt @@ -1,11 +1,11 @@ -tests/cases/compiler/returnTypeParameter.ts(1,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/returnTypeParameter.ts(1,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/compiler/returnTypeParameter.ts(2,34): error TS2693: 'T' only refers to a type, but is being used as a value here. ==== tests/cases/compiler/returnTypeParameter.ts (2 errors) ==== function f(a: T): T { } // error, no return statement ~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. function f2(a: T): T { return T; } // bug was that this satisfied the return statement requirement ~ !!! error TS2693: 'T' only refers to a type, but is being used as a value here. \ No newline at end of file diff --git a/tests/baselines/reference/unknownType1.errors.txt b/tests/baselines/reference/unknownType1.errors.txt index f42cc88fa7c62..aef2f0e58e65c 100644 --- a/tests/baselines/reference/unknownType1.errors.txt +++ b/tests/baselines/reference/unknownType1.errors.txt @@ -20,7 +20,7 @@ tests/cases/conformance/types/unknown/unknownType1.ts(128,5): error TS2322: Type tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type 'number' is not assignable to type '{ [x: string]: unknown; }'. tests/cases/conformance/types/unknown/unknownType1.ts(143,29): error TS2698: Spread types may only be created from object types. tests/cases/conformance/types/unknown/unknownType1.ts(144,29): error TS2698: Spread types may only be created from object types. -tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. tests/cases/conformance/types/unknown/unknownType1.ts(156,14): error TS2700: Rest types may only be created from object types. tests/cases/conformance/types/unknown/unknownType1.ts(162,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. tests/cases/conformance/types/unknown/unknownType1.ts(170,9): error TS2322: Type 'T' is not assignable to type '{}'. @@ -223,7 +223,7 @@ tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type function f27(): unknown { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. } // Rest type cannot be created from unknown From 5632662911be3c421f4e8861b00a37912bbfbcb5 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Fri, 17 Mar 2023 19:09:45 +0100 Subject: [PATCH 7/7] Split error messages --- src/compiler/checker.ts | 7 ++++- src/compiler/diagnosticMessages.json | 6 +++- src/services/codefixes/returnValueCorrect.ts | 6 ++-- .../reference/ParameterList5.errors.txt | 4 +-- .../asyncFunctionDeclaration15_es5.errors.txt | 8 ++--- .../asyncFunctionDeclaration15_es6.errors.txt | 8 ++--- .../conflictingTypeAnnotatedVar.errors.txt | 8 ++--- .../controlFlowGenericTypes.errors.txt | 4 +-- ...orOnContextuallyTypedReturnType.errors.txt | 4 +-- .../errorOnFunctionReturnType.errors.txt | 12 +++---- .../functionImplementationErrors.errors.txt | 4 +-- ...gReturnStatementsAndExpressions.errors.txt | 12 +++---- ...sMissingReturnStatementsAndExpressions2.js | 10 ------ ...ingReturnStatementsAndExpressions2.symbols | 7 ----- ...ssingReturnStatementsAndExpressions2.types | 7 ----- ...sAndExpressionsStrictNullChecks.errors.txt | 25 +++++++++++++++ ...tatementsAndExpressionsStrictNullChecks.js | 31 +++++++++++++++++++ ...entsAndExpressionsStrictNullChecks.symbols | 27 ++++++++++++++++ ...ementsAndExpressionsStrictNullChecks.types | 25 +++++++++++++++ .../invalidReturnStatements.errors.txt | 16 +++++----- .../jsdocFunction_missingReturn.errors.txt | 4 +-- .../jsdocOuterTypeParameters2.errors.txt | 4 +-- .../methodInAmbientClass1.errors.txt | 4 +-- .../missingReturnStatement.errors.txt | 4 +-- .../missingReturnStatement1.errors.txt | 4 +-- .../reference/multiLineErrors.errors.txt | 4 +-- .../parseInvalidNonNullableTypes.errors.txt | 8 ++--- .../parserErrorRecovery_Block3.errors.txt | 8 ++--- .../reference/parserParameterList5.errors.txt | 4 +-- .../reference/reachabilityChecks5.errors.txt | 4 +-- .../reference/reachabilityChecks7.errors.txt | 4 +-- .../recursiveFunctionTypes.errors.txt | 8 ++--- .../reference/returnTypeParameter.errors.txt | 4 +-- .../reference/unknownType1.errors.txt | 4 +-- ...sMissingReturnStatementsAndExpressions2.ts | 7 ----- ...tatementsAndExpressionsStrictNullChecks.ts | 19 ++++++++++++ 36 files changed, 216 insertions(+), 109 deletions(-) delete mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js delete mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols delete mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.errors.txt create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.symbols create mode 100644 tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.types delete mode 100644 tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts create mode 100644 tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f86ded12b8c9c..d3367776dec35 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -35560,7 +35560,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { else if (type && !hasExplicitReturn) { // minimal check: function has syntactic return type annotation and no explicit return statements in the body // this function does not conform to the specification. - error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_undefined_is_also_allowed_with_strictNullChecks); + if (strictNullChecks) { + error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value); + } + else { + error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); + } } else if (type && strictNullChecks) { error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f6b01caa094e0..4562b7e738e3e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1872,7 +1872,7 @@ "category": "Error", "code": 2354 }, - "A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks.": { + "A function whose declared type is neither 'void' nor 'any' must return a value.": { "category": "Error", "code": 2355 }, @@ -3611,6 +3611,10 @@ "category": "Error", "code": 2846 }, + "A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.": { + "category": "Error", + "code": 2847 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/src/services/codefixes/returnValueCorrect.ts b/src/services/codefixes/returnValueCorrect.ts index 42248bc07a3ee..d3d0945959483 100644 --- a/src/services/codefixes/returnValueCorrect.ts +++ b/src/services/codefixes/returnValueCorrect.ts @@ -51,7 +51,8 @@ const fixIdAddReturnStatement = "fixAddReturnStatement"; const fixRemoveBracesFromArrowFunctionBody = "fixRemoveBracesFromArrowFunctionBody"; const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen"; const errorCodes = [ - Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_undefined_is_also_allowed_with_strictNullChecks.code, + Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code, + Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code, Diagnostics.Type_0_is_not_assignable_to_type_1.code, Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code ]; @@ -213,7 +214,8 @@ function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number, const declaration = findAncestor(node.parent, isFunctionLikeDeclaration); switch (errorCode) { - case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_undefined_is_also_allowed_with_strictNullChecks.code: + case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code: + case Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code: if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined; return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /* isFunctionType */ false); case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code: diff --git a/tests/baselines/reference/ParameterList5.errors.txt b/tests/baselines/reference/ParameterList5.errors.txt index 6bea066828c56..618ce74091527 100644 --- a/tests/baselines/reference/ParameterList5.errors.txt +++ b/tests/baselines/reference/ParameterList5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/ParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/ParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/ParameterList5.ts(1,16): error TS2369: A parameter property is only allowed in a constructor implementation. tests/cases/compiler/ParameterList5.ts(1,29): error TS2304: Cannot find name 'C'. @@ -6,7 +6,7 @@ tests/cases/compiler/ParameterList5.ts(1,29): error TS2304: Cannot find name 'C' ==== tests/cases/compiler/ParameterList5.ts (3 errors) ==== function A(): (public B) => C { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ~~~~~~~~ !!! error TS2369: A parameter property is only allowed in a constructor implementation. ~ diff --git a/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt index c36ede56dee0b..7e3595ec6f4e0 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt +++ b/tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt @@ -1,8 +1,8 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS1055: Type '{}' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. -tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(7,23): error TS1055: Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS1055: Type 'number' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. -tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(9,23): error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(10,23): error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. Construct signature return types 'Thenable' and 'PromiseLike' are incompatible. @@ -22,7 +22,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1 ~~~ !!! error TS1055: Type '{}' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. ~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. async function fn3(): any { } // error ~~~ !!! error TS1055: Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. @@ -30,7 +30,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1 ~~~~~~ !!! error TS1055: Type 'number' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. async function fn5(): PromiseLike { } // error ~~~~~~~~~~~~~~~~~ !!! error TS1055: Type 'PromiseLike' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. diff --git a/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt index 56428640489d8..5b054a616893a 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt +++ b/tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt @@ -1,8 +1,8 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise<{}>'? -tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? -tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member. @@ -19,7 +19,7 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1 ~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise<{}>'? ~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. async function fn3(): any { } // error ~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? @@ -27,7 +27,7 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1 ~~~~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. async function fn5(): PromiseLike { } // error ~~~~~~~~~~~~~~~~~ !!! error TS1064: The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise'? diff --git a/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt b/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt index 16ea00b0dcc48..2ab30ece7a080 100644 --- a/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt +++ b/tests/baselines/reference/conflictingTypeAnnotatedVar.errors.txt @@ -1,10 +1,10 @@ tests/cases/compiler/conflictingTypeAnnotatedVar.ts(1,5): error TS2300: Duplicate identifier 'foo'. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,10): error TS2300: Duplicate identifier 'foo'. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,10): error TS2393: Duplicate function implementation. -tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/conflictingTypeAnnotatedVar.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,10): error TS2300: Duplicate identifier 'foo'. tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,10): error TS2393: Duplicate function implementation. -tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ==== tests/cases/compiler/conflictingTypeAnnotatedVar.ts (7 errors) ==== @@ -17,11 +17,11 @@ tests/cases/compiler/conflictingTypeAnnotatedVar.ts(3,17): error TS2355: A funct ~~~ !!! error TS2393: Duplicate function implementation. ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function foo(): number { } ~~~ !!! error TS2300: Duplicate identifier 'foo'. ~~~ !!! error TS2393: Duplicate function implementation. ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. \ No newline at end of file +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. \ No newline at end of file diff --git a/tests/baselines/reference/controlFlowGenericTypes.errors.txt b/tests/baselines/reference/controlFlowGenericTypes.errors.txt index 47d97b9039910..4686e36af6091 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.errors.txt +++ b/tests/baselines/reference/controlFlowGenericTypes.errors.txt @@ -2,7 +2,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(49,15): error TS2 tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(55,15): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'Box'. tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'. Property 'foo' does not exist on type 'AA'. -tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'. Property 'foo' does not exist on type 'AA'. tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'. @@ -109,7 +109,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS1 const fn2 = (value: T): MyUnion => { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. value.foo; // Error ~~~ !!! error TS2339: Property 'foo' does not exist on type 'MyUnion'. diff --git a/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt b/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt index 9c9c74205b76e..403e79b874271 100644 --- a/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt +++ b/tests/baselines/reference/errorOnContextuallyTypedReturnType.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(1,5): error TS2322: Type '() => void' is not assignable to type '() => boolean'. Type 'void' is not assignable to type 'boolean'. -tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ==== tests/cases/compiler/errorOnContextuallyTypedReturnType.ts (2 errors) ==== @@ -10,5 +10,5 @@ tests/cases/compiler/errorOnContextuallyTypedReturnType.ts(2,37): error TS2355: !!! error TS2322: Type 'void' is not assignable to type 'boolean'. var n2: () => boolean = function ():boolean { }; // expect an error here ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. \ No newline at end of file diff --git a/tests/baselines/reference/errorOnFunctionReturnType.errors.txt b/tests/baselines/reference/errorOnFunctionReturnType.errors.txt index 73f10818b1682..4fa30fe7edf7f 100644 --- a/tests/baselines/reference/errorOnFunctionReturnType.errors.txt +++ b/tests/baselines/reference/errorOnFunctionReturnType.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/jsdoc/foo.js(7,10): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/jsdoc/foo.js(7,10): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/jsdoc/foo.js(13,5): error TS2322: Type 'string' is not assignable to type 'number'. -tests/cases/conformance/jsdoc/foo.js(16,60): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/conformance/jsdoc/foo.js(21,20): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/jsdoc/foo.js(16,60): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/jsdoc/foo.js(21,20): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/jsdoc/foo.js(31,10): error TS2534: A function returning 'never' cannot have a reachable end point. tests/cases/conformance/jsdoc/foo.js(37,5): error TS2322: Type 'string' is not assignable to type 'never'. tests/cases/conformance/jsdoc/foo.js(40,56): error TS2534: A function returning 'never' cannot have a reachable end point. @@ -17,7 +17,7 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning /** @type {FunctionReturningPromise} */ function testPromise1() { ~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. console.log("Nope"); } @@ -30,14 +30,14 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning var testPromise3 = /** @type {FunctionReturningPromise} */ function() { ~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. console.log("test") } /** @type {FunctionReturningPromise} */ var testPromise4 = function() { ~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. console.log("test") } diff --git a/tests/baselines/reference/functionImplementationErrors.errors.txt b/tests/baselines/reference/functionImplementationErrors.errors.txt index 18cb52bd571af..cdd16bc902718 100644 --- a/tests/baselines/reference/functionImplementationErrors.errors.txt +++ b/tests/baselines/reference/functionImplementationErrors.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/functions/functionImplementationErrors.ts(25,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/functions/functionImplementationErrors.ts(25,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/functions/functionImplementationErrors.ts(30,17): error TS2373: Parameter 'n' cannot reference identifier 'm' declared after it. tests/cases/conformance/functions/functionImplementationErrors.ts(35,17): error TS2373: Parameter 'n' cannot reference identifier 'm' declared after it. tests/cases/conformance/functions/functionImplementationErrors.ts(40,1): error TS2839: This condition will always return 'false' since JavaScript compares objects by reference, not value. @@ -31,7 +31,7 @@ tests/cases/conformance/functions/functionImplementationErrors.ts(40,1): error T // Function implemetnation with non -void return type annotation with no return function f5(): number { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. } var m; diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt index df01c64504497..f4466e0b869e4 100644 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions.errors.txt @@ -1,6 +1,6 @@ -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(112,16): error TS2378: A 'get' accessor must return a value. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(134,15): error TS18050: The value 'undefined' cannot be used here. tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): error TS1003: Identifier expected. @@ -9,7 +9,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): e ==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (6 errors) ==== function f1(): string { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. // errors because there are no return statements } @@ -109,7 +109,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): e function f21(): number | string { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. // Not okay; union does not contain void or any } @@ -119,7 +119,7 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): e function f23(): undefined | number { ~~~~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. // Error; because `undefined | number` becomes `number` without strictNullChecks. } diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js deleted file mode 100644 index 613af8bc0530b..0000000000000 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.js +++ /dev/null @@ -1,10 +0,0 @@ -//// [functionsMissingReturnStatementsAndExpressions2.ts] -function f1(): undefined | number { - // Okay; return type allows implicit return of undefined -} - - -//// [functionsMissingReturnStatementsAndExpressions2.js] -function f1() { - // Okay; return type allows implicit return of undefined -} diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols deleted file mode 100644 index f407970032ca4..0000000000000 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.symbols +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts === -function f1(): undefined | number { ->f1 : Symbol(f1, Decl(functionsMissingReturnStatementsAndExpressions2.ts, 0, 0)) - - // Okay; return type allows implicit return of undefined -} - diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types deleted file mode 100644 index 73c18a4631650..0000000000000 --- a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressions2.types +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts === -function f1(): undefined | number { ->f1 : () => undefined | number - - // Okay; return type allows implicit return of undefined -} - diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.errors.txt b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.errors.txt new file mode 100644 index 0000000000000..a570470cf2ea0 --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(5,16): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(13,22): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. + + +==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts (2 errors) ==== + function f1(): undefined | number { + // Okay; return type allows implicit return of undefined + } + + function f2(): number { + ~~~~~~ +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. + // Error; return type does not include undefined + } + + async function f3(): Promise { + // Okay; return type allows implicit return of undefined + } + + async function f4(): Promise { + ~~~~~~~~~~~~~~~ +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. + // Error; return type does not include undefined + } + \ No newline at end of file diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js new file mode 100644 index 0000000000000..3ca9a8cc8353d --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js @@ -0,0 +1,31 @@ +//// [functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts] +function f1(): undefined | number { + // Okay; return type allows implicit return of undefined +} + +function f2(): number { + // Error; return type does not include undefined +} + +async function f3(): Promise { + // Okay; return type allows implicit return of undefined +} + +async function f4(): Promise { + // Error; return type does not include undefined +} + + +//// [functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js] +function f1() { + // Okay; return type allows implicit return of undefined +} +function f2() { + // Error; return type does not include undefined +} +async function f3() { + // Okay; return type allows implicit return of undefined +} +async function f4() { + // Error; return type does not include undefined +} diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.symbols b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.symbols new file mode 100644 index 0000000000000..befceb738b18a --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.symbols @@ -0,0 +1,27 @@ +=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts === +function f1(): undefined | number { +>f1 : Symbol(f1, Decl(functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts, 0, 0)) + + // Okay; return type allows implicit return of undefined +} + +function f2(): number { +>f2 : Symbol(f2, Decl(functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts, 2, 1)) + + // Error; return type does not include undefined +} + +async function f3(): Promise { +>f3 : Symbol(f3, Decl(functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts, 6, 1)) +>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, --, --)) + + // Okay; return type allows implicit return of undefined +} + +async function f4(): Promise { +>f4 : Symbol(f4, Decl(functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts, 10, 1)) +>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, --, --)) + + // Error; return type does not include undefined +} + diff --git a/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.types b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.types new file mode 100644 index 0000000000000..beb7ae82be089 --- /dev/null +++ b/tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts === +function f1(): undefined | number { +>f1 : () => undefined | number + + // Okay; return type allows implicit return of undefined +} + +function f2(): number { +>f2 : () => number + + // Error; return type does not include undefined +} + +async function f3(): Promise { +>f3 : () => Promise + + // Okay; return type allows implicit return of undefined +} + +async function f4(): Promise { +>f4 : () => Promise + + // Error; return type does not include undefined +} + diff --git a/tests/baselines/reference/invalidReturnStatements.errors.txt b/tests/baselines/reference/invalidReturnStatements.errors.txt index 95a31e20b7705..fe34106169bf6 100644 --- a/tests/baselines/reference/invalidReturnStatements.errors.txt +++ b/tests/baselines/reference/invalidReturnStatements.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(4,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(5,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(2,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(3,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(4,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(5,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(16,22): error TS2739: Type '{ id: number; }' is missing the following properties from type 'D': name, dispose tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(18,22): error TS2741: Property 'name' is missing in type 'C' but required in type 'D'. @@ -10,16 +10,16 @@ tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(1 // all the following should be error function fn1(): number { } ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function fn2(): string { } ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function fn3(): boolean { } ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function fn4(): Date { } ~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function fn7(): any { } // should be valid: any includes void interface I { id: number } diff --git a/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt b/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt index 8b8f27f47111d..8e102d797ec7c 100644 --- a/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt +++ b/tests/baselines/reference/jsdocFunction_missingReturn.errors.txt @@ -1,9 +1,9 @@ -/a.js(1,24): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +/a.js(1,24): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. ==== /a.js (1 errors) ==== /** @type {function(): number} */ ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. function f() {} \ No newline at end of file diff --git a/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt b/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt index 6c3b1a2113598..a0a53680b66db 100644 --- a/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt +++ b/tests/baselines/reference/jsdocOuterTypeParameters2.errors.txt @@ -1,6 +1,6 @@ error TS5055: Cannot write file 'tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js' because it would overwrite input file. Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. -tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(1,14): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(1,14): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(7,35): error TS2339: Property 'foo' does not exist on type 'Bar'. @@ -9,7 +9,7 @@ tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js(7,35): error TS2339: ==== tests/cases/conformance/jsdoc/jsdocOuterTypeParameters1.js (2 errors) ==== /** @return {T} */ ~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. const dedupingMixin = function(mixin) {}; /** @template T */ diff --git a/tests/baselines/reference/methodInAmbientClass1.errors.txt b/tests/baselines/reference/methodInAmbientClass1.errors.txt index 6879c4d95d624..c931cc0101c81 100644 --- a/tests/baselines/reference/methodInAmbientClass1.errors.txt +++ b/tests/baselines/reference/methodInAmbientClass1.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/methodInAmbientClass1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/methodInAmbientClass1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/methodInAmbientClass1.ts(2,20): error TS1183: An implementation cannot be declared in ambient contexts. @@ -6,7 +6,7 @@ tests/cases/compiler/methodInAmbientClass1.ts(2,20): error TS1183: An implementa declare class Foo { fn(): boolean { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ~ !!! error TS1183: An implementation cannot be declared in ambient contexts. } diff --git a/tests/baselines/reference/missingReturnStatement.errors.txt b/tests/baselines/reference/missingReturnStatement.errors.txt index 76054509bc616..8926d22be3d42 100644 --- a/tests/baselines/reference/missingReturnStatement.errors.txt +++ b/tests/baselines/reference/missingReturnStatement.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/missingReturnStatement.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/missingReturnStatement.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ==== tests/cases/compiler/missingReturnStatement.ts (1 errors) ==== @@ -6,7 +6,7 @@ tests/cases/compiler/missingReturnStatement.ts(3,22): error TS2355: A function w export class Bug { public foo():string { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. } } } diff --git a/tests/baselines/reference/missingReturnStatement1.errors.txt b/tests/baselines/reference/missingReturnStatement1.errors.txt index 7f982ce1a4407..9d3cf78411edf 100644 --- a/tests/baselines/reference/missingReturnStatement1.errors.txt +++ b/tests/baselines/reference/missingReturnStatement1.errors.txt @@ -1,11 +1,11 @@ -tests/cases/compiler/missingReturnStatement1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/missingReturnStatement1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ==== tests/cases/compiler/missingReturnStatement1.ts (1 errors) ==== class Foo { foo(): number { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. //return 4; } } diff --git a/tests/baselines/reference/multiLineErrors.errors.txt b/tests/baselines/reference/multiLineErrors.errors.txt index 3a88b4686f7ef..851425baef434 100644 --- a/tests/baselines/reference/multiLineErrors.errors.txt +++ b/tests/baselines/reference/multiLineErrors.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/multiLineErrors.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/multiLineErrors.ts(3,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/multiLineErrors.ts(21,1): error TS2322: Type 'A2' is not assignable to type 'A1'. The types of 'x.y' are incompatible between these types. Type 'string' is not assignable to type 'number'. @@ -15,7 +15,7 @@ tests/cases/compiler/multiLineErrors.ts(21,1): error TS2322: Type 'A2' is not as ~~~~~~~~~~~~~~ } ~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. { var x = 4; var y = 10; diff --git a/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt b/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt index 89d49919f5a96..378419d288544 100644 --- a/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt +++ b/tests/baselines/reference/parseInvalidNonNullableTypes.errors.txt @@ -4,9 +4,9 @@ tests/cases/compiler/parseInvalidNonNullableTypes.ts(9,16): error TS17019: '!' a tests/cases/compiler/parseInvalidNonNullableTypes.ts(10,16): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(12,16): error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(13,16): error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'number'? -tests/cases/compiler/parseInvalidNonNullableTypes.ts(15,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/parseInvalidNonNullableTypes.ts(15,16): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. tests/cases/compiler/parseInvalidNonNullableTypes.ts(15,16): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'string'? -tests/cases/compiler/parseInvalidNonNullableTypes.ts(16,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/parseInvalidNonNullableTypes.ts(16,16): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. tests/cases/compiler/parseInvalidNonNullableTypes.ts(16,16): error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(18,16): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'any'? tests/cases/compiler/parseInvalidNonNullableTypes.ts(19,10): error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number'? @@ -43,12 +43,12 @@ tests/cases/compiler/parseInvalidNonNullableTypes.ts(22,10): error TS17020: '!' function f7(): string! {} ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. ~~~~~~~ !!! error TS17019: '!' at the end of a type is not valid TypeScript syntax. Did you mean to write 'string'? function f8(): !string {} ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. ~~~~~~~ !!! error TS17020: '!' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string'? diff --git a/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt b/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt index 7739a7fa918d9..e6033d05215d9 100644 --- a/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt +++ b/tests/baselines/reference/parserErrorRecovery_Block3.errors.txt @@ -1,18 +1,18 @@ -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(2,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(2,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,5): error TS1128: Declaration or statement expected. -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts(4,18): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block3.ts (3 errors) ==== class C { private a(): boolean { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. private b(): boolean { ~~~~~~~ !!! error TS1128: Declaration or statement expected. ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. } } \ No newline at end of file diff --git a/tests/baselines/reference/parserParameterList5.errors.txt b/tests/baselines/reference/parserParameterList5.errors.txt index cebbdd296e5da..5f0899fa1a2a6 100644 --- a/tests/baselines/reference/parserParameterList5.errors.txt +++ b/tests/baselines/reference/parserParameterList5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,16): error TS2369: A parameter property is only allowed in a constructor implementation. tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts(1,29): error TS2304: Cannot find name 'C'. @@ -6,7 +6,7 @@ tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.t ==== tests/cases/conformance/parser/ecmascript5/ParameterLists/parserParameterList5.ts (3 errors) ==== function A(): (public B) => C { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ~~~~~~~~ !!! error TS2369: A parameter property is only allowed in a constructor implementation. ~ diff --git a/tests/baselines/reference/reachabilityChecks5.errors.txt b/tests/baselines/reference/reachabilityChecks5.errors.txt index e565fb1683569..f8c7e6f4dbc1a 100644 --- a/tests/baselines/reference/reachabilityChecks5.errors.txt +++ b/tests/baselines/reference/reachabilityChecks5.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/reachabilityChecks5.ts(5,17): error TS7030: Not all code paths return a value. -tests/cases/compiler/reachabilityChecks5.ts(18,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/reachabilityChecks5.ts(18,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/reachabilityChecks5.ts(30,17): error TS7030: Not all code paths return a value. tests/cases/compiler/reachabilityChecks5.ts(40,17): error TS7030: Not all code paths return a value. tests/cases/compiler/reachabilityChecks5.ts(51,17): error TS7030: Not all code paths return a value. @@ -33,7 +33,7 @@ tests/cases/compiler/reachabilityChecks5.ts(122,13): error TS7027: Unreachable c function f3(x): number { ~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. while (x) { throw new Error(); } diff --git a/tests/baselines/reference/reachabilityChecks7.errors.txt b/tests/baselines/reference/reachabilityChecks7.errors.txt index 3bf25a5ab049b..9ac2d0c1ad8ae 100644 --- a/tests/baselines/reference/reachabilityChecks7.errors.txt +++ b/tests/baselines/reference/reachabilityChecks7.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/reachabilityChecks7.ts(13,16): error TS7030: Not all code paths return a value. -tests/cases/compiler/reachabilityChecks7.ts(17,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/reachabilityChecks7.ts(17,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. ==== tests/cases/compiler/reachabilityChecks7.ts (2 errors) ==== @@ -23,7 +23,7 @@ tests/cases/compiler/reachabilityChecks7.ts(17,22): error TS2355: A function who async function f4(): Promise { ~~~~~~~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. } diff --git a/tests/baselines/reference/recursiveFunctionTypes.errors.txt b/tests/baselines/reference/recursiveFunctionTypes.errors.txt index 442f5e27a30b9..13ab49c4666ec 100644 --- a/tests/baselines/reference/recursiveFunctionTypes.errors.txt +++ b/tests/baselines/reference/recursiveFunctionTypes.errors.txt @@ -2,8 +2,8 @@ tests/cases/compiler/recursiveFunctionTypes.ts(1,28): error TS2322: Type 'number tests/cases/compiler/recursiveFunctionTypes.ts(3,5): error TS2322: Type '() => typeof fn' is not assignable to type 'number'. tests/cases/compiler/recursiveFunctionTypes.ts(4,5): error TS2322: Type '() => typeof fn' is not assignable to type '() => number'. Type '() => typeof fn' is not assignable to type 'number'. -tests/cases/compiler/recursiveFunctionTypes.ts(11,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. -tests/cases/compiler/recursiveFunctionTypes.ts(12,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/recursiveFunctionTypes.ts(11,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/recursiveFunctionTypes.ts(12,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/recursiveFunctionTypes.ts(17,5): error TS2322: Type '() => I' is not assignable to type 'number'. tests/cases/compiler/recursiveFunctionTypes.ts(22,5): error TS2345: Argument of type 'number' is not assignable to parameter of type '(t: typeof g) => void'. tests/cases/compiler/recursiveFunctionTypes.ts(25,1): error TS2322: Type 'number' is not assignable to type '() => any'. @@ -40,10 +40,10 @@ tests/cases/compiler/recursiveFunctionTypes.ts(43,4): error TS2769: No overload function f2(): typeof g2 { } ~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function g2(): typeof f2 { } ~~~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. interface I { } function f3(): I { return f3; } diff --git a/tests/baselines/reference/returnTypeParameter.errors.txt b/tests/baselines/reference/returnTypeParameter.errors.txt index 9f7cd47696863..24b9e725de8fd 100644 --- a/tests/baselines/reference/returnTypeParameter.errors.txt +++ b/tests/baselines/reference/returnTypeParameter.errors.txt @@ -1,11 +1,11 @@ -tests/cases/compiler/returnTypeParameter.ts(1,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/compiler/returnTypeParameter.ts(1,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/compiler/returnTypeParameter.ts(2,34): error TS2693: 'T' only refers to a type, but is being used as a value here. ==== tests/cases/compiler/returnTypeParameter.ts (2 errors) ==== function f(a: T): T { } // error, no return statement ~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. function f2(a: T): T { return T; } // bug was that this satisfied the return statement requirement ~ !!! error TS2693: 'T' only refers to a type, but is being used as a value here. \ No newline at end of file diff --git a/tests/baselines/reference/unknownType1.errors.txt b/tests/baselines/reference/unknownType1.errors.txt index 4ef8b5954228b..32034c8e2b9d9 100644 --- a/tests/baselines/reference/unknownType1.errors.txt +++ b/tests/baselines/reference/unknownType1.errors.txt @@ -20,7 +20,7 @@ tests/cases/conformance/types/unknown/unknownType1.ts(128,5): error TS2322: Type tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type 'number' is not assignable to type '{ [x: string]: unknown; }'. tests/cases/conformance/types/unknown/unknownType1.ts(143,29): error TS2698: Spread types may only be created from object types. tests/cases/conformance/types/unknown/unknownType1.ts(144,29): error TS2698: Spread types may only be created from object types. -tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. tests/cases/conformance/types/unknown/unknownType1.ts(156,14): error TS2700: Rest types may only be created from object types. tests/cases/conformance/types/unknown/unknownType1.ts(162,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. tests/cases/conformance/types/unknown/unknownType1.ts(170,9): error TS2322: Type 'T' is not assignable to type '{}'. @@ -223,7 +223,7 @@ tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type function f27(): unknown { ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. 'undefined' is also allowed with --strictNullChecks. +!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. } // Rest type cannot be created from unknown diff --git a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts deleted file mode 100644 index 003c03e083965..0000000000000 --- a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressions2.ts +++ /dev/null @@ -1,7 +0,0 @@ -// @strictNullChecks: true - -// @target: es5 - -function f1(): undefined | number { - // Okay; return type allows implicit return of undefined -} diff --git a/tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts new file mode 100644 index 0000000000000..89d3d5866221a --- /dev/null +++ b/tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts @@ -0,0 +1,19 @@ +// @strictNullChecks: true + +// @target: es2018 + +function f1(): undefined | number { + // Okay; return type allows implicit return of undefined +} + +function f2(): number { + // Error; return type does not include undefined +} + +async function f3(): Promise { + // Okay; return type allows implicit return of undefined +} + +async function f4(): Promise { + // Error; return type does not include undefined +}