Skip to content

Instantly share code, notes, and snippets.

@heyMP
Created March 8, 2022 20:26
Show Gist options
  • Save heyMP/3cb607933225d3fce64cfc42ee181c5a to your computer and use it in GitHub Desktop.
Save heyMP/3cb607933225d3fce64cfc42ee181c5a to your computer and use it in GitHub Desktop.
Two ways of syncing types and variables.
// 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 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