facebook/prop-types

PropTypes.oneOf always ends up as a string type

RyanMitchellWilson opened this issue · 3 comments

I'm trying to use PropTypes.oneOf to make sure that only a couple strings are accepted but every time I use oneOf it ends up allowing any string through instead of the strings specified in the oneOf array.

const schema = {
  foo: PropTypes.oneOf(['Bar', 'Baz']).isRequired
}

This ends up turning into the type (property) foo: string when it should be (property) foo: "bar" | "baz"

Would love to be able to use this and get proper types coming out.

Since this repo doesn’t include types, that’d be up to the DT package.

However, i think this one’s up to TypeScript, since the type of that array passed to oneOf is string[] and not a tuple. I believe you can type it as const, and then it should be picked up properly?

I'm just using standard Typescript and this. The docs say this should work as I expected and if it is passing a string[] in, that seems like a bug in PropTypes to me. oneOf is not working as documented and therefor should be fixed or removed.

It's not up to TypeScript to interpret these correctly, it is up to PropTypes to send in the proper type into typescript and it clearly is not doing so. I can get you more information if needed but this is definitely a bug in PropTypes and this should be reopened.

PropTypes is javascript - it doesn't know about types nor do anything with them, that's 100% TypeScript's job. This library also has no type information, therefore any type issues are decidedly not a bug in this library.

Please see this playground link: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBABShMAVAnmApgZzgM2RDgHIxkwBaGTXEgbgChGA3AQyjhximADsA5ngC8cANokAQhxIAaUtIBeJALr04Aeg1wAFhBZZO+w3AAG3XoJym4bPgBM4fCDCy6IAdzgwdbmtjgIfDMLfiEbYDxzHjCxFVN5bE4MbABlAGNeMBgAOmZ2TnIUFLdRJGLaHByIPiwAeXwAClCrAEp1LXcDI26zIrRaGztHZ1d3Lx8-WjhIkJjBIYcnFzMpGTgAHwU2ZVM81g4ueaEAQRxUAFcwABtS8TWoOW3lFVs8dJruDu09XuNOaKWU7nK63RYjFxuPReZxeEoZLLwfj4Qx4SZwfomfxYfKHfDAAAeWHs5QGATKFBKVRq9SaLWBlxuWHamh+-0CvVM+KJJMpgzgtiWoyhnm8vgxfOwJDR01msLgHh0bHg6AgFzgWAJ2HSMCAA

where you can see how if you fix your incorrectly TS-typed code, everything works out as you expect.