I've been working with Vue for a bit now but have started moving to implementations with Typescript. I'm running into an issue where I'm not able to access the child's methods via the refs on the parent.
Parent Code:
<template>
<ref-test ref="child"/>
</template>
<script lang="ts">
import Vue from "vue";
import RefTest from "./RefTest.vue";
export default Vue.extend({
name: "RefParent",
components: {
RefTest
},
data: () => ({}),
methods: {},
mounted() {
const child = this.$refs.child as RefTest;
child.pingMe();
}
});
</script>
<style scoped></style>
Child Code:
<template>
<div>REF TEST...</div>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
name: "RefTest",
data: () => ({}),
methods: {
pingMe() {
console.log("refTest pingMe");
}
}
});
</script>
<style scoped></style>
The issue I'm seeing is that in the parent when I reference the child with const child = this.$refs.child as RefTest; I'm seeing the error: 'RefTest' refers to a value, but is being used as a type here.. In addition, the child.pingMe(); is reporting: Property 'pingMe' does not exist on type 'Vue'.
I've tried various work arounds found here: https://github.com/vuejs/vue/issues/8406 primarily around the inferface definitions and the Vue.extend<> call.
I appreciate any help with helping me continue to wrap my brain around the differences with using Typescript.
RefTestis neither a type nor an interface, so it cannot be used where a type is expected. Tryas typeof RefTest.as typeof RefTestgives new errors.TS2352: Conversion of type 'Vue | Element | Vue[] | Element[]' to type 'VueConstructor<Vue>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. Type 'Element[]' is missing the following properties from type 'VueConstructor<Vue>': extend, nextTick, set, delete, and 8 more.