mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
add check for function return unknow type
This commit is contained in:
parent
3ab7a98ecf
commit
691459304f
@ -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;
|
||||
}
|
||||
|
||||
|
||||
12
tests/baselines/reference/explicitReturnUnknow.errors.txt
Normal file
12
tests/baselines/reference/explicitReturnUnknow.errors.txt
Normal file
@ -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.
|
||||
|
||||
12
tests/baselines/reference/explicitReturnUnknow.js
Normal file
12
tests/baselines/reference/explicitReturnUnknow.js
Normal file
@ -0,0 +1,12 @@
|
||||
//// [explicitReturnUnknow.ts]
|
||||
function f(): unknown { ""; }
|
||||
function g(): string | undefined { ""; }
|
||||
|
||||
|
||||
//// [explicitReturnUnknow.js]
|
||||
function f() {
|
||||
"";
|
||||
}
|
||||
function g() {
|
||||
"";
|
||||
}
|
||||
7
tests/baselines/reference/explicitReturnUnknow.symbols
Normal file
7
tests/baselines/reference/explicitReturnUnknow.symbols
Normal file
@ -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))
|
||||
|
||||
9
tests/baselines/reference/explicitReturnUnknow.types
Normal file
9
tests/baselines/reference/explicitReturnUnknow.types
Normal file
@ -0,0 +1,9 @@
|
||||
=== tests/cases/compiler/explicitReturnUnknow.ts ===
|
||||
function f(): unknown { ""; }
|
||||
>f : () => unknown
|
||||
>"" : ""
|
||||
|
||||
function g(): string | undefined { ""; }
|
||||
>g : () => string
|
||||
>"" : ""
|
||||
|
||||
@ -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
|
||||
|
||||
2
tests/cases/compiler/explicitReturnUnknow.ts
Normal file
2
tests/cases/compiler/explicitReturnUnknow.ts
Normal file
@ -0,0 +1,2 @@
|
||||
function f(): unknown { ""; }
|
||||
function g(): string | undefined { ""; }
|
||||
Loading…
x
Reference in New Issue
Block a user