Skip to content

Narrow property access of undeclared properties from string index signatures #19838

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2017

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Nov 8, 2017

Previously these accesses did not use control flow to narrow property accesses of undefined properties that are resolved from a string index signature. Now the use control flow to narrow these just like normal properties.

Fixes #17567

Previously these accesses did not use control flow to narrow property
accesses of undefined properties that are resolved from a string index
signature. Now the use control flow to narrow these just like normal
properties.
Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, otherwise.

type A = { [index: string]: number | null };
declare const value: A;
if (value.foo !== null) {
value.foo.toExponential()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a value.bar inside the if statement, too? Just to verify that other properties aren't narrowed despite being the same index signature. (I don't think it'll be wrong, I just want to see it in the test)

@sandersn sandersn merged commit 76a3be7 into master Nov 8, 2017
@sandersn sandersn deleted the narrow-index-signature-property-access branch November 8, 2017 20:10
@yseymour
Copy link

Should this be fixed?

interface Dictionary<T> {[K:string] : T|undefined}
const d:Dictionary<any[]> = {};
if (d['k']) d['k'].push(42);  // Object is possibly 'undefined'

It works with the dot operator — surely d.k and d['k'] are semantically equivalent.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 14, 2017

@yseymour that is tracked by #10530

@ghost ghost mentioned this pull request Dec 11, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow narrowing properties retrieved from types with index signatures
4 participants