Skip to content

Type annotation is stripped from Typescript template  #10636

@dentrado

Description

@dentrado

Bug Report

Current Behavior
A @babel/template template with placeholders and a type annotation generates code without the annotation. A template without placeholders works as expected.

Input Code

const template = require('@babel/template').default
const generate = require('@babel/generator').default
const t = require('@babel/types')const withType = template({plugins:['typescript']}).ast`
    const greeting: string = 'Hello';
`
console.log(generate(withType).code)
// => "const greeting: string = 'Hello';"const typeDisappears = template({plugins:['typescript']}).ast`
    const ${t.identifier('greeting')}: string = 'Hello';
`
console.log(generate(typeDisappears).code)
// => "const greeting = 'Hello';"

Expected behavior/code

I expect both templates above to keep the : string type in the generated code.

Babel Configuration (.babelrc, package.json, cli command)

No configuration, used from code, see above.

Environment

  • Babel version(s): 7.6.4
  • Node/npm version: Node 12.9.1 / yarn 1.17.3
  • Monorepo: no

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: template

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions