typescript - 如何在 Vue SFC 中使用 Typescript 全局类型声明?
问题描述
在Vue2 + Typescript
项目中,我有global.d.ts
一些类型的文件:
全球.d.ts
type Nullable<T> = T | undefined | null;
在常规.ts
文件中使用时它工作正常(没有明确类型导出/导入):
杂项
const answer: Nullable<Answer> = null;
但它在文件中不起作用.vue
:
组件.vue
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
data: () => ({
answer: null as Nullable<Answer> // 'Nullable' is not defined
})
});
</script>
三重斜线也不起作用:
<script lang="ts">
/// <reference path="../../types/global.d.ts" />
import Vue from 'vue';
export default Vue.extend({
data: () => ({
answer: null as Nullable<Answer> // 'Nullable' is not defined
})
});
</script>
有什么解决办法吗?
解决方案
我通过将路径添加到我的目录中的global.d.ts
文件来解决了这个问题tsconfig.json
:
{
"compilerOptions": {
"typeRoots": [
"./types"
]
}
}
推荐阅读
- r - 为什么 str_split() 会多加一层列表?
- python - 为什么 bytes.fromhex() 会产生显示的输出?
- flutter - Didier Boelens 的反应式编程 BLoC 或如何实现应用范围的 BLoC
- python - Django Channels - 聊天教程 - 仅在第二个选项卡上收到消息
- angular - ExpressionChangedAfterItHasBeenCheckedError - 增加一个数字并在视图中显示该数字
- javascript - 我们可以为对象键使用连接的值和变量吗?
- javascript - 使用 Jest 在 React 中测试输入数据
- javascript - HTML 如何阻止用户输入大于 2100-01-01 的日期?
- python - 延迟后如何重置键盘输入?
- excel - 向 Excel 列追溯添加“am”或“pm”