英文:
Is there any way to create object using function in TypeScript , like JavaScript?
问题
我想在TypeScript中像JavaScript一样使用函数创建对象。
例如:
function Box(a: number, b: number) {
this.width = a;
this.height = b;
}
var box = new Box(3, 5);
我在TypeScript中尝试了这段代码,但它给我抛出了错误 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
,还有 'this' implicitly has type 'any' because it does not have a type annotation.
我期望能够像使用函数一样创建对象
var box = new Box(3, 5);
英文:
I want to create Object using Function in TypeScript like JavaScript.
Eg.
function Box(a,b) {
this.width=a;
this.height=b;
}
var box=new Box(3,5);
I tried this code in TypeScript, It throws me error 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
and also 'this' implicitly has type 'any' because it does not have a type annotation.
I am expecting to create object like using function
var box=new Box(3,5);
答案1
得分: 3
Yes you can, like this:
class Box {
width: number;
height: number;
constructor(a: number, b: number) {
this.width = a;
this.height = b;
}
}
<details>
<summary>英文:</summary>
### Yes you can, like this:
class Box {
width: number;
height: number;
constructor(a: number, b: number) {
this.width = a;
this.height = b;
}
}
答案2
得分: 2
It's possible if you use let Box = function ...
instead of function Box...
with type casting and interfaces, but there are probably other problems here while manually typing.
It's better to just use class
es (see the other answers).
For demo purposes:
interface BoxStatic {
new(a: number, b: number): BoxInstance
}
interface BoxInstance{
width: number
height: number
}
let Box = function (this: BoxInstance, a: number, b: number) {
this.width = a;
this.height = b;
} as unknown as BoxStatic;
var box: BoxInstance = new Box(3, 5)
You can rename BoxInstance
to just Box
then it acts somewhat similarly to the typing produced by a class
.
英文:
It's possible if you use let Box = function ...
instead of function Box...
with type casting and interfaces, but there are probably other problems here while manually typing.<br>
It's better to just use class
es (see the other answers).
For demo purposes:
interface BoxStatic {
new(a: number, b: number): BoxInstance
}
interface BoxInstance{
width: number
height: number
}
let Box = function (this: BoxInstance, a: number, b: number) {
this.width = a;
this.height = b;
} as unknown as BoxStatic;
var box: BoxInstance = new Box(3, 5)
You can rename BoxInstance
to just Box
then it acts somewhat similarly to the typing produced by a class
.
答案3
得分: 1
简短回答是“不行”,因为TypeScript明确区分对象和函数,这就是为什么它对你在函数上使用'new'运算符抱怨的原因。
如另一个回答中所提到的,TypeScript 中通过使用 class
运算符并为类提供一个 constructor
来创建对象。
由于TypeScript是JavaScript的超集,你的代码在技术上可以工作,但由于你正在使用TypeScript,利用那个类型系统是有意义的。
英文:
The short answer is 'No', since Typescript specifically differentiates between objects and functions, which is why it is complaining about your use of the 'new' operator for a function.
As mentioned in another answer, an object in Typescript is created using the class
operator and giving the class a constructor
.
Since Typescript is a superset of javascript, your code would technically work, but since you are using Typescript, it would make sense to leverage that type system
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论