简体   繁体   English

在TypeScript中创建声明文件以使用JavaScript库

[英]Creating a declaration file in TypeScript to use a JavaScript library

I need to create a declaration type file in TypeScript to be able to use an old JavaScript library I'm using. 我需要在TypeScript中创建一个声明类型文件,以便能够使用我正在使用的旧JavaScript库。

Here's the JavaScript library 这是JavaScript库

Module.js Module.js

var GridManager = function(x) {
  this.foo = function () {
  }
}

When I need to instantiate GridManager in JavaScript I do this 当我需要在JavaScript中实例化GridManager时,请执行此操作

let gridManager = new GridManager(4);

Now I need to create a declaration file .d.ts in TypeScript 现在我需要在TypeScript中创建一个声明文件.d.ts

TypeScript.d.ts TypeScript.d.ts

declare var GridManager: {
    foo(): void;
}

When I create a GridManager in the .ts file I get the following error 当我在.ts文件中创建GridManager时,出现以下错误

Cannot use 'new' with an expression whose type lacks a call or construct signature 无法对类型缺少调用或构造签名的表达式使用“ new”

TypeScript.ts TypeScript.ts

export class View {

  gridManager: GridManager;

  constructor() {
    this.gridManager = new GridManager(4);
  }

}

Can anyone help me with this? 谁能帮我这个?

Change your .d.ts file as 将您的.d.ts文件更改为

export class GridManager {
    constructor(x:Number)
}

foo is hidden from the scope of your class foo隐藏在您的课程范围之外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM