【问题标题】:TypeScript custom definition fileTypeScript 自定义定义文件
【发布时间】:2017-01-28 00:39:33
【问题描述】:

所以我有一个第三方库,其中包含可用的全局函数,如 jQuery:$.functionName() 但没有定义文件。所以我想编写自己的文件,但我无法使引用/导入工作。

如果我在内联编写声明,它工作正常:

declare let a:string;

myClass {
   constructor() {
       console.log(a);
   }
}

但是如果我在另一个文件中声明它,我就不能让它工作:

myDeclaration.d.ts

export declare let a:string;

myClass.ts

/// <reference path="myDeclaration.d.ts" />

myClass {
   constructor() {
       console.log(a);
   }
}

我也试过import * as a from "myDeclaration"。它总是说:Cannot find name 'a'

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    您不应将 export 与 ambient 声明一起使用。

    只需从您的 .d.ts 中删除导出,如下所示:

    declare let a:string
    

    通常您甚至可以删除 &lt;reference path=""&gt;,因为声明应该对您的整个项目都可用。

    在为导入的模块(例如 JS npm 库)编写定义时应使用 export declare,当对象在全局范围内可用且无需任何导入时使用 ambient 定义(由 &lt;script&gt; 导入的 jQuery 用于示例)。

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2016-05-04
      • 2012-11-16
      • 2016-01-07
      • 2021-01-03
      相关资源
      最近更新 更多