Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为VS代码IntelliSense的现有Node.js模块创建TypeScript接口_Node.js_Typescript_Visual Studio Code - Fatal编程技术网

为VS代码IntelliSense的现有Node.js模块创建TypeScript接口

为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

考虑以下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.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


希望这有帮助。

谢谢,但这是一个我不想要的实现。订单仍然是任何类型的。