question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional chaining failing with `"target": "esnext"` in `tsconfig` -- Rollup doesn't understand this syntax yet

See original GitHub issue

What happens and why it is wrong

https://github.com/brandon-leapyear/rollup-optional-chaining-repro

Copied from README:

yarn
yarn rollup -c

Uncomment the "target": "esnext" line in tsconfig.json to show the error:

index.ts: (3:57)
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
index.ts (3:57)
1: import React from 'react'
2: 
3: const a: { b?: string } = {}
                               ^
4: 
5: export default () => <span>{a?.b}</span>
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:5351:30)
    at Module.error (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:9643:9)
    at tryParse (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:9552:16)
    at Module.setSource (/Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:9868:33)
    at /Users/bchinn/Desktop/rollup-example/node_modules/rollup/dist/rollup.js:12148:20
    at async Promise.all (index 0)
    at async Promise.all (index 0)
    at async Promise.all (index 0)

Seems to be a rollup-plugin-typescript2 bug because with "target": "esnext" uncommented, yarn tsc --noEmit still works (e.g. Typescript parses it fine).

Environment

Shouldn’t be relevant

Versions

  • typescript: latest
  • rollup: latest
  • rollup-plugin-typescript2: latest

rollup.config.js

import typescript from 'rollup-plugin-typescript2'

export default {
  input: 'index.ts',
  output: [
    {
      file: 'dist/index.js',
      format: 'es',
    },
  ],
  plugins: [
    typescript({
      typescript: require('typescript'),
    })
  ],
}

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "jsx": "react",

    // Uncomment to break:
    // "target": "esnext",
  }
}

package.json

{
    "name": "rollup-optional-chaining-repro",
    "version": "0.0.1",
    "private": true,
    "devDependencies": {
        "rollup": "^1.27.0",
        "rollup-plugin-typescript2": "^0.25.2"
    },
    "dependencies": {
        "typescript": "^3.7.2"
    }
}

plugin output with verbosity 3

plugin-debug.txt

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ezolenkocommented, Jun 22, 2022

@dagda1 this is not a bug – this is typescript generating correct code (for esnext target) that rollup can’t quite consume. You can add babel plugin into the chain if you must have esnext.

2reactions
ezolenkocommented, Nov 25, 2019

Typescript transpiles a?.b to (_a = a) === null || _a === void 0 ? void 0 : _a.b when es2015 (default) is used.

It transpiles a?.b to a?.b when esnext is used. I guess this is a native JS feature in esnext,

The error you see is rollup failing to parse this. (not sure if they are supposed to support it or not).

You can verify by running tsc, getting it to emit index.js and then using js file as rollup input. You should get the same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional Chaining Causes Build to Fail · Issue #205 - GitHub
I've done some investigation and I think that Rollup/acorn is having trouble parsing the new optional chaining syntax. If you set target: ...
Read more >
How to get optional chaining working in TypeScript?
The problem is you are targeting esnext this will tell the compiler to output all language features as is without any transpilation.
Read more >
babel/plugin-transform-typescript
This plugin adds support for the types syntax used by the TypeScript programming ... supports certain JavaScript proposals such as optional chaining (...
Read more >
TSConfig Reference - Docs on every TSConfig option
The value of extends is a string which contains a path to another configuration file to inherit from. The path may use Node.js...
Read more >
TypeScript rules for Bazel - bazelbuild/rules_nodejs
A unique name for this target. deps. Optional list of labels. Default: []. Additional tsconfig.json files referenced via extends. src.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found