-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Proposal: Type Relationship API #9879
Comments
I was discussing something like this today with @novemberborn in that sometimes we aren't sure why the types are what they are at a particular point, especially when they are contextually inferred. Would it be possible to also consider potentially offering up additional meta data that would explain the type at a particular node (sort of like good ole SQL "explain plan")? |
can you elaborate on the scenario here? there are multiple ways the compiler can find a type. though, all is specified by the spec, the information is not persisted any ways. what we know is a type. |
Is there any progress on this? It would be incredibly useful to have. Just an |
As PR #9943 is closed now, is it possible to export only
also it will cover few closed issues, where authors were waiting for PR #9943 like: also at least partly it should cover this request: #11728 (comment) also it will close a few more closed by bot issues. |
As another use case, this would enable type checking HTML template systems such as lit-html. For this code: html`<date-picker .date=${currentDate}></date-picker>` We'd want to assert that This is applicable to many template systems. |
Would love to see some baby steps that could occur in this area. It seems like the tooling that would be enabled by having APIs like this would be tremendous! As @ahejlsberg pointed out, it's hard to know where to stop with these APIs, but I liked @weswigham's suggestion in the original pull request #9943 about separating the "type comparison" apis from the "type building" apis. It seems like the "type building" apis is a big task, but the type comparison apis might be a smaller effort? |
Just chatted with @octref about improving the custom element editing experience. I think the need for these APIs is showing up across the editing ecosystem as every template system provider eventually runs into the need to type check templates. He pointed out that the Vue LSP is working on implementing type checking of templates by doing code generation, running the typescript compiler over the generated code, then mapping diagnostics on that code back to the original source: https://github.com/vuejs/vetur/pull/681/files Having a type relationship API would make this sort of work simpler, faster, and more robust. |
Just trying to keep this alive 😅 It would be immensely useful to have at least the |
Just for visibility, I'm planning on having typescript-eslint take a dependency on the private |
I just merged #56448 which makes This obviously isn't the entire proposal in the thread (which contains many more relationships and getters), but it is likely the most valuable function given all of the feedback above. It also has the benefit of having been around for a very long time with the same name and behavior, so is a good option for those looking to just "start using it now". This issue is nowhere near closed, of course. Please do provide feedback for the rest! |
Is there any way to use I'm looking for a way to check that a type I'm maintaining is assignable to the type of a library (For a unified data model that is extensible) I simply want typescript to error if my provided type is not assignable to the library type It seems right now I can do this
But it's not typescript only and will polute the output |
You're effectively asking for #23689/#40468, but you can check this without any runtime emit using |
They type checker has no public API for checking type relationships or for looking up types in a given scope. This can make writing semantic-analysis informed lint rules very difficult (either because you need to replicate a lot of logic, or because you simply don't have enough information). Since tslint enabled semantic checks we now have a popular consumer who would very much like to be able to perform meaningful semantic comparisons (See this rule for instance).
Proposal
A small new set of public API methods on the type checker:
While this isn't a complete set of APIs for manipulating the type checker (I've left off an API to programatically create new types for comparison), it is enough to do meaningful analysis with existing types in the compilation without resorting to brittle (or often wrong) syntax-based solutions.
The text was updated successfully, but these errors were encountered: