如何使用TypeScript声明文件中定义的类?

时间:2017-11-21 04:24:41

标签: typescript typescript-typings

我正在尝试为this simple queue implementation创建一个打字文件。

在我的TS项目中,我创建了一个名为customTypings的文件夹,并在我的 tsconfig.json 文件的typeRoots属性中指向它。

这是我的.d.ts文件的样子:

declare module 'queue-fifo' {
    export default class Queue {
        constructor();
        isEmpty(): boolean;
        size(): number;
        clear(): void;
        enqueue(data: any): void;
        dequeue(): any;
        peek(): any;
    }
}

我的导入为:import Queue from 'queue-fifo';

然后我尝试创建Queue类的实例:const queue = new Queue();

此时,我在VS Code中没有出现类型错误,也没有出现任何编译错误。但是,当我尝试通过调试器运行代码时,我得到:

Exception has occurred: TypeError
TypeError: queue_fifo_1.default is not a constructor
    at BinarySearchTree.bfs (/../binarySearchTree.ts:110:23)
    at testBst (/../binarySearchTree.ts:139:10)
    at Object.<anonymous> (/../binarySearchTree.ts:144:1)
    at Module._compile (module.js:632:14)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)

如果我在该行中断,我看到Queue(我导入的内容)未定义,但queue_fifo_1 已定义,我可以创建一个实例在调试控制台中使用该名称的类。

有人可以解释我在声明/消费导致这种不良行为的声明中我做错了吗?

1 个答案:

答案 0 :(得分:3)

解决方案

queue-fifo模块使用CommonJS风格的export = Queue,它与ES6导入不兼容,并且不提供默认导出。 correct type definition需要使用相同的export =样式语法:

declare module "queue-fifo" {
    class Queue {
        isEmpty(): boolean;
        size(): number;
        // etc
    }
    export = Queue;
}

可以使用CommonJS样式导入导入:

import Queue = require("queue-fifo");
const queue = new Queue();

ES6导入

如果您想知道是否可以使用ES6语法导入此模块,则可能使用命名空间定义hack:

class Queue { /* ... */ }
namespace Queue { /* empty */ }
export = Queue;

然后使用ES6通配符导入它:

import * as Queue from "queue-fifo";
const queue = new Queue();

但这只适用于非ES6环境,例如Webpack和Rollup等捆绑商今天就可以使用,但未来的ES6兼容模块系统将无法实现这一功能。点击此处:What does "... resolves to a non-module entity and cannot be imported using this construct" mean?