Coder Social home page Coder Social logo

Comments (4)

jcalz avatar jcalz commented on June 14, 2024 2

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.

RyanCavanaugh avatar RyanCavanaugh commented on June 14, 2024

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.

Arkellys avatar Arkellys commented on June 14, 2024

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.

typescript-bot avatar typescript-bot commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.