-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error on types when using multiple conditional properties #58432
Comments
The fact that this works sometimes is due to #30779, which is limited no matter what. It looks like if the discriminant is optional and the property is missing, #30779 doesn't kick in, and you get the same error you'd get when trying to equate a general union-of-objects with an object-of-unions. I don't know if they'll consider this a bug or just a limitation of the sort of thing handled by #30779. |
Yeah, in general a single source type There's almost always a better way to write the target type; I'm not really clear on the intended semantics of the type as written here so can't advise much |
If it helps to give a more concrete example, I need this for a React component with several props required or unnecessary depending on other props. For example: <Chip
removable={isRemovable} // Optional
onRemove={noop} // Required if `removable` is `true`, unnecessary if removable is `false`
label="test" // Required when no `children`, unnecessary when `children` is defined
>
Test {/* Required when `label` is not defined */}
</Chip> /**
* @typedef {object} ChildlessProps
* @property {string} label
*/
/**
* @typedef {object} ParentProps
* @property {any} children
*/
/**
* @typedef {object} RemovableProps
* @property {true} removable
* @property {Function} onRemove
*/
/**
* @typedef {object} StaticProps
* @property {false} [removable=false]
*/
/**
* @typedef {object} BaseProps
* Other properties....
*/
/**
* @typedef {BaseProps & (RemovableProps | StaticProps) & (ParentProps | ChildlessProps)} ChipProps
*/ I was handling these conditions with I'm not sure there is another way to write this? |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
"jsdoc conditional properties", "typescript conditonnal properties", "type conditon"
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.4.5#code/FASwdgLgpgTgZgQwMZQAQGED2YAmIIjboBqCANgGLkDOaA3sKk6kgPwBcqiZtwAvsFCRYiFBmx4CRUmQAqMAK71GzJJwiKo-QeGjxkaLLnyEwAIRlUey5qgBGHLjS0Che0YYknsF8vKWoDLZ26prabiIGqGYItIEqTAic1BrgAOb8ANyCSNgpqND5ALyoABS5YHAgaZwxcQBkZUaSpr5ymqgAPuLGUuaWzgCUqI2lzd5gJH4d3eN9U5RDw0UAfE7WwBX5MFAQCjDmmJhkqCWlAG7knHZHZFAIYMPXt-dgp2uol2TZwIUQpXRUElUAAiBAggA09k4Oz2BzMt1KGiUwz4gyAA
💻 Code
🙁 Actual behavior
When using a second set of conditional properties, the first one throws a type error while it passes when using the condition alone.
If I remove the second condition, or that I passes
true
orfalse
directly, I get not error:Somehow, when passing the
c
property – which is not supposed to be required – it passes:It also passes if I only declare the
a
required property:So
b
andc
are indeed optional.🙂 Expected behavior
I expect the type checking to be the same whether I passes one or multiple conditional properties. I believe all of these should work:
Additional information about the issue
For the context, I'm trying to make some properties required based on other property values.
I realize that maybe there is better way to do this in TS, but actually my problem is in JSDoc, and I believe I'm more limited on what I can do with JSDoc synthax.
My real code is on a React component, and unlike the example above, even passing all the optional properties throws the error.
The text was updated successfully, but these errors were encountered: