Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

111
Vistas
How need I do to get ts text when I use transpileModule API

I use transpileModule to insert a field into Page-data, but I get result is a js text, miss interface Account and opt: string | any. How can I do to get 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) { }
7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.