Skip to content

Loop contains block-scoped variable 'i' referenced by a function in the loop #6219

Closed
@tcstory

Description

@tcstory

typescript version: 1.7.5

I have the following code,

function testing () {
    for (let i = 0; i < 5; i++) {
        setTimeout(function () {
            console.log(i);
        },0);
    }
}

when set the target javascript version to es5, ts compiler reports the following error:

Error:(7, 5) TS4091: Loop contains block-scoped variable 'i' referenced by a function in the loop. This is only supported in ECMAScript 6 or higher.

but the compiled code is ok. and if I change the target javascript to es6, it won't have this error.

who can tell me why it give me the error? thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions