Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import {
Expression,
forEachReturnStatement,
FunctionExpression,
FunctionFlags,
FunctionLikeDeclaration,
GetAccessorDeclaration,
getEffectiveReturnTypeNode,
getEffectiveTypeAnnotationNode,
getFunctionFlags,
getJSDocTypeAssertionType,
getStrictOptionValue,
HasInferredType,
Expand Down Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 {};
}
22 changes: 22 additions & 0 deletions tests/cases/transpile/declarationAsyncAndGeneratorFunctions.ts
Original file line number Diff line number Diff line change
@@ -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
}