Skip to content

Allow narrowing properties retrieved from types with index signatures #17567

Closed

Description

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'.

Activity

  1. added
    In Discussion
    Suggestion
    on Aug 2, 2017
  2. DanielRosenwasser commented on Aug 2, 2017

    @DanielRosenwasser
    Member

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

  3. changed the title [-]null checks don't work for indexed types[/-] on Aug 4, 2017
  4. added
    Committed
    and removed
    In Discussion
    on Nov 6, 2017
  5. mhegazy commented on Nov 6, 2017

    @mhegazy
    Contributor

    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.

  6. sandersn commented on Nov 8, 2017

    @sandersn
    Member

    Fix is up at #19838.

  7. added
    Fixed
    on Nov 8, 2017
  8. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedFixedSuggestion

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions