Utilizo transpileModule para insertar un campo en Page-data
, pero obtengo el resultado es un texto js, pierdo interface Account
y opt: string | any
¿Cómo puedo hacer para obtener ts text?
const ts = require('typescript') const factory = ts.factory const content = ` interface Account { id: number displayName: string version: 1 } Page({ data: { name: 'miser', }, }) Component({ data: { age: 18, }, }) function Component(opt: string | any) {} function Page(opt: any) {} ` const result = ts.transpileModule(content, { compilerOptions: {}, transformers: { before: [visitNodes], }, }) function visitNodes(ctx) { const visitor = node => { if ( ts.isPropertyAssignment(node) && node.name.escapedText === 'data' && node.parent.parent.expression.escapedText === 'Page' ) { const newNode = factory.createPropertyAssignment('haha', factory.createStringLiteral('ok')) node.initializer.properties.push(newNode) } return ts.visitEachChild(node, visitor, ctx) } return sourceFile => ts.visitNode(sourceFile, visitor) } console.log(result.outputText) // Page({ // data: { // name: 'miser', // haha: "ok", // }, // }); // Component({ // data: { // age: 18, // }, // }); // function Component(opt) { } // function Page(opt) { }