Skip to content

Babel's TypeScript support doesn't match the compiler w.r.t. inline declarations of in-scope identifiers #10353

@ELLIOTTCABLE

Description

@ELLIOTTCABLE

Bug Report

Current Behavior
When encountering a declare statement for an identifier that's already in scope, Babel throws up. The upstream TypeScript compiler understands this syntax.

{ SyntaxError: /Users/ec/Sync/Code/excmd/src/interface.ts: Identifier 'Something'
    has already been declared (3:14)

   1 | import Something from './somewhere.js'
   2 | 
>  3 | declare class Something {
     |               ^
   4 |    // ...
   5 | }
    at Object.raise (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:6325:17)
    at TypeScriptScopeHandler.checkRedeclarationInScope (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:3759:12)
    at TypeScriptScopeHandler.declareName (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:3725:12)
    at TypeScriptScopeHandler.declareName (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:3835:11)
    at Object.checkLVal (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:8021:22)
    at Object.checkLVal (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:5727:15)
    at Object.parseClassId (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:10855:14)
    at Object.parseClassId (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:5508:11)
    at Object.parseClass (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:10572:10)
    at Object.tsTryParseDeclare (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:4984:21)
  pos: 1216,
  loc: Position { line: 3, column: 14 },
  code: 'BABEL_PARSE_ERROR' }

Input Code

import Something from './somewhere.js'

declare class Something {
   // ...
}

Expected behavior/code
As with tsc, this should have no effect on the produced code; these declarations are essentially "inline .d.ts" for non-TypeScript imports. To satisfy the current Babel, I guess I'll need to extract these declarations into additional .d.ts files, and check those into my source-control; but as I only have a single TypeScript file in my codebase, I'd really prefer to keep the typings for other modules centralized to that single TypeScript file!

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

// .babelrc
{
   "presets": ["@babel/env", "@babel/preset-typescript"]
}

// CLI invocation
babel --verbose src --extensions '.ts,.js' --ignore '**/*.bs.js' --ignore '**/*.d.ts' --copy-files --out-dir lib

Environment

  • Babel version(s): 7.5.5 (@babel/core 7.5.5)
  • Node/npm version: v10.12.0
  • OS: macOS 10.14.5
  • How you are using Babel: cli

Additional context/Screenshots
The above example is somewhat massaged / simplified from my actual codebase I can't even get the web-playground (with "TypeScript" enabled!) to parse a simple declare class Blah, but I would have included a working example if I could.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRarea: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions