Skip to content

Instantly share code, notes, and snippets.

@dejanvasic85
Last active March 17, 2022 22:29
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 dejanvasic85/ad6f065844093fc9d8a99f7ce708961e to your computer and use it in GitHub Desktop.
Save dejanvasic85/ad6f065844093fc9d8a99f7ce708961e to your computer and use it in GitHub Desktop.
Typescript string array to Type
export const layout = ['inline', 'stacked'] as const;
export type Layout = typeof layout[number];
// Then it can be used in React props
interface Props {
layout: Layout;
}
const Component = ({ layout }: Props) => (
<div style={{ display: 'flex', flexDirection: layout === 'inline' ? 'column' : 'row' }}></div>
)
// Storybook uses the layout array
export default {
title: 'Component',
argTypes: { layout: { options: layout } }
}
@dejanvasic85
Copy link
Author

Allowing public view.

This is how to convert an array of strings to a Typescript type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment