[英]Failed execution of untyped typescript module
我正在尝试将Typescript与Webpack集成到项目构建中。 我有一个不带ts定义的库( Chess.js ),所以我使用了一个外部定义库( @ types / chess.js )。
我设法使代码得以编译而没有任何错误,但是在浏览器中执行该代码时,出现以下错误:
TypeError: i.Chess is not a constructor
这是无效的打字稿代码:
import {Chess} from 'chess.js';
const board = new Chess();
Chess.js模块没有关联的打字稿类型。 这是此模块的导出方式。
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; });
这是我使用的声明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;
};
这是我的tsconfig.json:
这是我的tsconfig文件:
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"allowJs": true
}
}
我使用的webpack配置与webpack基本TS设置指南中定义的相同。
同时声明模块
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.