From 691459304fa48d5f6ce30297772e00ba52068533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Thu, 2 Aug 2018 18:24:45 +0800 Subject: [PATCH 1/2] add check for function return unknow type --- src/compiler/checker.ts | 2 +- .../reference/explicitReturnUnknow.errors.txt | 12 ++++++++++++ tests/baselines/reference/explicitReturnUnknow.js | 12 ++++++++++++ .../baselines/reference/explicitReturnUnknow.symbols | 7 +++++++ tests/baselines/reference/explicitReturnUnknow.types | 9 +++++++++ tests/baselines/reference/unknownType1.errors.txt | 5 ++++- tests/cases/compiler/explicitReturnUnknow.ts | 2 ++ 7 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/explicitReturnUnknow.errors.txt create mode 100644 tests/baselines/reference/explicitReturnUnknow.js create mode 100644 tests/baselines/reference/explicitReturnUnknow.symbols create mode 100644 tests/baselines/reference/explicitReturnUnknow.types create mode 100644 tests/cases/compiler/explicitReturnUnknow.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1c1eebd94e4..c5b2b766313 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20621,7 +20621,7 @@ namespace ts { } // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. - if (returnType && maybeTypeOfKind(returnType, TypeFlags.AnyOrUnknown | TypeFlags.Void)) { + if (returnType && maybeTypeOfKind(returnType, TypeFlags.Any | TypeFlags.Void)) { return; } diff --git a/tests/baselines/reference/explicitReturnUnknow.errors.txt b/tests/baselines/reference/explicitReturnUnknow.errors.txt new file mode 100644 index 00000000000..69de40ba4e0 --- /dev/null +++ b/tests/baselines/reference/explicitReturnUnknow.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/explicitReturnUnknow.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/compiler/explicitReturnUnknow.ts(2,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. + + +==== tests/cases/compiler/explicitReturnUnknow.ts (2 errors) ==== + function f(): unknown { ""; } + ~~~~~~~ +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. + function g(): string | undefined { ""; } + ~~~~~~~~~~~~~~~~~~ +!!! 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/explicitReturnUnknow.js b/tests/baselines/reference/explicitReturnUnknow.js new file mode 100644 index 00000000000..f42b9e878f1 --- /dev/null +++ b/tests/baselines/reference/explicitReturnUnknow.js @@ -0,0 +1,12 @@ +//// [explicitReturnUnknow.ts] +function f(): unknown { ""; } +function g(): string | undefined { ""; } + + +//// [explicitReturnUnknow.js] +function f() { + ""; +} +function g() { + ""; +} diff --git a/tests/baselines/reference/explicitReturnUnknow.symbols b/tests/baselines/reference/explicitReturnUnknow.symbols new file mode 100644 index 00000000000..77e2fa68b89 --- /dev/null +++ b/tests/baselines/reference/explicitReturnUnknow.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/explicitReturnUnknow.ts === +function f(): unknown { ""; } +>f : Symbol(f, Decl(explicitReturnUnknow.ts, 0, 0)) + +function g(): string | undefined { ""; } +>g : Symbol(g, Decl(explicitReturnUnknow.ts, 0, 29)) + diff --git a/tests/baselines/reference/explicitReturnUnknow.types b/tests/baselines/reference/explicitReturnUnknow.types new file mode 100644 index 00000000000..fabad2e4602 --- /dev/null +++ b/tests/baselines/reference/explicitReturnUnknow.types @@ -0,0 +1,9 @@ +=== tests/cases/compiler/explicitReturnUnknow.ts === +function f(): unknown { ""; } +>f : () => unknown +>"" : "" + +function g(): string | undefined { ""; } +>g : () => string +>"" : "" + diff --git a/tests/baselines/reference/unknownType1.errors.txt b/tests/baselines/reference/unknownType1.errors.txt index 37d0cd010e7..83849184dce 100644 --- a/tests/baselines/reference/unknownType1.errors.txt +++ b/tests/baselines/reference/unknownType1.errors.txt @@ -18,11 +18,12 @@ tests/cases/conformance/types/unknown/unknownType1.ts(114,9): error TS2322: Type Type 'unknown' is not assignable to type '{}'. tests/cases/conformance/types/unknown/unknownType1.ts(120,9): error TS2322: Type 'T' is not assignable to type 'object'. tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type '123' is not assignable to type '{ [x: string]: unknown; }'. +tests/cases/conformance/types/unknown/unknownType1.ts(149,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. tests/cases/conformance/types/unknown/unknownType1.ts(155,14): error TS2700: Rest types may only be created from object types. tests/cases/conformance/types/unknown/unknownType1.ts(161,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. -==== tests/cases/conformance/types/unknown/unknownType1.ts (21 errors) ==== +==== tests/cases/conformance/types/unknown/unknownType1.ts (22 errors) ==== // In an intersection everything absorbs unknown type T00 = unknown & null; // null @@ -211,6 +212,8 @@ tests/cases/conformance/types/unknown/unknownType1.ts(161,5): error TS2564: Prop // Functions with unknown return type don't need return expressions function f27(): unknown { + ~~~~~~~ +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. } // Rest type cannot be created from unknown diff --git a/tests/cases/compiler/explicitReturnUnknow.ts b/tests/cases/compiler/explicitReturnUnknow.ts new file mode 100644 index 00000000000..aecd05a7a9d --- /dev/null +++ b/tests/cases/compiler/explicitReturnUnknow.ts @@ -0,0 +1,2 @@ +function f(): unknown { ""; } +function g(): string | undefined { ""; } From c47a57fb763dad9611593c7e9e01a73d34e6064d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Fri, 3 Aug 2018 16:12:56 +0800 Subject: [PATCH 2/2] remove useless test case --- .../reference/explicitReturnUnknow.errors.txt | 12 ------------ tests/baselines/reference/explicitReturnUnknow.js | 12 ------------ .../baselines/reference/explicitReturnUnknow.symbols | 7 ------- tests/baselines/reference/explicitReturnUnknow.types | 9 --------- tests/cases/compiler/explicitReturnUnknow.ts | 2 -- 5 files changed, 42 deletions(-) delete mode 100644 tests/baselines/reference/explicitReturnUnknow.errors.txt delete mode 100644 tests/baselines/reference/explicitReturnUnknow.js delete mode 100644 tests/baselines/reference/explicitReturnUnknow.symbols delete mode 100644 tests/baselines/reference/explicitReturnUnknow.types delete mode 100644 tests/cases/compiler/explicitReturnUnknow.ts diff --git a/tests/baselines/reference/explicitReturnUnknow.errors.txt b/tests/baselines/reference/explicitReturnUnknow.errors.txt deleted file mode 100644 index 69de40ba4e0..00000000000 --- a/tests/baselines/reference/explicitReturnUnknow.errors.txt +++ /dev/null @@ -1,12 +0,0 @@ -tests/cases/compiler/explicitReturnUnknow.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. -tests/cases/compiler/explicitReturnUnknow.ts(2,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. - - -==== tests/cases/compiler/explicitReturnUnknow.ts (2 errors) ==== - function f(): unknown { ""; } - ~~~~~~~ -!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. - function g(): string | undefined { ""; } - ~~~~~~~~~~~~~~~~~~ -!!! 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/explicitReturnUnknow.js b/tests/baselines/reference/explicitReturnUnknow.js deleted file mode 100644 index f42b9e878f1..00000000000 --- a/tests/baselines/reference/explicitReturnUnknow.js +++ /dev/null @@ -1,12 +0,0 @@ -//// [explicitReturnUnknow.ts] -function f(): unknown { ""; } -function g(): string | undefined { ""; } - - -//// [explicitReturnUnknow.js] -function f() { - ""; -} -function g() { - ""; -} diff --git a/tests/baselines/reference/explicitReturnUnknow.symbols b/tests/baselines/reference/explicitReturnUnknow.symbols deleted file mode 100644 index 77e2fa68b89..00000000000 --- a/tests/baselines/reference/explicitReturnUnknow.symbols +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/compiler/explicitReturnUnknow.ts === -function f(): unknown { ""; } ->f : Symbol(f, Decl(explicitReturnUnknow.ts, 0, 0)) - -function g(): string | undefined { ""; } ->g : Symbol(g, Decl(explicitReturnUnknow.ts, 0, 29)) - diff --git a/tests/baselines/reference/explicitReturnUnknow.types b/tests/baselines/reference/explicitReturnUnknow.types deleted file mode 100644 index fabad2e4602..00000000000 --- a/tests/baselines/reference/explicitReturnUnknow.types +++ /dev/null @@ -1,9 +0,0 @@ -=== tests/cases/compiler/explicitReturnUnknow.ts === -function f(): unknown { ""; } ->f : () => unknown ->"" : "" - -function g(): string | undefined { ""; } ->g : () => string ->"" : "" - diff --git a/tests/cases/compiler/explicitReturnUnknow.ts b/tests/cases/compiler/explicitReturnUnknow.ts deleted file mode 100644 index aecd05a7a9d..00000000000 --- a/tests/cases/compiler/explicitReturnUnknow.ts +++ /dev/null @@ -1,2 +0,0 @@ -function f(): unknown { ""; } -function g(): string | undefined { ""; }