johnsoncodehk / vue-tsc

vue-tsc --noEmit && vite build
https://www.npmjs.com/package/vue-tsc
MIT License
241 stars 6 forks source link

Errors when attempting to use a prop validator #76

Closed ghost closed 3 years ago

ghost commented 3 years ago

Maybe I'm just doing it wrong, but when I add a prop validator for a Vue 3 project, I get all kinds of nonsensical TS errors inside my setup method. When I remove the validator the errors go away.

<template>
<template>

<script lang="ts">
import { defineComponent, computed } from 'vue';

export default defineComponent({
  name: 'Example',
  props: {
    str: {
      type: String,
      required: true,
      validator(value: unknown) {
        return value.length > 2;
      }
    }
  },
  setup(props) {
    const greeting = computed(() => "Here's the string: " + props.str);
    return { greeting };
  }
});
</script>

Error:

error TS2571: Object is of type 'unknown'.

        return value.length > 2;
               ~~~~~

Error:

error TS2339: Property 'str' does not exist on type 'Readonly<LooseRequired<Readonly<readonly unknown[] & { [x: number]: string; } & { toString?: string | undefined; toLocaleStr
ing?: string | undefined; concat?: string[] | undefined; indexOf?: ((searchElement: string, fromIndex?: number | undefined) => number) | undefined; ... 18 more ...; flat?: unknown[] | undefined...'.

        const greeting = computed(() => "Here's the string: " + props.str);
                                                                      ~~~

vue-tsc version is 0.3.0

johnsoncodehk commented 3 years ago

This is upstream problem, see https://github.com/johnsoncodehk/volar/issues/93.