mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
Ban inferred return type on async and generator functions. (#58628)
This commit is contained in:
parent
ddf43cd0e0
commit
a203ace7af
@ -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 => {
|
||||
|
||||
@ -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 => {
|
||||
|
||||
71
tests/baselines/reference/transpile/declarationAsyncAndGeneratorFunctions.d.ts
vendored
Normal file
71
tests/baselines/reference/transpile/declarationAsyncAndGeneratorFunctions.d.ts
vendored
Normal 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
|
||||
}
|
||||
@ -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 {};
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user