Skip to content
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

Allow narrowing properties retrieved from types with index signatures #17567

Closed
kujon opened this issue Aug 2, 2017 · 3 comments · Fixed by #19838
Closed

Allow narrowing properties retrieved from types with index signatures #17567

kujon opened this issue Aug 2, 2017 · 3 comments · Fixed by #19838
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@kujon
Copy link
Contributor

kujon commented Aug 2, 2017

TypeScript Version: 2.4.0

Code

type A = {
    [index: string]: [number, number] | null;
};

const value: A = {
    foo: [42, 42]
};

const extractedValue = value.foo !== null ?
    // Object is possibly 'null'.
    value.foo[0] :
    5;

Expected behavior:
Null check is correctly performed and the compiler allows for accessing

Actual behavior:
// Object is possibly 'null'.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 2, 2017
@DanielRosenwasser
Copy link
Member

This would require us to synthesize some sort of knowledge about a property that's never been declared on the type A.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

Should be doable. getFlowTypeOfReference operates on nodes, so we should be able to pass in the node, along with the declared type from the index signature.

@sandersn
Copy link
Member

sandersn commented Nov 8, 2017

Fix is up at #19838.

@sandersn sandersn added the Fixed A PR has been merged for this issue label Nov 8, 2017
@ghost ghost mentioned this issue 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
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants