在TypeScript中,是否有一种类似JavaScript的方式来使用函数创建对象?

huangapple go评论66阅读模式
英文:

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.

在TypeScript中,是否有一种类似JavaScript的方式来使用函数创建对象?

我期望能够像使用函数一样创建对象

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.
在TypeScript中,是否有一种类似JavaScript的方式来使用函数创建对象?

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 classes (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 classes (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

huangapple
  • 本文由 发表于 2023年3月7日 14:45:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定