gpt4 book ai didi

typescript - 如何解决 TS7009 TypeScript 错误?

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:25 25 4
gpt4 key购买 nike

我是 Typescript 编程的新手。现在我正处于学习阶段。通过在后端控制台中收到错误,我在编码过程中遇到了问题。上面提到的代码是:

function employee(id:number,name:string) { 
this.id = id
this.name = name
}

var emp = new employee(123,"Smith")
employee.prototype.email = "smith@abc.com"

console.log("Employee 's Id: "+emp.id)
console.log("Employee's name: "+emp.name)
console.log("Employee's Email ID: "+emp.email)

输出@浏览器控制台是:

www.ts:10 Employee 's Id: 123
www.ts:11 Employee's name: Smith
www.ts:12 Employee's Email ID: smith@abc.com

Node控制台的错误是:

[0] www/www.ts(6,15): error TS7009: 'new' expression, whose target lacks
a construct signature, implicitly has an 'any' type.

请帮我解决这个错误。谢谢....

最佳答案

在 TypeScript 中,你应该只在类上使用 new。考虑像这样重写它:

class Employee {
id: number;
name: string;
email: string;

constructor(id:number, name:string) {
this.id = id;
this.name = name;
}
}

let emp = new Employee(123,"Smith");
emp.email = "smith@abc.com";

我不明白你试图通过原型(prototype)属性分配实现什么。

关于typescript - 如何解决 TS7009 TypeScript 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42106306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com