String literal types with React Typescript
See original GitHub issueI want to use String literal types in my component and I want to able to select any of those strings in my story using Storybook.
The component can only receive 3 types of strings: LATER | IMMEDIATELY | FCFS
But, I have this error:
Type 'string' is not assignable to type '"LATER" | "INMEDIATELY" | "FCFS"'.ts(2322) index.tsx(4, 3): The expected type comes from property 'type' which is declared here on type 'IntrinsicAttributes & LotteryCardInterface & { children?: ReactNode; }'
Component:
interface LotteryCardInterface {
  type: 'LATER' | 'INMEDIATELY' | 'FCFS'
}
const LotteryCard: React.FC<LotteryCardInterface> = (props: LotteryCardInterface) => 
(<p>
    {props.type}
  </p>)
export default LotteryCard;
Story:
export default {
  title: 'organism|LotteryCard',
  component: LotteryCard,
  decorators: [withKnobs]
}
const label = 'type';
const options = {
  LATER: 'LATER',
  INMEDIATELY: 'INMEDIATELY',
  FCPS: 'FCPS',
};
const defaultValue = 'LATER';
export const Summary = () => {
  const value = radios(label, options, defaultValue);
  return (
    <LotteryCard
      type={value}
    />
  )
}
Any idea how to fix it?
Thanks
Issue Analytics
- State:
 - Created 3 years ago
 - Comments:6
 
Top Results From Across the Web
Handbook - Literal Types - TypeScript
There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an...
Read more >What are string literal types in TypeScript ? - GeeksforGeeks
The string literal type allows you to specify a set of possible string values for a variable, only those string values can be...
Read more >String literal union types - Learn React with TypeScript 3 [Book]
A string literal union type is where we combine multiple string literal types together. Let's continue from the previous example and go through...
Read more >Extract parameter types from string literal types with TypeScript
String Literal Type ; let · : string = 'abc';. str = ; let · : 'abc' = 'abc';. str = ; function...
Read more >How to Use TypeScript Template Literal Types Like a Pro
After reading the above code, do you think it is much simpler? Similar to template strings in JavaScript, template literal types are enclosed...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Sorry, kinda busy today so I don’t have much time to explain, but this should look something like this:
To have a variable match a series of string in Typescript, you have to use enum. The enum value can then be used directly in your code.
I’m not sure what is the radio methods doing, so you might need to change it a bit, but this should probably works.
thanks 😃, is working.