[英]How to specify global type declarations in create-react-app?
我在我的 create-react-app 項目中設置全局類型時遇到問題。 這是我的tsconfig.json文件。
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"components/*": [
"src/app/components/*"
],
"constants/*": [
"src/app/constants/*"
],
"services/*": [
"src/app/services/*"
],
"reducers/*": [
"src/app/reducers/*"
],
"selectors/*": [
"src/app/selectors/*"
],
"types/*": [
"src/app/types/*"
],
"pages/*": [
"src/app/pages/*"
],
"styles/*": [
"src/static/styles/*"
],
},
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": false,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"typeRoots": ["node_modules", "typings"],
},
"include": [
"src"
],
"exclude": ["node_modules", "**/*.js"]
}
如您所見,我在項目的根目錄下創建了一個類型文件夾,我將在其中保存全局類型和其他一些類型。 所以在我的打字文件夾中,我有一個包含此聲明的global.d.ts文件。
declare global {
interface Window { fastlink: any; }
}
但是在我的反應組件中,我有這個 typescript 錯誤
類型“Window & typeof globalThis”上不存在屬性“fastlink”。
我已經在這里調查了很多文章和帖子,也閱讀了文檔,但沒有結果。
interface Window {
fastlink: any;
}
不需要聲明全局,可以直接在接口Window中定義。
您可以通過window.fastlink
訪問它
小心使用全局變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.