electron/electron-quick-start-typescript

Sharing global object

Leonardonline opened this issue · 6 comments

The documentation shows how to share data between pages:

// In the main process.
global.sharedObject = {
  someProperty: 'default value'
}

but with Typescript version, we get this error:

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

interface SharedObject {
  someProperty: string;
};

declare const sharedObject: SharedObject = { ... }

Does it work for you?

I tried but it doesn't work, it doesn't get the data.

Here a repo where I replicate my error and @theJian solution, but unfortunately not working

https://github.com/Leonardonline/electron-quick-start-typescript

I solved using:

(<any>global).sharedObject = { 
  someProperty: string
};

@Leonardonline Thank you. I faced the same issue and your solution helped!

Create a global.d.ts file in your project.

declare namespace NodeJS{
    export interface Global {
        myVar: number
    }
}

This will merge the base NodeJS declarations with yours, so you can write global.myVar in your code. I couldn't figure out how to specify existing interfeces, so I use : any for my global settingsService