Skip to content

[Bug]: Typescript plugin fails on named tuple positions where the name is a reserved word in JS #13702

@happycollision

Description

@happycollision

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

type FuncWithDescription = [
  function: (...args: any[]) => any,
  string: string
]

https://babeljs.io/repl#?browsers=defaults&build=&builtIns=false&corejs=3.6&spec=false&loose=false&code_lz=C4TwDgpgBAYgrgOwMYHUCWwAWARCBnJAJzTGDQHsEoBeKAbQCgooAzRJMygLigAoA6QQENCAczw8hCEHQC6AShoA-KFJAAaJlDzBiCUTx17RDWQwZJKO1gnRZcBYqQoIJsdnZz4iJTgjk09FrMdLwAHlwIcAC2AEYQhIrUKmHqUABEhBDAcIRUWNBRcQnpsprMIeGRMfGEaSDVxYnKUGEAVBoZ0XAANmRgPSBQBVBFtXilpkA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2%2Ctypescript&prettier=false&targets=&version=7.15.3&externalPlugins=&assumptions=%7B%7D

Configuration file name

No response

Configuration

See REPL link

Current and expected behavior

Current

It throws at line 2: /repl.tsx: Unexpected token (2:2)
If you rename function to fn, then
it throws at line 3: Tuple members must be labeled with a simple identifier. (3:2)

Expected

It does not throw an error on either line. Why would I expect this? Because TS is totally fine with this same code: https://www.typescriptlang.org/play?ts=4.0.2#code/C4TwDgpgBAYgrgOwMYHUCWwAWARCBnJAJzTGDQHsEoBeKAbQFgAoKVqAM0STMoC4oAFADoRAQ0IBzPP1EIQdALoBKagD4oskABpmbKHmDEEE-gaMTmC5sySUDHBOiy4CxUhQTTYXJznxESHgRFGnpdNjoBAA9eBDgAWwAjCEIlGnUorSgAIkIIYDhCKixoOKSU7IUdFgjo2ITkwiyQevLU9KgogCptHPi4ABsyMAGQKBKoMsa8SssgA

Environment

See REPL link for env details

Possible solution

There are two ways to fix this problem, in my opinion

Option 1: Updated docs, and better error messages

Indicate in the docs (just like with other caveats) that there are certain tokens that are not acceptable for named tuple positions.

Then, ensure that the errors thrown in my example above clearly state this problem and/or reference the newly updated docs. I think the second error (Tuple members must be labeled with a simple identifier.) is possibly acknowledging this discrepancy, but it is not clear to me that the error is a known caveat vs an actual TS syntax issue. (The first error thrown against the word function, though, is quite vague.)

Option 2: Update the TypeScript plugin to handle these names

I do not know how many words fail in the Babel plugin, and yet succeed in TS. I do know that the following are in the list:

  • function
  • string
  • number

Additional context

No response

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions