You can get a union with all array types using bracket notation. So:
type MyArr = [1, 2, 3, 4, 5];
// %inferred-type: 5 | 1 | 2 | 3 | 4
type MyArrUnion = MyArr[number];
See on Typescript playground.
Note that in this case the type of the array is static. If you need to get the type from an existing array, use the operator typeof
(in the context of static typing). Example:
const myArr = [1, 2, 3, 4, 5] as const; // Note que a asserção `as const` foi necessária para preservar o tipo estático que colocamos.
type MyArr = typeof myArr;
// %inferred-type: 1 | 2 | 3 | 4 | 5
type MyArrUnion = MyArr[number];
See on Typescript playground.
What if the inferred type of MyArr
was something like number[]
, MyArr[number]
would also work, only that would be inferred the type number
.
interface data{
 numbers: int[];
}
That’s what you want to know?– Ricardo Pontual
@Ricardopontual Não
– Nexus Prime
@Ricardopunctual I want it to take the array values and use as type
– Nexus Prime
but what kind? isn’t an array of integers? would that be
int[]
, or you want to turn the array into a string separates by pipe (|
) ?– Ricardo Pontual
@Ricardopunctual I want to take the array elements and type them, not the array itself
– Nexus Prime
not to do this in an interface... the array should already be of a certain type, as
int
orstring
, if you do not know what will come in the array only powerobject
, then on the interface should beany[]
, and who will type has to be the implementation, the class– Ricardo Pontual
@Ricardopunctual, actually, it does! :-) Behold.
– Luiz Felipe