Subscribe on changes!

Conditional properties through discriminated unions and intersections in TypeScript

avatar
Jan 18th 2023

What problem does this feature solve?

It would be very useful to explain component properties the following manner.

interface CommonProps {
    size?: 'xl' | 'l' | 'm' | 's' | 'xs';
}

type ConditionalProps = {
    color?: 'normal' | 'primary' | 'secondary';
    appearance?: 'normal' | 'outline' | 'text';
} | {
    color: 'white';
    appearance: 'outline';
};

type Props = CommonProps & ConditionalProps

What does the proposed API look like?

This issue is related to https://github.com/vuejs/core/issues/4294, but the use case is different.

Here is an article that goes into more information on this approach.

avatar
May 13th 2023

@sxzz Do you know how this would be accomplished using .ts?

export default defineComponent({
  name: 'MyComponent',
  props: {
    // are there vue native types that can be imported here to support discriminated unions and intersections?
  },
  setup() {
    return () => (
      // ...
    )
  }
})