Skip to content

type auto resolve when use defineExpose in typescript #6644

@Dedicatus546

Description

@Dedicatus546

What problem does this feature solve?

it seems that we must create type for a componet when use defineExpose in typescript , and we can get type hint.

// src/types/index.ts
export type CompInst = {
  getString: () => string;
};
// src/Comp.vue
<script setup lang="ts">
import { CompInst } from "./types";

defineExpose<CompInst>({
  getString() {
    return "hello";
  },
});
</script>

<template>
  <div>comp1</div>
</template>
// src/App.vue
<script setup lang="ts">
import Comp from "./Comp.vue";
import { CompInst } from "./types";

const compRef = ref<CompInst | null>(null);

// type hint !
compRef.value?.getString();
</script>

<template>
  <Comp ref="compRef"></Comp>
</template>

maybe can we resolve type of expose by component directly?

What does the proposed API look like?

// src/App.vue
<script setup lang="ts">
// a inner tool can auto resolve expose type from type of component.
import { resolveExpose } from "vue"
import Comp from "./Comp.vue";


const compRef = ref<resolveExpose<typeof Comp> | null>(null);

// type hint !
compRef.value?.getString();
</script>

<template>
  <Comp ref="compRef"></Comp>
</template>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions