#How to do Array of not optional X Types

10 messages · Page 1 of 1 (latest)

earnest tapir
#

How to do Array of not optional X Type

Probably there i have to use satisfies but i dont know how

export type TpkgManagerSupport = "npm" | "yarn" | "pnpm" | "bun";
export const pkgManagerSupportWrong: TpkgManagerSupport[] = ["npm", "pnpm", "bun"] as const; // should crash - no "yarn"
export const pkgManagerSupportCorrect: TpkgManagerSupport[] = ["npm","yarn", "pnpm", "bun"] as const; // Fine, has everypossible type of Tpkg...

Ping me in answer

shut crystal
#

@earnest tapir There isn't really a way to do this - an array's type says what can be in it, not what must be in it.

#

The most common appraoch is to define the array and then derive the type from it.

#
export type TpkgManagerSupport = (typeof pkgManagerSupport)[number]
export const pkgManagerSupport = ["npm","yarn", "pnpm", "bun"] as const; 
earnest tapir
#

Oh, i thought it's possible with satisfies to do this.

Can you explain me how works (typeof pkgManagerSupport)[number] ? Why with [number] it's not a array

#

it's like "simulating" output with OurArray[index] ?

#

If you know where it would be, can you send me link to docs to that feature?

shut crystal
#

There's an example of it here: