Created
March 8, 2022 20:26
-
-
Save heyMP/3cb607933225d3fce64cfc42ee181c5a to your computer and use it in GitHub Desktop.
Two ways of syncing types and variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is where we infer Typescript types from JS arrays. | |
export const states = [ | |
'frame', | |
'color', | |
'pedals', | |
'seat', | |
'handles', | |
'wheels', | |
] as const; | |
export const colors = [ | |
'Red', | |
'Orange', | |
'Navy', | |
'Blue', | |
] as const; | |
export const bikeTypes = [ | |
'Road', | |
'loRider', | |
'wagon', | |
'Tri', | |
'eBike', | |
] as const; | |
export type State = typeof states[number]; | |
export type Color = typeof colors[number]; | |
export type BikeType = typeof bikeTypes[number]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is where we can sync JS object from types. This is useful if you're importing types from another file. | |
type State = "a"|"b"; | |
const states: Record<State, any> = { | |
a: 'a' | |
} | |
const allStates = Object.keys(states); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://steveholgado.com/typescript-types-from-arrays/#inferred-array-types