I want to define just the interface and type for the class..
What am I doing?I am creating a React Native Turbo module which require me to pass interface which extends TurboModule
This is class looks for which I want to write interface (this interface should extend turboModule)
export default class Thread { constructor(jsPath) { if (!jsPath || !jsPath.endsWith('.js')) { throw new Error('Invalid path for thread. Only js files are supported'); } this.id = ThreadManager.startThread(jsPath.replace(".js", "")) .then(id => { DeviceEventEmitter.addListener(`Thread${id}`, (message) => { !!message && this.onmessage && this.onmessage(message); }); return id; }) .catch(err => { throw new Error(err) }); } postMessage(message) { this.id.then(id => ThreadManager.postThreadMessage(id, message)); } terminate() { this.id.then(ThreadManager.stopThread); }}
This is what I have written
export interface ThreadInterface { postMessage(msg: string): void; onMessage(msg: string): void; terminate(): void;}interface ThreadConstructor { new (path: string): ThreadInterface;}declare let ThreadInterface: ThreadConstructor;export interface ThreadsSpec extends TurboModule, ThreadInterface {}
Is this correct? also when I do this
class Thread implements ThreadInterface { constructor(jsPath:string) { }
here I have to mention jsPath as string, or else typescript consider it as any. I was expecting constructor to pick value from here:
new (path: string): ThreadInterface;
This is based out of this answer: https://stackoverflow.com/a/46971111/10433835