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

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

Closed
ajafff opened this issue May 23, 2019 · 3 comments · Fixed by #31979
Closed

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

ajafff opened this issue May 23, 2019 · 3 comments · Fixed by #31979
Labels
Bug A bug in TypeScript
Milestone

Comments

@ajafff
Copy link
Contributor

ajafff commented May 23, 2019

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.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jun 13, 2019

If there isn't a way to observe this during batch compilation or interactive scenarios, I would just call this a quirk of the API. It's not clearly a defect (i.e. wouldn't accept a PR to "fix" this with any negative perf implications).

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 13, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 13, 2019
@ajafff
Copy link
Contributor Author

ajafff commented Jun 18, 2019

wouldn't accept a PR to "fix" this with any negative perf implications

@RyanCavanaugh
As you already mentioned this is not observable via batch compilation or language service. From the compilers POV this is dead code as it's never hit by any of the tests. For API users this function is not usable with this "quirk" as I cannot synthesize Symbols for union types.
Fixing this by iterating through all types with index signature will not have a perf impact since there's not way a user could ever notice it.

@ajafff
Copy link
Contributor Author

ajafff commented Jun 19, 2019

Turns out this bug affects property access completions. I added a test case in PR #31979

sandersn added a commit that referenced this issue Mar 13, 2020
…ature (#31979)

* getPropertiesOfUnionOrIntersectionType: handle types with index signature

Fixes: #31565

* fix test

* more testing

* fix typo in checker.ts

Co-authored-by: Nathan Shively-Sanders <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants