Skip to content

Instantly share code, notes, and snippets.

@peterhudec
Created December 14, 2021 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhudec/04caffa47f891c34ac2ae8aaf45f9358 to your computer and use it in GitHub Desktop.
Save peterhudec/04caffa47f891c34ac2ae8aaf45f9358 to your computer and use it in GitHub Desktop.
FX Asset Classes Typing
type Currency = 'USD' | 'EUR' | 'CZK'
type PreciousMetal = `X${ 'AG' | 'AU' | 'PD' | 'PT' }`
type Treasury = `T${`0${ 2 | 3 | 5 | 7 }` | `${ 1 | 2 | 3 }0`}`
type CurrencyCurrency = `${Currency}${Currency}`
type PreciousMetalCurrency = `${PreciousMetal}${Currency}`
type TreasuryCurrency = `${Treasury}${Currency}`
type InstrumentNames = CurrencyCurrency | PreciousMetalCurrency | TreasuryCurrency
type AssetClass<T> = T extends `X${string}`
? 'PreciousMetal'
: T extends `T${'0' | '1' | '2' | '3'}${string}`
? 'Treasury'
: 'FX'
type PM = AssetClass<'XAUUSD'>
type T = AssetClass<'T10USD'>
type FX = AssetClass<'foo'>
type Instruments = {
[K in InstrumentNames]: AssetClass<K>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment