You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is more of a Typescript question but how do I strongly-type Worker?
Here's a minimal reproduction:
interfaceJobData{id: string}interfaceFooDataextendsJobData{foo: string}interfaceBarDataextendsJobData{bar: boolean}typeFooWorker=Worker<FooData,string,'foo'>typeBarWorker=Worker<BarData,boolean,'bar'>constfooBarWorker: FooWorker|BarWorker=newWorker('worker',async(job)=>{switch(job.name){case'foo':
console.log(job.data.foo)// Error: Property `foo` does not exists on type `JobData`returnjob.data.foocase'bar':
console.log(job.data.bar)// Error: Property `bar` does not exists on type `JobData`returnjob.data.bar}})
Using & instead of | somehow works but only for one type of worker
const fooBarWorker: FooWorker & BarWorker = new Worker('worker', async (job) => {
switch (job.name) {
case 'foo':
console.log(job.data.foo)
return job.data.foo
// Error: Type 'bar' is not comparable to type 'foo'
case 'bar':
console.log(job.data.bar)
return job.data.bar
}
})
This is more of a Typescript question but how do I strongly-type Worker?
Here's a minimal reproduction:
Using
&
instead of|
somehow works but only for one type of workerRelated: #1156
Minimal reproduction: https://codesandbox.io/s/typescript-playground-export-forked-cmr7k2?file=/index.ts
The text was updated successfully, but these errors were encountered: