[英]Failed execution of untyped typescript module
I'm trying to integrate Typescript in a project build with webpack. 我正在尝试将Typescript与Webpack集成到项目构建中。 I have a library that does not come with ts definition ( chess.js ), so I used an external definition library ( @types/chess.js ).
我有一个不带ts定义的库( Chess.js ),所以我使用了一个外部定义库( @ types / chess.js )。
I managed to get the code to compile without any errors, but when it is executed in the browser I got the following error: 我设法使代码得以编译而没有任何错误,但是在浏览器中执行该代码时,出现以下错误:
TypeError: i.Chess is not a constructor
Here is the typescript code that does not work: 这是无效的打字稿代码:
import {Chess} from 'chess.js';
const board = new Chess();
The chess.js module does not have typescript types associated. Chess.js模块没有关联的打字稿类型。 Here is how this module is exported.
这是此模块的导出方式。
var Chess = function(fen) {
// ...
}
/* export Chess object if using node or any other CommonJS compatible
* environment */
if (typeof exports !== 'undefined') exports.Chess = Chess;
/* export Chess object for any RequireJS compatible environment */
if (typeof define !== 'undefined') define( function () { return Chess; });
Here is the ts types I use declares the chess.js module. 这是我使用的声明chess.js模块的ts类型。
/**
* The chess.js function that is used to build chess game instances.
* It can be used with or without `new` to build your instance. Both variants
* work the same.
*/
export const Chess: {
(fen?: string): ChessInstance;
new (fen?: string): ChessInstance;
};
And here is my tsconfig.json: 这是我的tsconfig.json:
Here is my tsconfig file: 这是我的tsconfig文件:
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"allowJs": true
}
}
The webpack configuration I use is the same as defined in the webpack basic TS setup guide . 我使用的webpack配置与webpack基本TS设置指南中定义的相同。
declare module also 同时声明模块
declare module 'chess.js' {
type ChessInstance = any
export const Chess: {
(fen?: string): ChessInstance;
new (fen?: string): ChessInstance;
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.