简体   繁体   English

在打字稿文件中使用JavaScript库

[英]Using JavaScript libraries in typescript files

I am trying to use the Phaser Input JS library in my TypeScript file. 我正在尝试在TypeScript文件中使用Phaser Input JS库

I downloaded all the files to my project: 我将所有文件下载到我的项目中:

夹

I have referenced the js files in my HTML page, 我在HTML页面中引用了js文件,

 <script type="text/javascript" src="phaser-input.js"></script>

and in my app.ts file. 并在我的app.ts文件中。

/// <reference path="phaser-input.d.ts" />

But when I try to use it in my ts file and I write 但是当我尝试在ts文件中使用它时,我写了

var input = game.add.inputField(10, 90);

I get the error 我得到错误

Property 'inputField' does not exit on type 'GameObjectFactory' 属性“ inputField”不会在类型“ GameObjectFactory”上退出

Any suggestion on how to make it work is appreciated. 任何有关如何使其工作的建议均受到赞赏。

What you need to do is Declaration Merging. 您需要做的是声明合并。

The phaser-input.d.ts has it's own namespace Fabrique , and doesn't seem to merge to Phaser namespace or the GameObjectFactory class. phaser-input.d.ts拥有自己的命名空间Fabrique ,似乎没有合并到Phaser命名空间或GameObjectFactory类。 So you need to do it manually by making another d.ts file and reference it in your app.ts 因此,您需要通过制作另一个d.ts文件并在app.ts中引用它来手动进行操作

//index.d.ts
/// <reference path="phaser.d.ts" />
/// <reference path="phaser-input.d.ts" />

declare module Phaser {
  interface GameObjectFactory {
    inputField(x: number, y: number, inputOptions?: Fabrique.InputOptions): Fabrique.InputField
  }
}

//app.ts
///<reference path="index.d.ts" />

var input = game.add.inputField(10, 90);

Try using this syntax : 尝试使用以下语法:

//phaser-input.d.ts
declare module "lib/phaser-input" {
  var input = game.add.inputField(10, 90);
}

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

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