Skip to content

API: inconsistent Type#getProperties involving index signature and union type order #31565

@ajafff

Description

@ajafff

TypeScript Version: 3.4.5, 3.5.0-dev.20190523

Search Terms:

Code

type IndexFirst = {[x: string]: string} | {x: string};
type IndexLast = {p: string} | {[p: string]: string};

Expected behavior:

type.getProperties() to return an array of one symbol for each of the type alias declarations.

Actual behavior:

For IndexFirst type.getProperties() returns an empty array.
For IndexLast type.getProperties() returns an array of one symbol.

AFAICT this is caused by an early break in getPropertiesOfUnionOrIntersectionType:

// The properties of a union type are those that are present in all constituent types, so
// we only need to check the properties of the first type

That's not quite correct if there are index signatures. Iterating through the rest of the union constituents would fix this. getPropertyOfUnionOrIntersectionType already strips properties not present in all constituents. So this is probably a performance optimization and shouldn't simply be removed. Maybe we could stop after the first constituent that has no index signature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions