In TypeScript, is there a way to define a generic parameter that extends only one value of a union type?
For example, assuming I declare a union type as follows:
type Any = "A" | "B"
then if I use the type in a function definition as follows:
const fn = <T extends Any>(arg: T[]) => {}
then the args argument can be an array that contain the values of both "A" and "B"; for example, this would be valid:
let x = fn(["A", "B"])
which defeats the very purpose of using the generic parameter in the function definition (i.e., to constrain the values in args argument array to only one specific type)
Of course, I could define the function as follows:
const fn = (arg: "A"[] | "B"[]) => {}
But if the number of component types in the union is large, this may be impractical