GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Validate indexed properties · Issue #8620 · microsoft/TypeScript

Validate indexed properties #8620

New issue
Copy link
New issue
Copy link
Closed
Closed
Validate indexed properties#8620
Copy link
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code

Description

@Liero
Daniel Turan (Liero)
opened on May 16, 2016
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'

Activity

  1. mhegazy commented on May 16, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on May 16, 2016
    Contributor
    More actions

    The compiler does know that values of type Person have a property called First Name and that its type is string. see:

    image

    It also knows that all JavaScript objects can be indexed in using strings. For these it does not know the type of the property, so it gives it a type any.

    You can use --noImplicitAny flag to catch these cases and flag them as errors.

    image

    As for completion for the name, this will be supported by #8428.

  2. Mohamed Hegazy (mhegazy)
    added
    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
    on May 16, 2016
  3. Mohamed Hegazy (mhegazy)
    closed this as completedon May 16, 2016
  4. Liero commented on May 17, 2016

    @Liero
    Daniel Turan (Liero)
    on May 17, 2016
    Author
    More actions

    ahh, ok. I thought --noImplicitAnyis related to unannotated variables and parameters, but person is annotated. Hmm, I would welcome if those two were splitted into two flags (e.g --noImplicitAny, --noImplicitIndex), but that's probably not gonna happen. Also the error message could be: "Property 'undefined' does not exists on type 'Person'.

  5. microsoft
    locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.