Skip to content

Commit 6914593

Browse files
committed
add check for function return unknow type
1 parent 3ab7a98 commit 6914593

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20621,7 +20621,7 @@ namespace ts {
2062120621
}
2062220622

2062320623
// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
20624-
if (returnType && maybeTypeOfKind(returnType, TypeFlags.AnyOrUnknown | TypeFlags.Void)) {
20624+
if (returnType && maybeTypeOfKind(returnType, TypeFlags.Any | TypeFlags.Void)) {
2062520625
return;
2062620626
}
2062720627

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/explicitReturnUnknow.ts(1,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
2+
tests/cases/compiler/explicitReturnUnknow.ts(2,15): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
3+
4+
5+
==== tests/cases/compiler/explicitReturnUnknow.ts (2 errors) ====
6+
function f(): unknown { ""; }
7+
~~~~~~~
8+
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
9+
function g(): string | undefined { ""; }
10+
~~~~~~~~~~~~~~~~~~
11+
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [explicitReturnUnknow.ts]
2+
function f(): unknown { ""; }
3+
function g(): string | undefined { ""; }
4+
5+
6+
//// [explicitReturnUnknow.js]
7+
function f() {
8+
"";
9+
}
10+
function g() {
11+
"";
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/explicitReturnUnknow.ts ===
2+
function f(): unknown { ""; }
3+
>f : Symbol(f, Decl(explicitReturnUnknow.ts, 0, 0))
4+
5+
function g(): string | undefined { ""; }
6+
>g : Symbol(g, Decl(explicitReturnUnknow.ts, 0, 29))
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/explicitReturnUnknow.ts ===
2+
function f(): unknown { ""; }
3+
>f : () => unknown
4+
>"" : ""
5+
6+
function g(): string | undefined { ""; }
7+
>g : () => string
8+
>"" : ""
9+

tests/baselines/reference/unknownType1.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ tests/cases/conformance/types/unknown/unknownType1.ts(114,9): error TS2322: Type
1818
Type 'unknown' is not assignable to type '{}'.
1919
tests/cases/conformance/types/unknown/unknownType1.ts(120,9): error TS2322: Type 'T' is not assignable to type 'object'.
2020
tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type '123' is not assignable to type '{ [x: string]: unknown; }'.
21+
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.
2122
tests/cases/conformance/types/unknown/unknownType1.ts(155,14): error TS2700: Rest types may only be created from object types.
2223
tests/cases/conformance/types/unknown/unknownType1.ts(161,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
2324

2425

25-
==== tests/cases/conformance/types/unknown/unknownType1.ts (21 errors) ====
26+
==== tests/cases/conformance/types/unknown/unknownType1.ts (22 errors) ====
2627
// In an intersection everything absorbs unknown
2728

2829
type T00 = unknown & null; // null
@@ -211,6 +212,8 @@ tests/cases/conformance/types/unknown/unknownType1.ts(161,5): error TS2564: Prop
211212
// Functions with unknown return type don't need return expressions
212213

213214
function f27(): unknown {
215+
~~~~~~~
216+
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
214217
}
215218

216219
// Rest type cannot be created from unknown
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function f(): unknown { ""; }
2+
function g(): string | undefined { ""; }

0 commit comments

Comments
 (0)