javascript - Vue 在编译时使用 Typescript 类型检查 Prop

标签 javascript typescript vue.js compilation

我将 Vue.js 与 Typescript 结合使用。 代码很简单。 (使用 vue-class-component 和 vue-property-decorator)

<script lang="ts">
    import { Component, Prop, Vue } from 'vue-property-decorator';

    @Component
    export default class Child extends Vue {
        @Prop(String) private value!: string;
    }
</script>

当然,如果我将字符串作为 props 传递给子组件,它会在没有警告或错误的情况下工作。然后传递数字,它会在运行时显示警告,如下所示。

enter image description here

但是编译没有错误。有什么方法可以检查“编译”时间的类型吗?

最佳答案

将评论转化为答案...

当我遇到类似的问题时,它与 vue 如何处理从模板向组件传递 Prop 有关。如果您使用 value="1"则传递一个字符串,如果您使用 :value="1"则传递一个数字。这确实令人困惑,您只能在运行时看到它,因为模板未编译。

关于javascript - Vue 在编译时使用 Typescript 类型检查 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51982115/

相关文章:

typescript - 无法从 Vue.js 中的方法访问数据(使用 Ionic)

javascript - node.js 本地模块 :cannot find module error

typescript - 我可以在 TypeScript 中将一个类型数组映射到另一个类型数组吗?

javascript - Typescript,自返回通用

typescript - 我在哪里可以找到 tsc 错误列表?

javascript - 无法让 Vue.js CSS 转换工作?

javascript - Vue.js orderBy 无法正确处理大写和小写

javascript - AWS Lambda 函数返回 "Cannot find module ' index'"但配置中的处理程序设置为 index

javascript - 我无法设置 CSS 来选择选择框选项

javascript - ExtJs 3.4 : Set label text and background color from Json data