#Literal array inference
29 messages · Page 1 of 1 (latest)
not really possible
you can't go from an union to a tuple
in an union, the order doesn't matter, whereas it (technically) would in a tuple
pretty sure you can though
could use something like
type TupleUnion<U extends string, R extends any[] = []> = {
[S in U]: Exclude<U, S> extends never ? [...R, S] : TupleUnion<Exclude<U, S>, [...R, S]>;
}[U];
typeof poeple is a tuple, you can map it
more like: it's not a good idea
how come?
this
different types for different purposes
Pretty much I want to make a type in zod where it has to be a key of an object
This doesn't work:
game: z.enum(Object.keys(GAMES) as (keyof typeof GAMES)[]),
I was going to use .refine but I don't know how to get that keyof type inference after the schema is parsed
[number] is lossy, so don't do that
Preview:```ts
const people = [
{name: "john", age: 20},
{name: "jane", age: 30},
] as const
type NameArray<T extends readonly {name: string}[]> = {
[K in keyof T]: T[K]["name"]
}
type A = NameArray<typeof people>
// ^?```
A more extensible version, if you want:
Preview:```ts
const people = [
{name: "john", age: 20},
{name: "jane", age: 30},
] as const
type TupleProps<
T extends readonly {[K in P]: string}[],
P extends string
= {[K in keyof T]: T[K][P]}
type A = TupleProps<typeof people, "name">
// ^?```
Of course, if you also want to deduplicate, then U2T is your friend:
Preview:```ts
import type {U2T} from "type-collections"
const people = [
{name: "john", age: 20},
{name: "jane", age: 30},
{name: "john", age: 30},
] as const
type TupleProps<
T extends readonly {[K in P]: string}[],
P extends string
= U2T<T[number][P]>
...```
i thought that was impossible
then u got u2t
typo, fixed