const A= "Hello"
const B= "Bye"
const C="Foo"
const myArrayOfConstants=[A,B,C]
type ConvertArrayToUnion<myArrayOfConstants>
==> Expected type output: "Hello" | "Bye" | "Foo".
I tried with
type ConvertArrayToUnion<T extends readonly string[]>= T[number]
But in this case I am not passing an array of strings, but the variable name of each constant (which I guess it is a tupple?) so it doesn't work.
CodePudding user response:
Try like this:
const A= "Hello"
const B= "Bye"
const C="Foo"
const myArrayOfConstants=[A,B,C] as const;
type test = typeof myArrayOfConstants[number]
CodePudding user response:
You could use as const
and typeof
const A= "Hello";
const B= "Bye";
const C= "Foo";
const myArrayOfConstants = [A, B, C] as const;
type MyType = typeof myArrayOfConstants[number]; // "Hello" | "Bye" | "Foo"
CodePudding user response:
const convert = myArrayOfConstants as const;
// type UnionType = "Hello" | "Bye" | "Foo"
type UnionType = typeof convert[number];
hope it can help