babel / babel/babel

[Bug]: Typescript parser crash on `as`-style assertion on generic instantiation expression

Open
#15,990 4 comments 0 reactions 0 assignees View on GitHub
area: typescript pkg: parser
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

### How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

### Input code

```ts
// produces a parser error, but is valid TS
const x = jsonParse as (str: string) => KeyType
```

`jsonParse` is an [instantiation expression](https://dev.to/pffigueiredo/2-new-killer-features-coming-to-typescript-3b5j#after-47), which is followed by a cast. As also shown in the repl, the same code works when the cast is written with angle-bracket syntax.

See REPL: https://babel.dev/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=PTAEAcCcHsBMFcDGBTAzqAhhDlXMqPjJAFCLQB2qALqAB6gC8oAVqpQAo54A8AytAC2yACoBPcMgB8mdAAoakAFyhFASwoBzAJRMZAaWRjxkkiRCgA7tEgBrdADMNyMpRr0moHgurLVvjR09UENjCWk2Tm5kfiFRcKkgA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=typescript&prettier=false&targets=&version=7.22.20&externalPlugins=&assumptions={}

### Configuration file name

_No response_

### Configuration

See REPL.

### Current and expected behavior

The code in the REPL (i.e. `const x = jsonParse as (str: string) => KeyType`) should parse successfully, as it's valid TS.

### Environment

System:
OS: macOS 13.5.1
Binaries:
Node: 20.0.0 - ~/.nvm/versions/node/v20.0.0/bin/node
npm: 9.6.4 - ~/.nvm/versions/node/v20.0.0/bin/npm

### Possible solution

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported TypeScript parser error with the linked Babel REPL and the input `const x = jsonParse as (str: string) => KeyType`. Compare it with the working angle-bracket assertion form; done means the `as`-style assertion parses successfully as valid TypeScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.