Subscribe on changes!

Conditional properties through discriminated unions and intersections in TypeScript

avatar
Aug 10th 2023

What problem does this feature solve?

https://github.com/vuejs/core/issues/7553 Reopening this because I believe the problem is not solved. It is still impossible to use coditional props.

Just try this after npm create vue@latest inside HelloWorld component

<script setup lang="ts">
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
defineProps<Props>()
</script>

and then try to use it

image

Theoretically it should not allow us to use both. That is, there can't be color="white" appearance="text" only color="white" appearance="outline"

What does the proposed API look like?

{
      color?: 'normal' | 'primary' | 'secondary'
      appearance?: 'normal' | 'outline' | 'text'
    }
  | {
      color: 'white'
      appearance: 'outline'
    }
avatar
Aug 10th 2023

Added another simple example with fully "exclusive" props that should be covered as well and does not work (yet).

avatar
Sep 13th 2023

I ran into this many times, huge +1.

avatar
Sep 27th 2023

Typescript allows me to use Conditional Types, but Vue doesn't allow me to do this

avatar
Nov 3rd 2023

C'mon Vue and TS NEED FULL SYNERGY.

avatar
Feb 28th 2024

Hi guys, Any update here? @sxzz if you can please follow up on this issue.