Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"__assign is not defined" - NativeScript Object Spread Woes

I was making good progress with a native-script project until this happened:

JS: EXCEPTION: Uncaught (in promise): ReferenceError: __assign is not defined

This is bubbling up from this line of code:

return [...state, { ...action.payload, success: false }];

Here's my tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": true,
        "noEmitOnError": true
    },
    "exclude": [
        "node_modules",
        "platforms",
        "**/*.aot.ts"
    ]
}

Typescript doesn't seem to be including it's helper __assign function in the compiled source - which is their way of implementing the object spread syntax. Would any of you fine people happen to know why?

like image 372
Ian Haggerty Avatar asked Feb 16 '17 03:02

Ian Haggerty


1 Answers

I'm happy to report I found the solution to this. This GitHub repo explains things quite nicely, but here's a quick rundown:

The flag noEmitHelpers in tsconfig.json tells Typescript to omit these 'helpers' (such as __assign) in every file that needs them.

{
  "compilerOptions": {
    // changing this to false does the job, but duplicates helpers across every file
    "noEmitHelpers": false
  }
}

The latest Typescript offers a better way to manage this, using the flag importHelpers (see compiler options):

{
  "compilerOptions": {
    "noEmitHelpers": true,
    "importHelpers": true // better
  }
}

This'll get object spread working, and avoid code duplication across files.

You might also need to npm install tslib --save to stop IDE errors.

like image 165
Ian Haggerty Avatar answered Oct 23 '22 21:10

Ian Haggerty