我正在尝试使用DefinitelyTyped存储库中的类型声明文件。其中许多文件使用以下模式:
export = React;
export as namespace React;通过谷歌搜索,我发现了一些关于这一点的参考信息,这些信息被用来使声明文件在基于模块的系统和非基于模块的系统中都可用。然而,我正在努力寻找一个清晰的解释(a)这些行中的每一行到底分别做了什么,以及(b)这种组合是如何支持这两种类型的消费者的。
发布于 2017-06-30 23:03:31
第一种形式用于CommonJS和AMD模块系统。您必须将export = React与import React = require('./React')匹配
请参阅给出此示例的documentation:
ZipCodeValidator.ts
let numberRegexp = /^[0-9]+$/;
class ZipCodeValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
export = ZipCodeValidator;Test.ts
import zip = require("./ZipCodeValidator");
// Some samples to try
let strings = ["Hello", "98052", "101"];
// Validators to use
let validator = new zip();
// Show whether each string passed each validator
strings.forEach(s => {
console.log(`"${ s }" - ${ validator.isAcceptable(s) ? "matches" : "does not match" }`);
});export as namespace表单创建了一个全局变量,因此可以在不导入的情况下使用它,但是您仍然可以使用导入的import { name } from "some-library"表单导入它。请参阅给出此示例的documentation:
math-lib.d.ts
export const isPrime(x: number): boolean;
export as namespace mathLib;然后,可以将该库用作模块中的导入:
import { isPrime } from "math-lib";
isPrime(2);
mathLib.isPrime(2); // ERROR: can't use the global definition from inside a module它也可以用作全局变量,但只能在脚本内部使用。(脚本是没有导入或导出的文件。)
mathLib.isPrime(2);这里你有一个泛型类型库,它不知道正在使用哪个模块系统,所以它试图覆盖所有的库。
https://stackoverflow.com/questions/44847749
复制相似问题