Skip to content

Number index signature doesn't work with Readonly generic #17857

Closed
@remagpie

Description

@remagpie

TypeScript Version: 2.4.2

Code

type Foo = Readonly<{
  [n: number]: string;
}>;

type Bar = {
  readonly [n: number]: string;
};

function foobar(): void {
  const x: Foo = {
    [0]: 'asdf',
    [1]: 'fdsa',
    [-1]: 'dddd',
  };
  const y: Bar = {
    [0]: 'qwer',
    [1]: 'rewq',
    [-1]: 'eeee',
  };
  const z: string = x[0] + y[0];
}

Expected behavior:
No Error

Actual behavior:
Shows error Element implicitly has an 'any' type because type 'Readonly<{ [n: number]: string; }>' has no index signature. for x[0].

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions