我想在typescript项目中使用一个小的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;在我使用的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"
]
}我确信只是某个地方有一个简单的配置错误,但我现在找不出来。
Typescript 2.2.2
发布于 2017-04-26 00:34:49
你必须使用:
import functionOfInterest = require("the-package");
(functionOfInterest as any)(1);** Alternatively **
您可以将定义文件更改为:
declare function functionOfInterest(val: number): void;
export = functionOfInterest;然后在不强制转换为any的情况下进行消费
import functionOfInterest = require("the-package");
functionOfInterest(1);发布于 2017-04-26 00:57:56
试试这个:
import * as _ thePackage from "the-package";
thePackage.functionOfInterest(1);https://stackoverflow.com/questions/43616320
复制相似问题