Home > OS >  Get values of Generic argument extending array
Get values of Generic argument extending array

Time:07-21

Is it possible to retrieve the values of a generic type that extends, e.g., string[]? My use case is to check whether a given value is included in this "array":

function isC<C extends string[]>(attribute: string): attribute is C[number] {
  const valueSet: Set<string> = new Set(C)
  return valueSet.has(attribute)
}

CodePudding user response:

TypeScript's type system is erased when TypeScript is compiled to JavaScript, and the JavaScript is what actually runs. Your function therefore compiles to

function isC(attribute) {
    const valueSet = new Set(C); // <-- this C doesn't exist
    return valueSet.has(attribute);
}

And that's the problem the compiler is warning you about. There is no such thing as C in your JavaScript code. C is just a type, and that type is not present at runtime. In order for this to make any sense at runtime, you need to have a value of type C. Possibly like this:

function isC<C extends string[]>(attributes: [...C], attribute: string): attribute is C[number] {
    const valueSet: Set<string> = new Set(attributes)
    return valueSet.has(attribute)
}

And that will work:

let attr = Math.random() < 0.5 ? "b" : "d";
if (isC(["a", "b", "c"], attr)) {
    console.log({ a: 1, b: 2, c: 3 }[attr]);
} else {
    console.log("nope");
}

Note, though, that the particular implementation can probably be tweaked. There's no reason why you need to keep track of the actual array type, you just want the string literal types of the elements. And putting all elements from the array into a Set and then doing a has() is overkill, since you can stop inspecting the array as soon as you find the element in question. Therefore I'd suggest:

function isC<C extends string>(cArray: C[], attribute: string): attribute is C {
    return (cArray as readonly string[]).includes(attribute);
}

Which also works.

Playground link to code

  • Related