Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Validate indexed properties #8620

Copy link
Copy link
@Liero

Description

@Liero
Issue body actions

When object is annotated with following definition:

interface Person {
  "Id": number
  "First Name": string,
  "Last Name": string,
}

I would like to access the "First Name" and "Last Name" properties in type safe manner.

var person : Person = { Id: 5, "First Name": "Lorem", "Last Name": "Ipsum" };
var firstName = person["First Name"]; //this is neither validated by compiler nor I get auto completion for the properties
var _undefined = person["undefined"]; //compiler does not complain.

similarly:

person["First Name"] = "Lorem"; //this is neither validated by compiler nor I get auto completion for the properties
person["undefined"] = "Ipsum"; //compiler does not complain.

I have tried experimenting with restricting index signature type:

interface Person {
  [key: "First Name" | "Last Name"]: string;
} 

however, it compiler complains:

An index signature parameter type must be 'string' or 'number'

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.