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.

Typescript class conversion silently fails when using ES6 imports

See original GitHub issue

Environment Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 6.5.0
  • Cross-platform modules:
  • Android Runtime: 6.5.0-2020-03-12-110223-01
  • iOS Runtime: 6.4.0
  • Plugin(s):

Describe the bug

When defining a class that extends a native class (in my case an Android service) and using an ES6-style import at the top of the file, the class does not get converted to Java and is not included in the /platforms folder. No error is given at bundling time.

To Reproduce

import { Utils } from '@nativescript/core';

@JavaProxy('com.belvederef.MyNotificationListener')
class MyNotificationListener extends android.service.notification
  .NotificationListenerService {
  constructor() {
    super();
    return global.__native(this);
  }

  onNotificationPosted(
    sbn: android.service.notification.StatusBarNotification,
  ): void {
    ...
    const context = Utils.android.getApplicationContext();
    context.sendBroadcast(intent);
  }

  ...
}

Expected behavior The class should be added to the platform folder or, if not possible to bundle with ES6-style imports, raise an error.

Additional context

I was able to solve the problem using node require() imports in my method as below:

  onNotificationPosted(
    sbn: android.service.notification.StatusBarNotification,
  ): void {
    ...
    const Utils = require('@nativescript/core').Utils;
    const context = Utils.android.getApplicationContext();
    context.sendBroadcast(intent);
  }
}

ES6-style imports are shown as working on the documentation when extending native classes. I am not sure whether the error is caused by the object destructuring in the import, but when I use the same import with destructuring somewhere else in the app and it works.

For searchability’s sake, I got to know about the error due to a java.lang.ClassNotFoundException raised at runtime.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
NathanaelAcommented, May 15, 2020

Just a follow up it seems to be a combination of both the vue version of the webpack config and something in the vue version of the TSConfig settings. I made a couple changes to the vue webpack config to take features from the straight ts webpack and then moved the tsconfig from the same app; and it built the JAVA code properly…

1reaction
belvederefcommented, May 13, 2020

Here is a barebone project that reproduces the error https://github.com/belvederef/nativescript--8573. Have a look at the file /app/services/MyNotificationListener.ts; if you run the project as is, it will not generate the Java file but when you remove import { Utils } from '@nativescript/core' and uncomment this line you can see it will.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In typescript, how can I import a plain javascript file with a ...
When I do this and try and run the typescript compiler, I get the following error: error TS7016: Could not find a declaration...
Read more >
Documentation - Migrating from JavaScript - TypeScript
Converting a JavaScript codebase over to TypeScript is, while somewhat tedious, usually not challenging. In this tutorial, we're going to look at how...
Read more >
Documentation - TypeScript 4.7
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
Read more >
Documentation - TypeScript 1.8
With TypeScript 1.8 it becomes possible for a type parameter constraint to ... like SystemJS wrap CommonJS modules and expose then as a...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
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