Skip to content

defineComponent cross-property generics usage is broadened on usage  #3782

@crutchcorn

Description

@crutchcorn

I have a .vue file that accepts generic types like so:

<!-- Test.vue -->
<script setup lang="ts" generic="TStr extends 'one' | 'two', TNum extends TStr extends 'one' ? 1 : 2">
const props = defineProps<{
  str: TStr,
  num: TNum,
  numFn: (num: TNum) => TNum
}>()
</script>

<template>
  <div>
    <p>str: {{ props.str }}</p>
    <p>num: {{ props.num }}</p>
  </div>
</template>

And when using it in App.vue, it works just as expected to infer the values of numFn:

<!-- App.vue -->
<script setup lang="ts">
import Test from "./Test.vue"
</script>

<template>
  <!-- val is `1` -->
  <Test str="one" :num="1" :numFn="val => val" />
  <!-- val is `2` -->
  <Test str="two" :num="2" :numFn="val => val" />
</template>

However, when using defineComponent and generics inside like so:

// Test2.ts
import { defineComponent } from "vue";

export const Test2 = defineComponent(
  <TStr extends "one" | "two", TNum extends TStr extends "one" ? 1 : 2>(props: {
    str: TStr;
    num: TNum;
    numFn: (num: TNum) => TNum;
  }) => {
    return () => props.str;
  }
);

The type inferencing no longer works:

<script setup lang="ts">
import {Test2} from './Test2'
</script>

<template>
  <!-- val is `1 | 2` -->
  <Test2 str="one" :num="1" :numFn="val => val" />
  <!-- val is `1 | 2` -->
  <Test2 str="two" :num="2" :numFn="val => val" />
</template>

Link to Reproduction

https://github.com/crutchcorn/vue-define-component-ts-broadening-bug

https://stackblitz.com/github/crutchcorn/vue-define-component-ts-broadening-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions