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

Compiler API: Creating a TypeNode from a string? #23042

Closed
ffMathy opened this issue Mar 31, 2018 · 6 comments
Closed

Compiler API: Creating a TypeNode from a string? #23042

ffMathy opened this issue Mar 31, 2018 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ffMathy
Copy link

ffMathy commented Mar 31, 2018

In TypeScript, how can I turn the string { [index: string]: { foo: string } } into a TypeNode in the compiler API? Is that possible?

I already tried doing this by creating a "file" and putting the contents into it and parsing it, but that doesn't seem to be creating a TypeNode.

Any ideas?

@ffMathy ffMathy changed the title Compiler API: Creating a TypeNode from a string? Compiler API: Creating a TypeNode from a string? Mar 31, 2018
@ajafff
Copy link
Contributor

ajafff commented Mar 31, 2018

I usually create a string with a variable declaration with the type I want to parse and parse it as a SourceFile:

ts.createSourceFile('tmp.ts', `let a:${type}`).statements[0].declarationList.declarations[0].type;

@ffMathy
Copy link
Author

ffMathy commented Mar 31, 2018

Is there a cleaner way to do it? I think that feels pretty dirty.

@ffMathy
Copy link
Author

ffMathy commented Apr 1, 2018

@ajafff your solution also produces an unexpected result when emitted.

{ [key: number]: number; } as a string turned into a TypeNode and then emitted turns into the code { [: number]: number; } .

Note how the "key" name is lost. How come? Any way to get around this?

@ajafff
Copy link
Contributor

ajafff commented Apr 1, 2018

When emitting a transformed file, you always need to pass the original SourceFile object. This is then used to look up the content of not modified nodes.
In your case you probably try to combine nodes from different SourceFiles. That will always go wrong.
You need to transform that AST subtree from the other SourceFile to create synthesized nodes (ts.createXXX) from at least all literals and identifiers.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 3, 2018

Seems related to #9883

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Apr 3, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants