docs/libs/typescript.js
function checkAsyncFunctionReturnType(node) {
            if (languageVersion >= 2 /* ES6 */) {
                var returnType = getTypeFromTypeNode(node.type);
                return checkCorrectPromiseType(returnType, node.type);
            }
            var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType();
            if (globalPromiseConstructorLikeType === emptyObjectType) {
                // If we couldn't resolve the global PromiseConstructorLike type we cannot verify
                // compatibility with __awaiter.
                return unknownType;
            }
            // As part of our emit for an async function, we will need to emit the entity name of
            // the return type annotation as an expression. To meet the necessary runtime semantics
            // for __awaiter, we must also check that the type of the declaration (e.g. the static
            // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`.
            //
            // An example might be (from lib.es6.d.ts):
            //
            //  interface Promise<T> { ... }
            //  interface PromiseConstructor {
            //      new <T>(...): Promise<T>;
            //  }
            //  declare var Promise: PromiseConstructor;
            //
            // When an async function declares a return type annotation of `Promise<T>`, we
            // need to get the type of the `Promise` variable declaration above, which would
            // be `PromiseConstructor`.
            //
            // The same case applies to a class:
            //
            //  declare class Promise<T> {
            //      constructor(...);
            //      then<U>(...): Promise<U>;
            //  }
            //
            // When we get the type of the `Promise` symbol here, we get the type of the static
            // side of the `Promise` class, which would be `{ new <T>(...): Promise<T> }`.
            var promiseType = getTypeFromTypeNode(node.type);
            if (promiseType === unknownType && compilerOptions.isolatedModules) {
                // If we are compiling with isolatedModules, we may not be able to resolve the
                // type as a value. As such, we will just return unknownType;
                return unknownType;
            }
            var promiseConstructor = getNodeLinks(node.type).resolvedSymbol;
            if (!promiseConstructor || !symbolIsValue(promiseConstructor)) {
                var typeName = promiseConstructor
                    ? symbolToString(promiseConstructor)
                    : typeToString(promiseType);
                error(node, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type, typeName);
                return unknownType;
            }
            // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced.
            checkReturnTypeAnnotationAsExpression(node);
            // Validate the promise constructor type.
            var promiseConstructorType = getTypeOfSymbol(promiseConstructor);
            if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, node, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type)) {
                return unknownType;
            }
            // Verify there is no local declaration that could collide with the promise constructor.
            var promiseName = ts.getEntityNameFromTypeNode(node.type);
            var promiseNameOrNamespaceRoot = getFirstIdentifier(promiseName);
            var rootSymbol = getSymbol(node.locals, promiseNameOrNamespaceRoot.text, 107455 /* Value */);
            if (rootSymbol) {
                error(rootSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, promiseNameOrNamespaceRoot.text, getFullyQualifiedName(promiseConstructor));
                return unknownType;
            }
            // Get and return the awaited type of the return type.
            return checkAwaitedType(promiseType, node, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type);
        }
function checkAsyncFunctionReturnType(node) {
            if (languageVersion >= 2 /* ES6 */) {
                var returnType = getTypeFromTypeNode(node.type);
                return checkCorrectPromiseType(returnType, node.type);
            }
            var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType();
            if (globalPromiseConstructorLikeType === emptyObjectType) {
                // If we couldn't resolve the global PromiseConstructorLike type we cannot verify
                // compatibility with __awaiter.
                return unknownType;
            }
            // As part of our emit for an async function, we will need to emit the entity name of
            // the return type annotation as an expression. To meet the necessary runtime semantics
            // for __awaiter, we must also check that the type of the declaration (e.g. the static
            // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`.
            //
            // An example might be (from lib.es6.d.ts):
            //
            //  interface Promise<T> { ... }
            //  interface PromiseConstructor {
            //      new <T>(...): Promise<T>;
            //  }
            //  declare var Promise: PromiseConstructor;
            //
            // When an async function declares a return type annotation of `Promise<T>`, we
            // need to get the type of the `Promise` variable declaration above, which would
            // be `PromiseConstructor`.
            //
            // The same case applies to a class:
            //
            //  declare class Promise<T> {
            //      constructor(...);
            //      then<U>(...): Promise<U>;
            //  }
            //
            // When we get the type of the `Promise` symbol here, we get the type of the static
            // side of the `Promise` class, which would be `{ new <T>(...): Promise<T> }`.
            var promiseType = getTypeFromTypeNode(node.type);
            if (promiseType === unknownType && compilerOptions.isolatedModules) {
                // If we are compiling with isolatedModules, we may not be able to resolve the
                // type as a value. As such, we will just return unknownType;
                return unknownType;
            }
            var promiseConstructor = getNodeLinks(node.type).resolvedSymbol;
            if (!promiseConstructor || !symbolIsValue(promiseConstructor)) {
                var typeName = promiseConstructor
                    ? symbolToString(promiseConstructor)
                    : typeToString(promiseType);
                error(node, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type, typeName);
                return unknownType;
            }
            // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced.
            checkReturnTypeAnnotationAsExpression(node);
            // Validate the promise constructor type.
            var promiseConstructorType = getTypeOfSymbol(promiseConstructor);
            if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, node, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type)) {
                return unknownType;
            }
            // Verify there is no local declaration that could collide with the promise constructor.
            var promiseName = ts.getEntityNameFromTypeNode(node.type);
            var promiseNameOrNamespaceRoot = getFirstIdentifier(promiseName);
            var rootSymbol = getSymbol(node.locals, promiseNameOrNamespaceRoot.text, 107455 /* Value */);
            if (rootSymbol) {
                error(rootSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, promiseNameOrNamespaceRoot.text, getFullyQualifiedName(promiseConstructor));
                return unknownType;
            }
            // Get and return the awaited type of the return type.
            return checkAwaitedType(promiseType, node, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type);
        }
function checkAsyncFunctionReturnType ( node ) { if ( languageVersion >= 2 /* ES6 */ ) { var returnType = getTypeFromTypeNode ( node . type ) ; return checkCorrectPromiseType ( returnType , node . type ) ; } var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType ( ) ; if ( globalPromiseConstructorLikeType === emptyObjectType ) { // If we couldn't resolve the global PromiseConstructorLike type we cannot verify // compatibility with __awaiter. return unknownType ; } // As part of our emit for an async function, we will need to emit the entity name of // the return type annotation as an expression. To meet the necessary runtime semantics // for __awaiter, we must also check that the type of the declaration (e.g. the static // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`. // // An example might be (from lib.es6.d.ts): // //  interface Promise<T> { ... } //  interface PromiseConstructor { //      new <T>(...): Promise<T>; //  } //  declare var Promise: PromiseConstructor; // // When an async function declares a return type annotation of `Promise<T>`, we // need to get the type of the `Promise` variable declaration above, which would // be `PromiseConstructor`. // // The same case applies to a class: // //  declare class Promise<T> { //      constructor(...); //      then<U>(...): Promise<U>; //  } // // When we get the type of the `Promise` symbol here, we get the type of the static // side of the `Promise` class, which would be `{ new <T>(...): Promise<T> }`. var promiseType = getTypeFromTypeNode ( node . type ) ; if ( promiseType === unknownType && compilerOptions . isolatedModules ) { // If we are compiling with isolatedModules, we may not be able to resolve the // type as a value. As such, we will just return unknownType; return unknownType ; } var promiseConstructor = getNodeLinks ( node . type ) . resolvedSymbol ; if ( ! promiseConstructor || ! symbolIsValue ( promiseConstructor ) ) { var typeName = promiseConstructor ? symbolToString ( promiseConstructor ) : typeToString ( promiseType ) ; error ( node , ts . Diagnostics . Type_0_is_not_a_valid_async_function_return_type , typeName ) ; return unknownType ; } // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced. checkReturnTypeAnnotationAsExpression ( node ) ; // Validate the promise constructor type. var promiseConstructorType = getTypeOfSymbol ( promiseConstructor ) ; if ( ! checkTypeAssignableTo ( promiseConstructorType , globalPromiseConstructorLikeType , node , ts . Diagnostics . Type_0_is_not_a_valid_async_function_return_type ) ) { return unknownType ; } // Verify there is no local declaration that could collide with the promise constructor. var promiseName = ts . getEntityNameFromTypeNode ( node . type ) ; var promiseNameOrNamespaceRoot = getFirstIdentifier ( promiseName ) ; var rootSymbol = getSymbol ( node . locals , promiseNameOrNamespaceRoot . text , 107455 /* Value */ ) ; if ( rootSymbol ) { error ( rootSymbol . valueDeclaration , ts . Diagnostics . Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions , promiseNameOrNamespaceRoot . text , getFullyQualifiedName ( promiseConstructor ) ) ; return unknownType ; } // Get and return the awaited type of the return type. return checkAwaitedType ( promiseType , node , ts . Diagnostics . An_async_function_or_method_must_have_a_valid_awaitable_return_type ) ; }
Checks the return type of an async function to ensure it is a compatible
Promise implementation.
@param node The signature to check
@param returnType The return type for the function
@remarks
This checks that an async function has a valid Promise-compatible return type,
and returns the *awaited type* of the promise. An async function has a valid
Promise-compatible return type if the resolved value of the return type has a
construct signature that takes in an `initializer` function that in turn supplies
a `resolve` function as one of its arguments and results in an object with a
callable `then` signature.
Checks the return type of an async function to ensure it is a compatible Promise implementation .