Nativescript: Execution of JS code stops when using Destructuring assignment(NativeScript + Angular)

Created on 21 Jun 2017  路  12Comments  路  Source: NativeScript/NativeScript

Please, provide the details below:

I am using TypeScript
When using the destructuring assignment in JS, any code below that line will not be executed.

  console.log('About to destructure an object')
  const user = { ...response, token: undefined }
  console.log('hi')

Compiled JS:

  var user = __assign({}, response, { token: undefined }); // Where is __assign defined?

Output:

JS: About to destructure an object

It's not just with console.log, simply any code is not executed, but no errors are thrown(on Android).

Fix with:

  console.log('About to destructure an object')
  const user = response
  user.token = undefined
  console.log('hi')

Compiled JS:

  var user = response;
  user.token = undefined;

Output:

JS: About to destructure an object
JS: hi // works

Did you verify this is a real problem by searching Stack Overflow and the other open issues in this repo?

Yeah, nothing there

Tell us about the problem

Cannot use destructuring assignment in code. Although there is an easy workaround, it is not very obvious. It took me a couple of hours to go back and forth to the faulty code attempting to fix my app.

Which platform(s) does your issue occur on?

On Android, the app DOES NOT throw any errors, but code does not get executed below that line
On iOS, the app will crash of segmentation fault(not sure if this is really caused by the destructuring assignment, might be an emulator bug) <- I'll get back to this.

Please provide the following version numbers that your issue occurs with:

  • CLI: 3.0.3
  • Cross-platform modules: 3.0.1
  • Runtime(s): 3.0.1 (Android + iOS)
  • Plugin(s): The issue is not related to a plugin

Please tell us how to recreate the issue in as much detail as possible.

To replicate my situation, paste in the code below into a button's (tap) event.

Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.

Simply copy this code, preferably in a button's (tap) event:

response is a response object from server. It contains some user data and the token:

const response = {
  name: 'Jane',
  last_name: 'Doe',
  token: 'ewqrewru0r7eyr87y87y43287r832yr8uye8uh82en8rxh8'
}
  console.log('About to destructure an object')
  const user = { ...response, token: undefined }
  console.log('hi')
medium

Most helpful comment

This seems more like a bug to me because we are not supporting TS built-in feature while we claim that {N} is TS first class citizen. Updating the labels.

All 12 comments

Hi @borislemke,
Thank you for reporting this issue.
I reviewed this case on my side and found that the issue is related to that the __assign method is not defined inside the global methods. Regarding that, I will log this as a new feature and you could keep track on it for further info.

In the meantime, as a temporary solution, you could set up in tsconfig.json file, noEmitHelpers option to false.

@tsonevn thanks for chiming in. I'm more concerned about the fact that no errors are being thrown.
Could you kindly explain why this is so?

Hi @borislemke,
While testing on my side this scenario with the above-given sample code on Button tap, the following error was logged in the console:

JS: About to destructure an object
JS: ERROR ReferenceError: __assign is not defined
JS: ERROR CONTEXT [object Object]

Then I was able to verify, that such a method is not defined in the globals.
Regarding that, not throwing an error could be caused in the cases when you are using an old CLI with new modules.

This seems more like a bug to me because we are not supporting TS built-in feature while we claim that {N} is TS first class citizen. Updating the labels.

Changing noEmitHelpers to false would break the webpack build. https://github.com/NativeScript/nativescript-dev-webpack/issues/14

Spread operator is actually a ES2015 feature. Hopefully it will be fixed soon.

One temporary workaround is to import this file in the beginning of main.ts and/or main.aot.ts

// __assign.ts

(<any>global)['__assign'] = (<any>global)['__assign'] || Object.assign || function (t: any) {
  for (let s, i = 1, n = arguments.length; i < n; i++) {
    s = arguments[i]
    for (const p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) { t[p] = s[p] } }
  }
  return t
}

I really wish NS can move faster into being stable soon, thanks team for ur work but we expect more!

The same case for __rest.

for anyone having this issue, I suggest taking a look at https://www.npmjs.com/package/nativescript-tslib

Thanks for noting that @edusperoni

I think the approach implemented in the plugin is a good candidate to be implemented directly into the tns-core-modules so that people will get it by default. We will have to add a dependency to https://github.com/Microsoft/tslib package.

@m-abs What do you think about that - are you keen on creating a PR with that. It will make the nativescript-tslib obsolete though.

@vakrilov
I'd love to make nativescript-tslib obsolete.

I'll get back to you with a PR soon.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings