ts-morph
ts-morph copied to clipboard
Show how to add factory generated statements
Is this the way to do it?
import { Project, ts, printNode } from "ts-morph";
const tsVariableStatement = ts.factory.createVariableStatement(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createVariableDeclarationList(
[ts.factory.createVariableDeclaration(
ts.factory.createIdentifier("resolvers"),
undefined,
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("Resolvers"),
undefined
),
ts.factory.createObjectLiteralExpression(
[ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("Profile"),
ts.factory.createObjectLiteralExpression(
[ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("name"),
ts.factory.createArrowFunction(
undefined,
undefined,
[ts.factory.createParameterDeclaration(
undefined,
undefined,
undefined,
ts.factory.createIdentifier("user"),
undefined,
undefined,
undefined
)],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createTemplateExpression(
ts.factory.createTemplateHead(
"",
""
),
[
ts.factory.createTemplateSpan(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("user"),
ts.factory.createIdentifier("firstName")
),
ts.factory.createTemplateMiddle(
" ",
" "
)
),
ts.factory.createTemplateSpan(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("user"),
ts.factory.createIdentifier("lastName")
),
ts.factory.createTemplateTail(
"",
""
)
)
]
)
)
)],
true
)
)],
true
)
)],
ts.NodeFlags.Const
)
)
sourceFile.addStatements([writer =>
writer.write(printNode( tsVariableStatement))
);
Any answers? I've successfully created the node, but cannot add using file.addVariableStatement(tsVariableStatement); 😞
@fivethreeo Did you ever figure out?
This is how far I got.