Issue
I have the following function signature:
const renderMessage = <T extends A | B >
(
a: T,
b: Map<T, Array<string>>,
c: T,
) => {
My problem is that I can call this function as follows:
type A = "A" | "B"
type B = "Z" | "Y"
let myA: A = "A"
let myB: B = "Z"
let myAMap: Map<A,Array<string>> = new Map(...)
let myBMap: Map<B,Array<string>> = new Map(...)
renderMessage(myA, myBMap, myA) // I want error here
renderMessage(myB, myAMap, my B) // I want error here
The reason for being able to do this is explained here. If I understood correctly, the structure of my Map object does not change based on T being A or B, so typescript won't complain. However, I am struggling to figure out how to enforce this.
EDIT
They are types and not classes. See Playground Link
Solution
The T type is too ambiguous for the TypeScript inference. It resolves to the most narrow type that satisfies the whole signature: A | B.
I guess you just have to provide it yourself between angle brackets:
Answered By - Guerric P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.