How to declare type of event parameter when listen for event
- Lingua principale
- TypeScript
- Stelle
- 11.9k
- Fork
- 488
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Hello,
I am using mitt 3.0.1 for my Nuxt app (nuxt 3.6.5). I use event-bus to emit/listen for the whole app:
File plugins/event-bus.ts
```
import mitt from 'mitt';
export default defineNuxtPlugin(() => {
const emitter = mitt();
return {
provide: {
event: emitter.emit, // Will emit an event
listen: emitter.on, // Will register a listener for an event
},
};
});
```
I am currently not able to declare event when I listen for event in my components. I have to use type "any" but this break out type safety rules.
```
$listen('component:display', (event: any) => {
// here the param I want to declare a specific type rather than type "any"
// like event: {id?: string; value?: string}
});
```
I already tried assigned a sepecific type but it throw error:
```
$listen('component:display', (event:{id?: string; value?: string}) => {
//
});
ERROR: No overload matches this call.
Overload 1 of 2, '(type: "*", handler: WildcardHandler>): void', gave the following error.
Overload 2 of 2, '(type: "component:display", handler: Handler): void', gave the following error.ts(2769)
```
It's now allowed to use flag @ts-ignore in my app so I don't know how to solve this now.
I search on document but it seems no mention about this so I hope to receive any advice on this.
Thank you so much.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia da plugins/event-bus.ts e dalla chiamata al listener di mitt mostrata nell’issue, quindi leggi le dichiarazioni TypeScript di mitt e la documentazione pertinente. Determina se la tipizzazione del parametro dell’evento segnalata può essere supportata o documentata per questo utilizzo. Il lavoro è completato quando l’approccio supportato o la limitazione sono riportati chiaramente e l’errore di tipo dell’esempio è stato risolto o spiegato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- nuxtjs, typescript
- Ambito
- developer-experience, frontend
- Tipo di issue
- Documentazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100