Skip to content

Index key accessed nullable index type is not narrowed from nullable #12636

Closed
@tinganho

Description

@tinganho
interface S {
    hello: any;
}

interface R {
    [i: string]: S | undefined;
}

function f<K extends keyof R>(p: K) {
    let r: R = {};
    if (r[p]) {
        r[p].hello // Error: hello does not exists on type R[K]
    }
}

Removing the type undefined from S | undefined fixes the error and also the if check is no longer required.

Metadata

Metadata

Assignees

Labels

Design LimitationConstraints of the existing architecture prevent this from being fixedSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions