|
1 |
| -tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(5,16): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
2 |
| -tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(13,22): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 1 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(5,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 2 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(9,17): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 3 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(17,7): error TS2322: Type '() => void' is not assignable to type '() => number | undefined'. |
| 4 | + Type 'void' is not assignable to type 'number | undefined'. |
| 5 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(21,7): error TS2322: Type '() => void' is not assignable to type '() => number'. |
| 6 | + Type 'void' is not assignable to type 'number'. |
| 7 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(29,23): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 8 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(33,23): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 9 | +tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(52,3): error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => undefined'. |
| 10 | + Type 'void' is not assignable to type 'undefined'. |
3 | 11 |
|
4 | 12 |
|
5 |
| -==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts (2 errors) ==== |
6 |
| - function f1(): undefined | number { |
7 |
| - // Okay; return type allows implicit return of undefined |
| 13 | +==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts (7 errors) ==== |
| 14 | + function f10(): undefined { |
| 15 | + // Ok, return type allows implicit return of undefined |
8 | 16 | }
|
9 | 17 |
|
10 |
| - function f2(): number { |
11 |
| - ~~~~~~ |
12 |
| -!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
13 |
| - // Error; return type does not include undefined |
| 18 | + function f11(): undefined | number { |
| 19 | + ~~~~~~~~~~~~~~~~~~ |
| 20 | +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 21 | + // Error, return type isn't just undefined |
14 | 22 | }
|
15 | 23 |
|
16 |
| - async function f3(): Promise<undefined | number> { |
17 |
| - // Okay; return type allows implicit return of undefined |
| 24 | + function f12(): number { |
| 25 | + ~~~~~~ |
| 26 | +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 27 | + // Error, return type doesn't include undefined |
18 | 28 | }
|
19 | 29 |
|
20 |
| - async function f4(): Promise<number> { |
21 |
| - ~~~~~~~~~~~~~~~ |
22 |
| -!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
23 |
| - // Error; return type does not include undefined |
| 30 | + const f20: () => undefined = () => { |
| 31 | + // Ok, contextual type for implicit return is undefined |
24 | 32 | }
|
| 33 | + |
| 34 | + const f21: () => undefined | number = () => { |
| 35 | + ~~~ |
| 36 | +!!! error TS2322: Type '() => void' is not assignable to type '() => number | undefined'. |
| 37 | +!!! error TS2322: Type 'void' is not assignable to type 'number | undefined'. |
| 38 | + // Regular void function because contextual type for implicit return isn't just undefined |
| 39 | + } |
| 40 | + |
| 41 | + const f22: () => number = () => { |
| 42 | + ~~~ |
| 43 | +!!! error TS2322: Type '() => void' is not assignable to type '() => number'. |
| 44 | +!!! error TS2322: Type 'void' is not assignable to type 'number'. |
| 45 | + // Regular void function because contextual type for implicit return isn't just undefined |
| 46 | + } |
| 47 | + |
| 48 | + async function f30(): Promise<undefined> { |
| 49 | + // Ok, return type allows implicit return of undefined |
| 50 | + } |
| 51 | + |
| 52 | + async function f31(): Promise<undefined | number> { |
| 53 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 54 | +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 55 | + // Error, return type isn't just undefined |
| 56 | + } |
| 57 | + |
| 58 | + async function f32(): Promise<number> { |
| 59 | + ~~~~~~~~~~~~~~~ |
| 60 | +!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. |
| 61 | + // Error, return type doesn't include undefined |
| 62 | + } |
| 63 | + |
| 64 | + // Examples from #36288 |
| 65 | + |
| 66 | + declare function f(a: () => undefined): void; |
| 67 | + |
| 68 | + f(() => { }); |
| 69 | + |
| 70 | + f((): undefined => { }); |
| 71 | + |
| 72 | + const g1: () => undefined = () => { }; |
| 73 | + |
| 74 | + const g2 = (): undefined => { }; |
| 75 | + |
| 76 | + function h1() { |
| 77 | + } |
| 78 | + |
| 79 | + f(h1); // Error |
| 80 | + ~~ |
| 81 | +!!! error TS2345: Argument of type '() => void' is not assignable to parameter of type '() => undefined'. |
| 82 | +!!! error TS2345: Type 'void' is not assignable to type 'undefined'. |
| 83 | + |
| 84 | + function h2(): undefined { |
| 85 | + } |
| 86 | + |
| 87 | + f(h2); |
25 | 88 |
|
0 commit comments