我想在打字稿项目中使用一个小的javascript库。 (对于full example see here)。
javascript库定义了以下内容:
"use strict";
exports.__esModule = true;
exports["default"] = functionOfInterest;
function functionOfInterest(val) { }
module.exports = exports["default"];
使用.d.ts
文件键入此内容:
export default function functionOfInterest(val: number): void;
在我使用的打字稿文件中:
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"
]
}
我确定某处只是一个简单的配置错误,但我现在无法解决这个问题。
Typescript 2.2.2
答案 0 :(得分:0)
你必须使用:
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);
答案 1 :(得分:0)
试试这个:
import * as _ thePackage from "the-package";
thePackage.functionOfInterest(1);