Skip to content

Type string is assignable to interface with index signature from number to string #41228

@vostrnad

Description

@vostrnad

TypeScript Version: 4.1.0-beta

Search Terms: string assignable index signature number

Code

interface NumberIndexable {
  [index: number]: string;
}

function setValueAtIndex(object: NumberIndexable, index: number, value: string) {
  object[index] = value;
}

setValueAtIndex({}, 0, 'test'); // works fine
setValueAtIndex('oops', 0, 'test'); // runtime error

Expected behavior:

Second call to setValueAtIndex should raise a compiler error.

Actual behavior:

No compiler error is raised, which results in a runtime error because string indeces are read-only.

Playground Link: Playground

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