简体   繁体   English

无法执行无类型打字稿模块

[英]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.

相关问题 打字稿:无类型的数组 - typescript: untyped array 如何在Typescript 1.8中包含无类型的节点模块? - How to include untyped node modules with Typescript 1.8? AngularJS/TypeScript:由于服务无法实例化模块 - AngularJS/TypeScript: Failed to instantiate module due to service 为导出单个功能的无类型模块定义类型 - Defining types for untyped module that exports single function 如何使用 Flow 解决无类型模块错误? - How to solve Untyped module error with Flow? 如何在TypeScript中使用无类型的JS NPM模块用于节点? - How to use untyped JS NPM modules in TypeScript for node? 将无类型 javascript 模块与 Typescript 一起使用:“未知”类型上不存在属性 - Using untyped javascript modules with Typescript: Property does not exist on type 'unknown' Typescript 泛型包装器:无类型函数调用可能不接受类型参数 - Typescript Generics wrapper: Untyped function calls may not accept type arguments Angularjs,Typescript,Uglify无法实例化模块myApp…未知提供程序: - Angularjs, Typescript, Uglify Failed to instantiate module myApp … unknown provider: a Vue:在 TypeScript 中使用“export * as”时模块解析失败 - Vue: Module parse failed when using "export * as" in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM