question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String literal types with React Typescript

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
marie-maximecommented, Aug 5, 2020

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.

enum CardStatus {  // placeholder name
  LATER = "LATER",
  INMEDIATELY = "INMEDIATELY",
  FCFS = "FCFS",
}

interface LotteryCardInterface {
  type: CardStatus
}

const options = CardStatus
const defaultValue = CardStatus.LATER

export const Summary = () => {
  const value = radios(label, options, defaultValue);
  return (
    <LotteryCard
      type={value}
    />
)

I’m not sure what is the radio methods doing, so you might need to change it a bit, but this should probably works.

1reaction
Alfrex92commented, Aug 6, 2020

thanks 😃, is working.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found