7

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?

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Ian Haggerty
  • 1,741
  • 16
  • 22

2 Answers2

15

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.

Ian Haggerty
  • 1,741
  • 16
  • 22
  • Did you know change to es6? Could you show us the whole tsconfig? – René Stalder Feb 28 '17 at 12:51
  • It shouldn't be necessary to change to es6. Object spread is easily implemented in es5. – Ian Haggerty Mar 15 '17 at 14:20
  • 1
    To clarify this, because your comment seems a bit misleading to me: Object Spread is a es6/es2015 feature. But since the target in the tsconfig defines the output, not the input, it doesn't matter. What matters is, which libraries you define in the library property of the tsconfig. In my case, I had to add "es6" and the correct typing reference in my references.d.ts. – René Stalder Mar 15 '17 at 16:26
0

Have you tried adding "lib": "es6" to your tsconfig?

bcherny
  • 3,072
  • 2
  • 27
  • 35
  • That flag threw a load of compile-time errors for me: `node_modules/@angular/platform-browser/src/browser/browser_adapter.d.ts(14,16): error TS2304: Cannot find name 'Node'. ` – Ian Haggerty Feb 16 '17 at 11:12
  • Sounds like you want the DOM lib too - `"lib": ["es6", "dom"]` – bcherny Feb 16 '17 at 17:22
  • I did try some variations of the `"lib"` flag. Nativescript doesn't seem to like any of them (at least the seed project I used). I'm fairly sure you don't need any flags to include `__assign` - the typescript playground generates it quite happily. It's a problem with the module system - I guess it doesn't make sense to generate `__assign` for every file that needs it - just one for the compiled output. No idea how this trickles down to native-script though. – Ian Haggerty Feb 16 '17 at 19:43
  • Would need to see you full project to be more helpful. Can you link to GitHub? – bcherny Feb 17 '17 at 00:47