首页 > 解决方案 > Typescript 文件找不到 .d.ts 文件中定义的声明

问题描述

我正在尝试为我的打字稿插件声明一些全局窗口函数。这些函数是在浏览器中定义的,我希望它们也可以在代码中声明。

出于某种奇怪的原因,我的代码无法从该文件中找到这些声明。声明位于 @types/globals.d.ts 文件中。

我是否需要配置 tsconfig.json 文件或类似的东西。似乎无法从其他任何地方找到直接的答案。

以防万一,这是我的 tsconfig.json 文件

{ 
    "compilerOptions": {
      "outDir": "./dist/",
      "noImplicitAny": true,
      "module": "es6",
      "target": "es5",
      "strict": true,
      "sourceMap": true,
      "allowJs": true,
      "moduleResolution": "node"
    }
}

全局.d.ts

在此处输入图像描述

索引.ts

在此处输入图像描述

这是错误消息

在此处输入图像描述

提前致谢。

标签: javascripttypescriptbabeljs

解决方案


正确的解决方案是重新定义窗口界面。现在其余的文件会自动检测类型。

declare global {
  interface Window {
    section: string;
    __t: (string: string) => string;
    apiSessionKey: string;
    companyData: CompanyData;
    conf_dateformat: string;
    country: string;
    customerCode: string;
    decimalSymbol: string;
    employeeEmail: string;
    employeeID: number;
    employeeName: string;
    formAttributes: any[];
    gdprFeaturesEnabled: boolean;
    isExistingRecord: boolean;
    page_lang: string;
    priceDecimals: number;
    recordID: number;
    subsection: string;
    thousandsSeparator: string;
    userGroupID: number;
    userGroupName: string;
    userID: number;
    userName: string;
  }
}


推荐阅读