Comments (4)
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.
from typescript.
Yeah, in general a single source type S
isn't assignable to a target type T = T1 | T2 | T3 | Tn...
just because a combinatorial explosion of S
would ultimately result in every possible inhabitant eventually finding varying targets in T
. There are exceptions, but this isn't one of them.
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
from typescript.
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 propTypes
, but since prop types check will be removed on React 19 I decided to try and strengthen my JSDoc so that it could handle them instead (I don't use TS).
I'm not sure there is another way to write this?
from typescript.
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- Recursive mapped type with callable signatures not working correctly. HOT 8
- "Property does not exist on type 'never'" with optional chaining HOT 5
- ts2322 incorrectly triggered when importing json HOT 8
- Incorrect type inferrence that sub-class does not extend super-class with generics HOT 3
- [NewErrors] 5.5.0-dev.20240602 vs 5.4.5 HOT 29
- [ServerErrors][JavaScript] 5.5.0-dev.20240602 HOT 11
- [ServerErrors][TypeScript] 5.5.0-dev.20240602 HOT 26
- Debug failure on paste with imports
- [TS2742] Occurs building a library that depends on `@reduxjs/toolkit`
- Promise<T> where T is an object allows additional keys on T HOT 1
- Module resolution: tsconfig.json#compilerOptions/path does not support files in parent folder HOT 2
- Annotated parameters are not used for inferring rest parameters HOT 2
- Support option to not expand type aliases in typescript HOT 2
- [ts-5.5-rc] Import elided in spite of being use in declarations
- Delete or Change Unavailable YouTube Video in /why-create-typescript
- [ts-5.5]Some types are excessively expanded in keyof and index access types
- `for (const x in #b)` is allowed
- Can't augment function overload is function is exported via `export {}` HOT 2
- Design Meeting Notes, 5/31/2024 HOT 3
- `lib.d.ts` Updates for TypeScript 5.6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.