TypeScript Union Type From Array (opens in new tab)
With the new as const construct, we can derive a TypeScript union type from a JavaScript array! const builderSteps = [ 'communications', 'estimates', 'health status', 'procedures' ] as const; type BuilderStep = (typeof builderSteps)[number]; This lets us both type a slice of state, type its updater, and build JSX elements all from the same array, a virtuous cycle. const [step, setStep] = useState ('communications') const handleStepClick = (step: BuilderStep) => setStep(step) {buil...
Read the original article