Skip to content

type has no index signature #14951

@testerez

Description

@testerez

TypeScript Version: 2.2.2

Code

function getName(id: number) {
  return {
    1: 'one',
    2: 'two',
  }[id] || 'something else';
}

Expected behavior:
{1: 'one'}[id] type should be string | undefined
So I would expect the return type to be inferred as string.

Actual behavior:

Element implicitly has an 'any' type because type '{ 1: string; 2: string; }' has no index signature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions