A
arktype3mo ago
Cobain

Use native ts type

I was wondering if I could use an already created type:
type BlockainsName = "bitcoin" | "ethereum" | "solana" | "polygon-pos" | "binance-smart-chain" | "avalanche"
type BlockainsName = "bitcoin" | "ethereum" | "solana" | "polygon-pos" | "binance-smart-chain" | "avalanche"
inside an arktype type declaration:
export const walletType = type({
address: "string",
blockchain: "BlockainsName",
{...}
export const walletType = type({
address: "string",
blockchain: "BlockainsName",
{...}
I would go on and create an arktype literal as suggested in the docs, but this type is being declared from another object:
export type BlockainsName = keyof typeof blockchains
export type BlockainsName = keyof typeof blockchains
And I want it to be tighly coupled with that object
4 Replies
TizzySaurus
TizzySaurus3mo ago
Iirc .as can be used for using a pre-existing type
Dimava
Dimava3mo ago
const EveryBlockainsName = Object.keys(blockchains) as BlockainsName []

type(['===', ...EveryBlockainsName ])
const EveryBlockainsName = Object.keys(blockchains) as BlockainsName []

type(['===', ...EveryBlockainsName ])
ssalbdivad
ssalbdivad3mo ago
If you have them in an object I'd do what Dimava suggests so there's a single source of truth. You can also do type.enumerated(...keys) if you prefer. You'll just have to make sure you cast your keys list since TS won't narrow it by default
Cobain
CobainOP3mo ago
Right I was doing that but and by default TS considered it string, doing what Dimava suggested makes this union of string literals that I wanted, thanks!
Want results from more Discord servers?
Add your server