在Vue2 + Typescript
项目中,我有global.d.ts
文件,其类型如下:
global.d.ts
type Nullable<T> = T | undefined | null;
在常规.ts
文件中使用时,它工作正常(没有显式类型的导出/导入):
misc.ts
const answer: Nullable<Answer> = null;
但在.vue
文件中不起作用:
component.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>
对此有什么解决办法吗?
答案 0 :(得分:0)
我通过使用global.d.ts
中的tsconfig.json
文件向目录添加路径来解决了这个问题:
{
"compilerOptions": {
"typeRoots": [
"./types"
]
}
}