Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Property access for string index signatures#12671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sandersn merged 4 commits intomasterfromproperty-access-for-string-index-sigs
Jan 18, 2017

Conversation

sandersn
Copy link
Member

Fixes#12596

This PR allows property access (dotted syntax) for arbitrary identifiers on types that have an explicitly declared string index signature. Previously, only element access (bracket syntax) was allowed. For example:

interfaceFlags{[name:string]:boolean}letflags:Flags;// newflags.thisIsFine=true;flags.imOkayWithTheEventsThatAreUnfoldingCurrently=true;// still worksflags["That's okay, things are going to be okay."]=true;

Improvements:

  1. Javascript says that property and element access are basically the same thing. Typescript does too now.
  2. Property access now behaves the way Javascript programmers expect. Less friction when learning Typescript and less code that needs to be rewritten to use element access.

Drawbacks:

Types with string index signatures allow any property name. This means:

  1. You won't get errors if you use a property that doesn't exist.
  2. The compiler won't report errors for typos.
interfaceMap<T>{[s:string]:T}letforgot:Map<number>;// oops, forgot isn't an array!for(leti=0;i<forgot.length;i++){console.log(forgot[i]);}letcantType:Map<string>;cantType.something='soemthing';if(cantType.soemthing){console.log(cantType.soemthign);launchMissiles(cantType.soehign);}

j-oliveras, svieira, AlexanderEkdahl, abenhamdine, unional, and petrgazarov reacted with thumbs up emojiblakeembrey, j-oliveras, zamb3zi, abenhamdine, unional, saschanaz, and niieani reacted with hooray emojiblakeembrey, T4cC0re, abenhamdine, and unional reacted with heart emoji
@mhegazy
Copy link
Contributor

@sandersn can you refresh and merge?

@sandersnsandersn merged commit4759ade intomasterJan 18, 2017
@sandersnsandersn deleted the property-access-for-string-index-sigs branchJanuary 18, 2017 16:38
@sandersn
Copy link
MemberAuthor

Done

niieani added a commit to niieani/typescript-vs-flowtype that referenced this pull requestFeb 3, 2017
@microsoftmicrosoft locked and limited conversation to collaboratorsJun 19, 2018
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@mhegazymhegazymhegazy approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@sandersn@mhegazy@msftclas

[8]ページ先頭

©2009-2025 Movatter.jp