0

I have a Node script with Typescript, where i want to extract an object (from a file), that occasionally is null. Therefore i want to use operational chaining (or some other operator) that allows me to have a fallback to null if the object does not exists

     const {
        type: jackpotType,
        contribution: jackpotContribution,
        contributionEnable: jackpotContributionEnable,
      }: {
        type: string | null;
        contribution: number | null;
        contributionEnable: boolean | null;
      } = entry.data?.jackpot;

when i compile i get this error: SyntaxError: Unexpected token .

I have checked my node version (recently upgraded), and it should be compatible.

$ node --version // v15.8.0

and in my tsconfig.json I have the following settings

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "ESNEXT" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    // "lib": [],              
....
Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69

1 Answers1

2

Change your target field to ES2020 which is suitable to Node v14.

{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["ES2020"],
    "moduleResolution": "node",
    "module": "commonjs"      
....

Based on What TypeScript configuration produces output closest to Node.js 14 capabilities?

felixmosh
  • 32,615
  • 9
  • 69
  • 88