0

I'm having some issues with defining the PropTypes for a string enumerator. I have the following type in Typescript:

state: "pending"|"accepted"|"rejected",

and the corresponding propType:

state: PropTypes.oneOf(["pending","accepted","rejected"]).isRequired,

This, however throws the following error: "Type 'string' is not assignable to type '"pending" | "accepted" | "rejected"'.ts(2322)". I really don't know what to do! Thanks in advance for any help you can give me.

3
  • What line throws that error, exactly? Commented Aug 10, 2021 at 18:50
  • The second line, the PropType one. It detects as "string" the propType, and as "pending" | "accepted" | "rejected" the TypeScript property Commented Aug 10, 2021 at 18:52
  • Can you provide a minimal reducible example? The closest I got was tsplay.dev/w11AYw. But I'm not familiar enough with this library to get it wired up how you have it. Commented Aug 10, 2021 at 18:57

1 Answer 1

1

Issue is state has type "pending"|"accepted"|"rejected" and its not Enum its Union type and["pending","accepted","rejected"] has type string[]

you have to Tell Typescript its "pending"|"accepted"|"rejected" [] not string[]

For convenience I'm going to create a new type

type State = 'pending' | 'accepted' | 'rejected';

state: State = 'pending'

// we have to tell typescript its type is not string[] but State[] 
state: PropTypes.oneOf(['pending','accepted','rejected'] as State[]).isRequired

here is Example that I have created for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.