为VS代码IntelliSense的现有Node.js模块创建TypeScript接口
考虑以下Node.js项目结构:为VS代码IntelliSense的现有Node.js模块创建TypeScript接口,node.js,typescript,visual-studio-code,Node.js,Typescript,Visual Studio Code,考虑以下Node.js项目结构: - entities - customer.js - node_modules - typings - index.js - jsconfig.json - package.json 当有这样一个实体/customer.js时: function Customer() { this.firstname = null; this.lastname = null; this.orders = [] } Customer.proto
- entities
- customer.js
- node_modules
- typings
- index.js
- jsconfig.json
- package.json
当有这样一个实体/customer.js
时:
function Customer() {
this.firstname = null;
this.lastname = null;
this.orders = []
}
Customer.prototype.addOrder = function (order) {
this.orders.push(order);
};)
module.exports = Customer
declare module 'customer'
{
export class customer
{
public firstName: string;
public lastName: string;
public addOrder(order: any): void;
}
}
在/index.js
中,使用了实体/customer.js
:
let Customer = require('./entities/customer');
let customer = new Customer();
customer.firstname = "John";
customer.lastname = "Doe";
customer.addOrder({ amount: 5, product : 'Apple' })
我如何声明一个TypeScript接口
来支持VS code IntelliSense,从而不仅显示addOrder(param:any):void
,而且将所有内容放在何处?
我不希望使用TypeScript实现来替换当前的Customer
模块
当然,我可以使用JSDoc注释(感谢VS代码中的Salsa语言服务):
但是为了理解,我对打字稿版本感兴趣
作为奖励,我希望成为
order
类型的order
您可以使用如下所示的d.ts文件实现这一点:
function Customer() {
this.firstname = null;
this.lastname = null;
this.orders = []
}
Customer.prototype.addOrder = function (order) {
this.orders.push(order);
};)
module.exports = Customer
declare module 'customer'
{
export class customer
{
public firstName: string;
public lastName: string;
public addOrder(order: any): void;
}
}
你可以把它放在根目录下的任何文件夹中。比如说“手工打字”
然后在index.js中:
let Customer = require('./entities/customer');
let c = new Customer();
c.addOrder(123);
在您的tsconfig.js集合“allowJs”=true
希望这有帮助。谢谢,但这是一个我不想要的实现。订单仍然是任何类型的。