Skip to content

async, await, loop, implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer. #36687

Closed
@AnyhowStep

Description

@AnyhowStep

TypeScript Version: 3.5.1, 3.7.5, 3.8-Beta

Search Terms: async, await, loop, promise, variable assignment, implicitly has type any, referenced directly or indirectly in its own initializer.

Code

interface Foo {
    id : string,
}

declare function paginate(after: string|undefined): Promise<Foo[]>;

async function main() {
    let after: string | undefined = undefined;
    while (true) {
        const page = await paginate(after);
        for (const foo of page) {
            after = foo.id;
        }
    }   
}

Expected behavior:

Type checks successfully

Actual behavior:
image

Playground Link: Playground

Related Issues:

Similar to #14428 , an already fixed issue.

Similar to #36666 , but without destructuring; and my initial variable also has explicit type annotations (after : string|undefined)


Workaround

Just use an explicit type annotation,

const page : Foo[] = await paginate(after);

However, it is strange. It shouldn't need the explicit type annotation because it can only possibly be Foo[].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions