Reputation: 2497
I know there are similar questions, but I don't get it.
How can I configure the types to have inputType
= outputType
?
public addReadableTime(message: PublicMsg | PrivateMsg): PublicMsg | PrivateMsg {
message.displayTime = moment(message.lastModified).format('HH:mm');
return message;
}
// ...
const publicMsg = this.addReadableTime(publicMsg);
TS2322: Type 'PublicMsg | PrivateMsg' is not assignable to type 'PublicMsg'. Property 'publicChannelMessageId' is missing in type 'PrivateMsg' but required in type 'PublicMsg'.
Upvotes: 0
Views: 38
Reputation: 101758
Seems like what you need is a generic method with a type constraint:
public addReadableTime<T extends PublicMsg | PrivateMsg>(message: T): T {
message.displayTime = moment(message.lastModified).format('HH:mm');
return message;
}
// ...
const publicMsg2 = this.addReadableTime(publicMsg);
Upvotes: 2