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

Questions: check if type is assignable to other #357

Closed
lazarljubenovic opened this issue Jul 9, 2018 · 4 comments · Fixed by #1517
Closed

Questions: check if type is assignable to other #357

lazarljubenovic opened this issue Jul 9, 2018 · 4 comments · Fixed by #1517
Labels
blocked compiler api Issue related to an issue in the TypeScript Compiler API enhancement

Comments

@lazarljubenovic
Copy link
Contributor

Need a bit of help here: how do I check if types assignable to one other?

Scenario 1: I have two nodes, I can get their types via getType(). How do I check if A is assignable to B and if B is assignable to A?

Scenario 2: I have one node, and I want to check if it's assignable to custom type that I will conclude based on a string (template in a framework). To give context, user does something like (let's assume Angular syntax for familiarity) *ngIf="foo". In my framework, ngIf doesn't exist runtime and there's no boolean type anywhere where I can getType it -- so how can I create it? What about more complex cases?

I was expecting some Type constructors, but browsing through .d.ts files gives me nothing. Also assignable gives no results. I see the docs mention the type checker but I don't see anything useful there either based on method names.

Any pointers would be appreciated. 😄

@dsherret
Copy link
Owner

dsherret commented Jul 9, 2018

Yeah, this would be very useful. We probably have to wait for this issue to be resolved in the TypeScript compiler API: microsoft/TypeScript#9879

In the meantime, you might have to roll with your own implementation :(

@lazarljubenovic
Copy link
Contributor Author

Wow, I was almost sure there was something in the TS API for this. How crazy would it to be to print two types, create a project with a single file, try the assignment and then type check the file? Do you think it would work?

@dsherret
Copy link
Owner

dsherret commented Jul 9, 2018

Haha! That would work. You could just create some temporary dummy file within your existing project then call dummyFile.delete() when you're done with it.

@dsherret dsherret added the compiler api Issue related to an issue in the TypeScript Compiler API label Jul 14, 2018
@cancerberoSgx
Copy link
Contributor

@dsherret @lazarljubenovic I'm doing just that to test a type utility library where I need to check for failures . With tools like tsd-check that run at compile time you cannot make the test fail (cannot leave compile errors in the project)... so I did basically what you mention:

https://github.com/cancerberoSgx/tsd-check-runtime/

BTW is based on ts-simple-ast, sorry, ts-morph. I added lots of features but still its execution time is acceptable ... and I'm creating a new project for each test and emitting diagnostics, each time,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked compiler api Issue related to an issue in the TypeScript Compiler API enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants