when I define (in typescript) custom type, it correctly whisper, when i assign to variable of that type. For example in my code below, when I define variable "error", typescript correctly whisper, that I must add "message" or "code" (as I defined type IErrorType, that it must have at least one of that). But when I use it in function parameter, like in my "printError()", that in function it gives me error: "Property 'message' does not exist on type 'IErrorType'."
But it exists! How can I solve it? I need type in which you must type at least one property from one interface or from another (or from both)...My code is minimal working example, so you can run it...
type IErrorType = IErrorCode | IErrorMessage;
interface IErrorCode {
code: number;
}
interface IErrorMessage {
message: string;
}
let error: IErrorType = {
code: 5
}
let error2: IErrorType = {
message: "Error!!!"
}
let error3: IErrorType = {
message: "Error 7!!!",
code: 7
}
function printError(err: IErrorType){
if(err && err.message){
console.log(err.message);
}
}
printError(error3);
printError(error2);
printError(error);
Thanks!