demo.js
应用程序中包含的节点:
var Demo = function () {
let self = this;
self.sayGoodbye = (x) => `goodbye ${x}`;
};
module.exports = new Demo();
我会在一个普通的js文件中使用
const demo = require('./demo');
demo.sayGoodbye('foo');
demo.d.ts
就住在附近
export = Demo
declare class Demo {
sayGoodbye(x: string) : string
}
在我新的typescript文件中
run.ts
我写了以下内容:
const dem = require('./demo');
console.log(dem.sayGoodbye('foo'));
当我盘旋在
dem
变量(在VSCode中)类型列为
any
我没有得到类型信息。
这是我的配置
{
"compilerOptions": {
"sourceMap": true, // allow sourcemap support
"strictNullChecks": true, // enable strict null checks as a best practice
"module": "commonjs", // specify module code generation,
"moduleResolution": "Node",
"target": "es2015", // specify ECMAScript target version
"allowJs": true, // allow a partial TypeScript and JavaScript codebase
"experimentalDecorators": false,
"types" : ["node"],
"baseUrl":"."
},
"include": [
"./**/*.ts",
],
"exclude": [
"node_modules"
]
}
如果有人能解释一下我在d.ts上出了什么问题,或者需要我的陈述,我将不胜感激。
更新:
我把d.ts文件改成这样:
interface Demo {
sayGoodbye(x: string) : string
}
declare var demo : Demo
export = demo
因为我认为前一个并不代表文件。如果我理解正确的话,初始的导出应该是一个构造函数函数而不是一个实例。
如果我现在在运行.js到
import dem = require('./demo');
console.log(dem.sayGoodbye('foo'));
我现在有了智能感。不幸的是,transpiled JS现在有以下行:
Object.defineProperty(exports, "__esModule", { value: true });
它不是一个交易破坏者,但我想知道我如何才能删除它,但保留作为非模块的代码的导入要求。