Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
How to enforce generic types typescript function

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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:

TypeScript playground

about 4 years ago · Juan Pablo Isaza Report

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!