Ban inferred return type on async and generator functions. (#58628)

This commit is contained in:
Titian Cernicova-Dragomir 2024-05-23 19:29:49 +03:00 committed by GitHub
parent ddf43cd0e0
commit a203ace7af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 134 additions and 0 deletions

View File

@ -48984,6 +48984,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function getSingleReturnExpression(declaration: SignatureDeclaration | undefined): Expression | undefined {
let candidateExpr: Expression | undefined;
if (declaration && !nodeIsMissing((declaration as FunctionLikeDeclaration).body)) {
if (getFunctionFlags(declaration) & FunctionFlags.AsyncGenerator) return undefined;
const body = (declaration as FunctionLikeDeclaration).body;
if (body && isBlock(body)) {
forEachReturnStatement(body, s => {

View File

@ -14,10 +14,12 @@ import {
Expression,
forEachReturnStatement,
FunctionExpression,
FunctionFlags,
FunctionLikeDeclaration,
GetAccessorDeclaration,
getEffectiveReturnTypeNode,
getEffectiveTypeAnnotationNode,
getFunctionFlags,
getJSDocTypeAssertionType,
getStrictOptionValue,
HasInferredType,
@ -469,6 +471,8 @@ export function createSyntacticTypeNodeBuilder(options: CompilerOptions, resolve
function typeFromSingleReturnExpression(declaration: FunctionLikeDeclaration | undefined, context: SyntacticTypeNodeBuilderContext): boolean | undefined {
let candidateExpr: Expression | undefined;
if (declaration && !nodeIsMissing(declaration.body)) {
if (getFunctionFlags(declaration) & FunctionFlags.AsyncGenerator) return undefined;
const body = declaration.body;
if (body && isBlock(body)) {
forEachReturnStatement(body, s => {

View File

@ -0,0 +1,71 @@
//// [declarationAsyncAndGeneratorFunctions.ts] ////
export async function asyncFn() {
return {} as Promise<void>
}
export async function asyncFn2() {
return {} as number
}
export async function asyncFn3() {
return (await 42) as number;
}
export function* generatorFn() {
return {} as number
}
export async function* asyncGeneratorFn() {
return {} as number
}
//// [declarationAsyncAndGeneratorFunctions.d.ts] ////
export declare function asyncFn(): unknown;
export declare function asyncFn2(): unknown;
export declare function asyncFn3(): unknown;
export declare function generatorFn(): {};
export declare function asyncGeneratorFn(): {};
//// [Diagnostics reported]
declarationAsyncAndGeneratorFunctions.ts(1,23): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
declarationAsyncAndGeneratorFunctions.ts(5,23): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
declarationAsyncAndGeneratorFunctions.ts(9,23): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
declarationAsyncAndGeneratorFunctions.ts(13,18): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
declarationAsyncAndGeneratorFunctions.ts(17,24): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
==== declarationAsyncAndGeneratorFunctions.ts (5 errors) ====
export async function asyncFn() {
~~~~~~~
!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:1:23: Add a return type to the function declaration.
return {} as Promise<void>
}
export async function asyncFn2() {
~~~~~~~~
!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:5:23: Add a return type to the function declaration.
return {} as number
}
export async function asyncFn3() {
~~~~~~~~
!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:9:23: Add a return type to the function declaration.
return (await 42) as number;
}
export function* generatorFn() {
~~~~~~~~~~~
!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:13:18: Add a return type to the function declaration.
return {} as number
}
export async function* asyncGeneratorFn() {
~~~~~~~~~~~~~~~~
!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:17:24: Add a return type to the function declaration.
return {} as number
}

View File

@ -0,0 +1,36 @@
//// [declarationAsyncAndGeneratorFunctions.ts] ////
export async function asyncFn() {
return {} as Promise<void>
}
export async function asyncFn2() {
return {} as number
}
export async function asyncFn3() {
return (await 42) as number;
}
export function* generatorFn() {
return {} as number
}
export async function* asyncGeneratorFn() {
return {} as number
}
//// [declarationAsyncAndGeneratorFunctions.js] ////
export async function asyncFn() {
return {};
}
export async function asyncFn2() {
return {};
}
export async function asyncFn3() {
return (await 42);
}
export function* generatorFn() {
return {};
}
export async function* asyncGeneratorFn() {
return {};
}

View File

@ -0,0 +1,22 @@
// @declaration: true
// @target: esnext
export async function asyncFn() {
return {} as Promise<void>
}
export async function asyncFn2() {
return {} as number
}
export async function asyncFn3() {
return (await 42) as number;
}
export function* generatorFn() {
return {} as number
}
export async function* asyncGeneratorFn() {
return {} as number
}