The company I work at has some code that looks something like this:
interface Command {
action: string
}
interface Notification {
event: string
}
type Message = Command | Notification;
function func(type: string, callback: (msg: Message) => void) {
// ...
}
func("command", (c: Command) => console.log(c.action) )
The second argument of the function call on the last line is underlined with the following type error:
Argument of type '(c: Command) => void' is not assignable to parameter of type '(msg: Message) => void'.
Types of parameters 'c' and 'msg' are incompatible.
Type 'Message' is not assignable to type 'Command'.
Property 'action' is missing in type 'Notification' but required in type 'Command'.ts(2345)
(The third line of this error seems to be the reverse of the first line.) The thing is that in this case I know that the argument given to the callback will be a Command and not anything else. Is there a way to assert this to remove the error?