Using webext-bridge I can define my listeners like
// shims.d.ts
declare module 'webext-bridge' {
export interface ProtocolMap {
// define message protocol types
// see https://github.com/antfu/webext-bridge#type-safe-protocols
'event-name': { helloMessage: string };
}
}
// code
onMessage('event-name', (message) => {
// do something strict way
console.log(message.data.helloMessage); // no ts error
});
How I can define the method for second param like
function onEventName(message: /* How to get message type dynamically here? */) {
console.log(message.data.helloMessage); // ts not able to get type here
}
onMessage('event-name', onEventName);
CodePudding user response:
It can be defined as:
import { onMessage, ProtocolMap, IBridgeMessage } from 'webext-bridge'
function onEventName(message: IBridgeMessage<ProtocolMap['event-name']>) {
console.log(message.data.helloMessage);
}
onMessage('event-name', onEventName);