Javascript 使用Typescript进行默认导出
我想在typescript项目中使用一个小的javascript库。(供参考) javascript库定义了以下内容:Javascript 使用Typescript进行默认导出,javascript,typescript,es6-modules,Javascript,Typescript,Es6 Modules,我想在typescript项目中使用一个小的javascript库。(供参考) javascript库定义了以下内容: "use strict"; exports.__esModule = true; exports["default"] = functionOfInterest; function functionOfInterest(val) { } module.exports = exports["default"]; 这是使用.d.ts文件键入的,如下所示: export d
"use strict";
exports.__esModule = true;
exports["default"] = functionOfInterest;
function functionOfInterest(val) { }
module.exports = exports["default"];
这是使用.d.ts
文件键入的,如下所示:
export default function functionOfInterest(val: number): void;
在typescript文件中,我使用:
import functionOfInterest from "the-package";
functionOfInterest(1); // invoke it.
这是为了:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var functionOfInterest_1 = require("the-package");
functionOfInterest_1.default.apply(void 0, 1);
这将导致以下错误:
TypeError: Cannot read property 'apply' of undefined
我的tsconfig.json
文件是:
{
"compilerOptions": {
"outDir": "js",
"module": "commonjs",
"declaration": true,
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "ts",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true
},
"exclude": [
"node_modules",
"build"
]
}
我肯定在某个地方有一个简单的配置错误,但我现在还没弄清楚
键入脚本2.2.2您必须使用:
import functionOfInterest = require("the-package");
(functionOfInterest as any)(1);
**或者**
您可以将定义文件更改为:
declare function functionOfInterest(val: number): void;
export = functionOfInterest;
然后在没有强制转换的情况下消费到any
:
import functionOfInterest = require("the-package");
functionOfInterest(1);
您必须使用:
import functionOfInterest = require("the-package");
(functionOfInterest as any)(1);
**或者**
您可以将定义文件更改为:
declare function functionOfInterest(val: number): void;
export = functionOfInterest;
然后在没有强制转换的情况下消费到any
:
import functionOfInterest = require("the-package");
functionOfInterest(1);
试试这个:
import * as _ thePackage from "the-package";
thePackage.functionOfInterest(1);
试试这个:
import * as _ thePackage from "the-package";
thePackage.functionOfInterest(1);
对于Typescript 2.2.2,此错误应为:
[ts]字符串文字。[ts]“from”应为。任何带有Typescript 2.2.2的
,此错误应为:[ts]字符串文字。[ts]“from”应为。任何