typescript - TypeScript 自引用
问题描述
我有一个 Vue 插件,它允许访问 .vue 文件模板中的常量对象作为快捷方式:
<template>
<span>{{ $consts.HELLO }}</span>
</template>
export default {
constants: {HELLO: 'hello there!'},
};
$consts
这是一个链接$options.constants
。
如何在 TypeScript 中描述此功能?我的版本是:
declare module 'vue/types/vue' {
interface Vue {
readonly constants?: object
readonly $consts: Vue['$options']['?constants'] // ???
}
}
但是 TypeScript 向我显示了这个错误:
TS2339:类型“ComponentOptions、DefaultMethods、DefaultComputed、PropsDefinition >、Record...>>”上不存在属性“?constants”。
解决方案
@lena 分享了这个链接到 Vue 官方说明: https ://vuejs.org/v2/guide/typescript.html#Augmenting-Types-for-Use-with-Plugins
就我而言,我需要ComponentOptions
像这样扩展
declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
constants?: {}
}
}
并从此行中删除问号
readonly $consts: Vue['$options']['?constants']
所以现在在 phpStorm *.vue 文件中,IDE 没有警告Unresolved variable or type $consts
推荐阅读
- elasticsearch - 查询子属性
- ruby-on-rails - Rails 6 + Webpacker 4 + PostCSS & @font-face + 本地字体
- perl - 如何重命名目录中的所有文件以具有相同数量的数字?
- sparql - Graphdb sparql 无法进行联合查询
- mysql - 需要将mysql转为SQL
- r - 如何将转换矩阵读入 Cytoscape 以进行马尔可夫链建模?
- reactjs - 我可以为 ReactJS 重新使用相同的 onload.js 函数吗
- android - 无法使用 com.android.test 模块插件运行 android espresso 测试
- scala - 无法在操作过滤器中读取请求正文
- c# - 如何修复 C# 代码中的额外空白问题?