Array/Object keys to type?

So I got this object:
export const supportedLanguages: {
afr: Language;
ara: Language;
ces: Language;
cmn: Language;
dan: Language;
deu: Language;
div: Language;
ell: Language;
fas: Language;
fra: Language;
gle: Language;
grc: Language;
hun: Language;
hye: Language;
jav: Language;
jpn: Language;
kat: Language;
kor: Language;
lat: Language;
lav: Language;
lit: Language;
mkd: Language;
nld: Language;
nno: Language;
nob: Language;
nor: Language;
pol: Language;
por: Language;
pus: Language;
ron: Language;
rus: Language;
slk: Language;
spa: Language;
srp: Language;
swe: Language;
tha: Language;
tur: Language;
ukr: Language;
urd: Language;
vie: Language;
};
export const supportedLanguages: {
afr: Language;
ara: Language;
ces: Language;
cmn: Language;
dan: Language;
deu: Language;
div: Language;
ell: Language;
fas: Language;
fra: Language;
gle: Language;
grc: Language;
hun: Language;
hye: Language;
jav: Language;
jpn: Language;
kat: Language;
kor: Language;
lat: Language;
lav: Language;
lit: Language;
mkd: Language;
nld: Language;
nno: Language;
nob: Language;
nor: Language;
pol: Language;
por: Language;
pus: Language;
ron: Language;
rus: Language;
slk: Language;
spa: Language;
srp: Language;
swe: Language;
tha: Language;
tur: Language;
ukr: Language;
urd: Language;
vie: Language;
};
Which as you can see has a few keys, you can pass these keys in an array, how would I make a ArkType for them automatically/dynamically since this object updates often. Without ArkType we currently use the following:
export type SupportedISOs = keyof typeof supportedLanguages;
export interface Options {
//Other options
languages: SupportedISOs[] // ("afr" | "ara" | "ces" | "cmn" | "dan" | "deu" | "div" | "ell" | "fas" | "fra" | "gle" | "grc" | "hun" | "hye" | "jav" | "jpn" | "kat" | "kor" | "lat" | "lav" | "lit" | "mkd" | "nld" | "nno" | "nob" | "nor" | "pol" | "por" | "pus" | "ron" | "rus" | "slk" | "spa" | "srp" | "swe" | "tha" | "tur" | "ukr" | "urd" | "vie")[];
}
export type SupportedISOs = keyof typeof supportedLanguages;
export interface Options {
//Other options
languages: SupportedISOs[] // ("afr" | "ara" | "ces" | "cmn" | "dan" | "deu" | "div" | "ell" | "fas" | "fra" | "gle" | "grc" | "hun" | "hye" | "jav" | "jpn" | "kat" | "kor" | "lat" | "lav" | "lit" | "mkd" | "nld" | "nno" | "nob" | "nor" | "pol" | "por" | "pus" | "ron" | "rus" | "slk" | "spa" | "srp" | "swe" | "tha" | "tur" | "ukr" | "urd" | "vie")[];
}
Can't figure out yet how I would make this in ArkType. Anyone have some ideas?
4 Replies
Dimava
Dimava2y ago
In 0.15 that's
const supportedLanguages = { en: Language, fr: Language }
export type Lang = keyof typeof supportedLanguages;
export const Lang = declare<Lang>().type('===', ...Object.keys(supportedLanguages ));
const supportedLanguages = { en: Language, fr: Language }
export type Lang = keyof typeof supportedLanguages;
export const Lang = declare<Lang>().type('===', ...Object.keys(supportedLanguages ));
ssalbdivad
ssalbdivad2y ago
If you already have them in an object I would try something like this:
export const myType = type(
Object.keys(supportedLanguages)
.map((lang) => `'${lang}'`)
.join("|") as Infer<keyof typeof supportedLanguages>
)
export const myType = type(
Object.keys(supportedLanguages)
.map((lang) => `'${lang}'`)
.join("|") as Infer<keyof typeof supportedLanguages>
)
You can try messing around with the type here! https://stackblitz.com/edit/mrvfrm-gq6z2z?file=type.ts
StackBlitz
ArkType Example (forked) - StackBlitz
ArkType type demo
Dimava
Dimava2y ago
JSON.stringify
Bas950
Bas950OP2y ago
Thanks both! Will use:
arrayOf(
type(
Object.keys(supportedLanguages).map(lang => `'${lang}'`)
.join("|") as Infer<SupportedISOs>
)
)
arrayOf(
type(
Object.keys(supportedLanguages).map(lang => `'${lang}'`)
.join("|") as Infer<SupportedISOs>
)
)
Want results from more Discord servers?
Add your server