【问题标题】:Typescript file cannot find the declarations defined in .d.ts fileTypescript 文件找不到 .d.ts 文件中定义的声明
【发布时间】:2021-08-04 11:50:14
【问题描述】:

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

由于某种奇怪的原因,我的代码无法从该文件中找到这些声明。声明在@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"
    }
}

globals.d.ts

index.ts

这是错误信息

提前致谢。

【问题讨论】:

  • 很确定您不能在“兄弟”文件(相同目录、相同名称、.d.ts 扩展名)以外的其他位置设置 TypeScript 定义文件

标签: javascript typescript babeljs


【解决方案1】:

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

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;
  }
}

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 2018-09-16
    • 2020-11-22
    • 2021-04-23
    • 2020-12-24
    • 2015-11-22
    • 2020-02-16
    • 2020-11-28
    • 2017-02-16
    相关资源
    最近更新 更多