Skip to content

Error: Type T cannot be used to index intersection with Record<T, any> #22317

Closed
@NaridaL

Description

@NaridaL

TypeScript Version: 2.8.0-dev.20180302

Search Terms: Type cannot be used to index intersection Type cannot be used to index

Code

interface I {a: 1}
function foo<T extends string>(o: I & Record<T, number>, k: T) {
    o[k] // Type 'T' cannot be used to index type 'I & Record<T, number>'.
}
// the following work:
function foo2<T extends string>(o: Record<T, number>, k: T) {
    o[k]
}
// same result, but T different:
function bar<T extends Record<string, number>>(o: I & T, k: keyof T) {
    o[k]
}

Expected behavior:
No errors.
Actual behavior:
Error.

Playground Link: playground

Related Issues:
There's #18538, but that seems to be an issue with keyof, which isn't an used here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions