-
Notifications
You must be signed in to change notification settings - Fork 189
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
How to pass an ObjectLiteralExpression as an initializer to addVariableStatement? #320
Comments
Hey @derolf, yeah I've thought about that, but not sure what the best way would be at the moment. I still have to think about it a bit more. This is somewhat related to #148, but not exactly. What I will do in the short term is improve |
In v11 it's possible to use a writer when creating a variable declaration: sourceFile.addVariableStatement({
declarationKind: VariableDeclarationKind.Const,
declarations: [{
name: "myVar",
initializer: writer => {
writer.inlineBlock(() => {
writer.writeLine("bar: 1,")
.writeLine(`zoo: "test"`);
});
}
}]
}); I haven't tested that, but it should be a little easier to use than before. You could even extract that out to a reusable function and then write something like: Anyway, I'm going to keep looking into this and evaluate how I can make this easier because it definitely is a pain point. I just have to figure out how it would work. |
Hm, that’s kind of hack. Better would be to be able to pass a structured Initializer-Expression. |
Added import { WriterFunctions } from "ts-simple-ast";
// ...etc...
sourceFile.addVariableDeclaration({
name: "myVarDeclaration",
initializer: WriterFunctions.object({
key1: 5,
key2: "undefined",
key3: writer => writer.quote("someString"),
key4: undefined
})
}); Outputs: let myVarDeclaration = {
key1: 5,
key2: undefined,
key3: "someString",
key4
}; I'm not going to go with specific methods for doing this because it should be sufficient to use the writer function. |
Right now, VariableDeclarationStructure exposes initializer just as a string, but I want to construct something like:
The text was updated successfully, but these errors were encountered: