Home > Software design >  How to enforce generic types typescript function
How to enforce generic types typescript function

Time:01-02

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

CodePudding user response:

The reason to have different classes - A and B - should be if one type of instance should hold differently structured data from the other, or if one type of instance should have different methods. If the data is structured the same way, and the methods are all the same as well, then there shouldn't be separate classes.

If you distinguish A and B in any way, the error will arise as desired:

class A{
  x = 'x'
}
class B{
  y = 'g'
}
const renderMessage = <T extends A | B > 
(
  a: T,
  b: Map<T, Array<string>>, 
  c: T,
) => {
}

let myA: A = new A()
let myB: B = new B()
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, myB) // I want error here

CodePudding user response:

The T type is too ambiguous for the TypeScript inference. It resolves to the most narrow type that makes the whole signature work: A | B.

I guess you just have to provide it yourself between angle brackets:

TypeScript playground

  • Related