vuejs2 - Vue SFC'de Typescript global tip bildirimleri nasıl kullanılır?
Vue2 + Typescript projesinde şu türden bazı tiplerin bulunduğu bir global.d.ts dosyam var:
global.d.ts
type Nullable<T> = T | undefined | null;
Normal .ts dosyalarında kullanıldığında (tipleri açıkça export/import etmeden) sorunsuz çalışır:
misc.ts
const answer: Nullable<Answer> = null;
Ama .vue dosyalarının içinde çalışmıyor:
component.vue
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
data: () => ({
answer: null as Nullable<Answer> // 'Nullable' is not defined
})
});
</script>
Üçlü eğik çizgi olayı da çalışmıyor:
<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>
Bunun için herhangi bir çözüm var mı?
Cevap #1
Bunu, tsconfig.json dosyasında global.d.ts dosyasının bulunduğu dizini yola ekleyerek çözdüm:
{
"compilerOptions": {
"typeRoots": [
"./types"
]
}
}
