I'm currently trying to improve the types on some existing code. My code looks roughly like this:
/* dispatcher.ts */
interface Message {
messageType: string;
}
class Dispatcher<M extends Message> {
on<
MessageType extends M["messageType"],
SubMessage extends M & { messageType: MessageType }
>(
messageType: MessageType,
handler: (message: SubMessage) => void
): void { }
}
/* messages.ts */
interface AddCommentMessage {
messageType: "ADD_COMMENT";
commentId: number;
comment: string;
userId: number;
}
interface PostPictureMessage {
messageType: "POST_PICTURE";
pictureId: number;
userId: number;
}
type AppMessage = AddCommentMessage | PostPictureMessage;
/* app.ts */
const dispatcher = new Dispatcher<AppMessage>();
dispatcher.on("ADD_COMMENT", (message: AddCommentMessage ) => {
/* ^^ REMOVE THIS TYPE HINT!*/
console.log(message.comment);
});
I'd like to remove the need to explicitly narrow the type of the message passed to the message handler (where /*REMOVE THIS TYPE HINT!*/
is), such that it correctly narrows to the type that has a matching messageType
type (e.g. if messageType
is "ADD_COMMENT"
then message
should be AddCommentMessage
).
If this is not possible right now please let me know. I'm under the impression that it isn't but I'm not quite sure.