Skip to content

type parameter with a constraint on an indexed type cannot be indexed #31726

Closed
@sthuck

Description

@sthuck

TypeScript Version: 3.6.0-dev.20190602

Search Terms:
generics, indexed types
Code

function foo<E extends {[key: string]: string}>(e: E) {
  e['a'] = 'a';
}

Expected behavior:
you should be able to index the e function parameter, as was the behavior before 3.5.
Actual behavior:
in typescript 3.5 this code results in a Type '"a"' cannot be used to index type 'E' error.

I understand this is (probably) caused by this change #30637

But since I'm adding a constraint on the type parameter I feel like this still keeps type safety and should be a bug.

(This feels like such an obvious use case I figured there has to be an issue already open or closed and I was really hesitant on opening this issue, but I couldn't find any. If it's a duplicate, sorry...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions