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

Include inferred usages in 'find references' #12685

Closed
OliverJAsh opened this issue Dec 6, 2016 · 2 comments
Closed

Include inferred usages in 'find references' #12685

OliverJAsh opened this issue Dec 6, 2016 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Suggestion An idea for TypeScript

Comments

@OliverJAsh
Copy link
Contributor

type Child = { name: string }
type Parent = { children: Child[] }
const createParent = (): Parent => (
  { children: ['foo', 'bar']
    .map((name) => ({ name }))
  }
)

TS is type-checking Parent, which involves type checking the children property and thus each `Child.

When I select name inside my Array mapper function and invoke TS to find references, VSCode/TypeScript server does not include the original type definition of Child.name:

image

(See Child.name is not highlighted).

However, if I add the type annotation for Child, it does include it in the list of references:

type Child = { name: string }
type Parent = { children: Child[] }
const createParent = (): Parent => (
  { children: ['foo', 'bar']
    .map((name): Child => ({ name }))
  }
)

image

Would it be possible to make TypeScript include the references that are inferred instead of annotated, e.g. example 1 as well as example 2?

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Dec 16, 2016
@mhegazy mhegazy assigned ghost Dec 16, 2016
@mhegazy mhegazy added this to the Future milestone Dec 16, 2016
@ghost ghost added the Design Limitation Constraints of the existing architecture prevent this from being fixed label May 11, 2017
@ghost
Copy link

ghost commented May 11, 2017

The problem is that in (name) => ({ name }), the type of { name } isn't inferred to be Child. It's inferred to be { name: string }, a different type which happens to be assignable to Child. So it's not the case that ({ name }) is a Child, but it's assignable to one.

So find-all-references won't work because the name there is actually a different property, defined on a newly created type. When you explicitly specify : Child, that gives us a contextual type for the literal and we can find references correctly.

The real fix for this situation would be to provide contextual types even through generic calls, so that ({ name }) would be interpreted as a Child literal and not as its own type. See #15680.

@ghost ghost closed this as completed May 11, 2017
@ghost
Copy link

ghost commented May 27, 2017

#15680 is closed now but that didn't fix this. See #11152 and #3423 now.

@mhegazy mhegazy removed this from the Future milestone Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants