使用Typescript接口类型的VueJS道具验证

时间:2019-03-28 16:09:01

标签: typescript vue.js vue-component

我有以下VueJS组件(打字稿,vue-property-decorator):

import {PropType} from 'vue';
import {Component, Vue, Prop} from 'vue-property-decorator';
import {buildRouteLink, Route} from 'my-config';

@Component
export default class Report extends Vue {
  // I've also tried Object as () => Route, which also doesn't work
  @Prop({type: Object as PropType<Route>, required: true})
  private route!: Route;
  private url: string = '';
  mounted() {
    this.url = buildRouteLink(this.route);
  }
}

问题是,如果我像这样安装组件:

<report :route="route"/>

并将route属性传递为不符合Route接口的类型(例如:{foo: true}),我没有运行时验证错误,只有{ {1}},它期望存在buildRouteLink()个字段。

Route界面很简单:

Route
  • interface Route { app: string; route: string; } 版本:3.2.4
  • typescript版本:2.6.10
  • vue版本:7.3.0

0 个答案:

没有答案