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

Language Service: TypeParameter.constraint is lazily calculated, but does not have an accessor function #5689

Open
fsoikin opened this issue Nov 16, 2015 · 4 comments
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@fsoikin
Copy link

fsoikin commented Nov 16, 2015

Because of this, I have to go through this awkward motion every time:

function getConstraint( type: ts.TypeParameter ) {
  // This call will cause the typechecker to resolve properties, as well as a bunch of other information
  // about the type (such as generic constraints), but we don't actually need its result right now.
  type.getProperties(); 

  return type.constraint;
}

For some members (such as "properties" - above), there is an accessor function, which I can use to be sure the data is up to date. Another category of members don't need an accessor function, because they are calculated eagerly, and so are "always there".

But there is this third category of members, which fall through the gap. One example is TypeParameter.constraint. Another is InterfaceTypeWithDeclaredMembers.declaredProperties. There are probably more, but I haven't come across them yet.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 3, 2015

Would exposing getConstraintOfTypeParameter on the API address the issue?

@mhegazy mhegazy added the API Relates to the public API for TypeScript label Dec 3, 2015
@fsoikin
Copy link
Author

fsoikin commented Dec 3, 2015

It would solve half the issue: now I can use that function to get the value while making sure it's calculated. But that is assuming that I know which function to use. If I was just exploring the API, I would see the constraint property and wouldn't think twice about just using it. It will be only after hours of debugging and googling (and possibly finding this issue) that I would gain the necessary knowledge.

So to solve the other half, the property needs to be hidden. But I'm not sure if it will break something somewhere.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 3, 2015

Agreed. so what we need to do is 1. expose the getConstraintOfTypeParameter on the TypeChecker interface, 2, add Type.getConstraint() method on the TypeObject class, and 3, mark type.constraint as internal.

3 is a breaking change, so not sure if/when we can do it. but other ones should be doable.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 3, 2015

PRs are definitely welcomed.

@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this labels Dec 3, 2015
@mhegazy mhegazy added this to the Community milestone Dec 8, 2015
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants