Nativescript: NS 3.0 Worker thread not compiling

Created on 16 Sep 2017  Â·  4Comments  Â·  Source: NativeScript/NativeScript

I'm migrating my NS 2.x project to NS 3.2.x - my Worker thread class is getting build errors.

Here's my dev env.

nativescript     │ 3.2.1
tns-core-modules │ 3.2.0
tns-android      │ 3.2.0
tns-ios          │

With node 6.11.3.

My worker thread ts file is

`require('globals');

global.onmessage = function( ev )
{
    postMessage( { } );
};`

I'm getting the following errors

error TS2339: Property 'onmessage' does not exist on type 'Global'.

error TS2346: Supplied parameters do not match any signature of call target.

The problem seems to be multiple definitions of postMessage() and the ts compiler is pickup up the wrong one. There are multiple definitions in dom.d.ts lib file

Also Global class doesn't define onmessage - but i can get around that by (<any>global).onmessage.

My tsconfig file is:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": true,
        "noEmitOnError": true,
        "lib": [
            "es6",
            "dom"
        ],
        "baseUrl": ".",
        "paths": {
            "*": [
                "./node_modules/tns-core-modules/*",
                "./node_modules/*"
            ]
        }
    },
    "exclude": [
        "node_modules",
        "platforms",
        "**/*.aot.ts"
    ]
}

To generate error - just create a template typescript blank project and then create a new worker thread .ts file as above.

* The underlying problem seems to be that Worker class is defined in both es6 and dom libraries and both are included in tsconfig.ts *

interface Worker extends EventTarget, AbstractWorker {
    onmessage: (this: Worker, ev: MessageEvent) => any;
    postMessage(message: any, transfer?: any[]): void;
    terminate(): void;
    addEventListener<K extends keyof WorkerEventMap>(type: K, listener: (this: Worker, ev: WorkerEventMap[K]) => any, useCapture?: boolean): void;
    addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
}
question

Most helpful comment

try

require('globals');

(<any>global).onmessage = function( ev )
{
(<any>global).postMessage( { } );
};

All 4 comments

try

require('globals');

(<any>global).onmessage = function( ev )
{
(<any>global).postMessage( { } );
};

Hi @erjdriver,
Does the solution of @triniwiz applicable in your case?

Yes works now thanks.

Maybe the docs should be updated.

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

Related issues

yclau picture yclau  Â·  3Comments

vtisnado picture vtisnado  Â·  3Comments

hshristov picture hshristov  Â·  3Comments

OscarLopezArnaiz picture OscarLopezArnaiz  Â·  3Comments

rLoka picture rLoka  Â·  3Comments