gettype获取类名_在TypeScript中运行时获取对象的类名

在TypeScript中,可以通过访问实例的`constructor.name`属性或者`Class.name`来获取对象的类名。然而,需要注意的是,使用缩小代码时,类名可能会改变。另外,可以使用正则表达式从构造函数的字符串形式中提取类名,或者在构造函数内部为对象分配一个`typeName`属性来避免缩小问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在TypeScript中运行时获取对象的类名

是否可以使用typescript在运行时获取对象的类/类型名称?

class MyClass{}

var instance = new MyClass();

console.log(instance.????); // Should output "MyClass"

Adam Mills asked 2019-04-11T03:14:30Z

9个解决方案

306 votes

简单回答:

class MyClass {}

const instance = new MyClass();

console.log(instance.constructor.name); // MyClass

console.log(MyClass.name); // MyClass

但是:请注意,使用缩小代码时名称可能会有所不同。

Mikael Couzic answered 2019-04-11T03:14:50Z

21 votes

我知道我迟到了,但我发现这也有效。

var constructorString: string = this.constructor.toString();

var className: string = constructorString.match(/\w+/g)[1];

另外...

var className: string = this.constructor.toString().match(/\w+/g)[1];

上面的代码将整个构造函数代码作为字符串获取并应用正则表达式来获取所有“单词”。 第一个单词应该是'function',第二个单词应该是该类的名称。

希望这可以帮助。

answered 2019-04-11T03:15:27Z

19 votes

看到这个问题。

由于TypeScript被编译为JavaScript,因此在运行时您运行的是JavaScript,因此将应用相同的规则。

Matt Burland answered 2019-04-11T03:16:01Z

15 votes

我的解决方案不是依赖类名。 object.constructor.name在理论上有效。 但是如果你在像Ionic这样的东西上使用TypeScript,那么一旦你进入制作阶段,它就会起火,因为Ionic的制作模式会缩小Javascript代码。 因此,类被命名为“a”和“e”。

我最终做的是在构造函数为其分配类名的所有对象中都有一个typeName类。 所以:

export class Person {

id: number;

name: string;

typeName: string;

constructor() {

typeName = "Person";

}

是的,这不是问的,真的。 但是使用constructor.name来解决可能会缩小规模的问题只是让人头疼。

SonOfALink answered 2019-04-11T03:16:42Z

8 votes

您需要先将实例强制转换为any,因为Function的类型定义没有name属性。

class MyClass {

getName() {

return (this).constructor.name;

// OR return (this as any).constructor.name;

}

}

// From outside the class:

var className = (new MyClass()).constructor.name;

// OR var className = (new MyClass() as any).constructor.name;

console.log(className); // Should output "MyClass"

// From inside the class:

var instance = new MyClass();

console.log(instance.getName()); // Should output "MyClass"

更新:

使用TypeScript 2.4(可能更早),代码可以更清晰:

class MyClass {

getName() {

return this.constructor.name;

}

}

// From outside the class:

var className = (new MyClass).constructor.name;

console.log(className); // Should output "MyClass"

// From inside the class:

var instance = new MyClass();

console.log(instance.getName()); // Should output "MyClass"

Westy92 answered 2019-04-11T03:17:20Z

5 votes

在Angular2中,这有助于获取组件名称:

getName() {

let comp:any = this.constructor;

return comp.name;

}

comp:any是必需的,因为typescript compile会发出错误,因为Function最初没有属性名。

Admir Sabanovic answered 2019-04-11T03:17:54Z

3 votes

不得不添加“.prototype”。 使用:error TS2339: Property 'name' does not exist on type 'Function'。

否则使用以下代码:error TS2339: Property 'name' does not exist on type 'Function',我有打字稿错误:

error TS2339: Property 'name' does not exist on type 'Function'。

Flox answered 2019-04-11T03:18:34Z

3 votes

完整的TypeScript代码

public getClassName() {

var funcNameRegex = /function (.{1,})\(/;

var results = (funcNameRegex).exec(this["constructor"].toString());

return (results && results.length > 1) ? results[1] : "";

}

Nati Krisi answered 2019-04-11T03:19:05Z

0 votes

如果您已经知道期望的类型(例如,当方法返回联合类型时),那么您可以使用类型保护。

例如,对于原始类型,您可以使用一种类型的保护:

if (typeof thing === "number") {

// Do stuff

}

对于复杂类型,您可以使用instanceof guard:

if (thing instanceof Array) {

// Do stuff

}

Cocowalla answered 2019-04-11T03:19:43Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值