Skip to content

Type guards do not work on array access expressions #11483

@OleksandrNechai

Description

@OleksandrNechai

TypeScript Version: 2.0.3

Code

let list = [1, '2'];
for (let i = 0; i < list.length; i++) {
    const j = i;
    if (typeof list[j] === 'string') {
        console.log(list[j].charAt(0)); // Property 'charAt' does not exist on type 'string | number'
    }
}

Expected behavior:
Successful compilation, since the type of list[j] is made clear with type guard

Actual behavior:
error TS2339: Property 'charAt' does not exist on type 'string | number'

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