Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

59
Vistas
Get values of Generic argument extending array

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)
}
6 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

6 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.