typescript 检查一个对象(或只是一个类)是否有一个属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36308570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if an object (or just a class) has a property
提问by zeroin
Lets say I have a class:
假设我有一堂课:
module MyModule {
export class MyClass {
x:number;
y:number;
}
}
Then I have a string, "x". How can I check if MyClass has property "x"?
然后我有一个字符串,“x”。如何检查 MyClass 是否具有属性“x”?
If I create an instance of MyClass and then do:
如果我创建 MyClass 的一个实例,然后执行以下操作:
myClassInstance.hasOwnProperty("x");
it returns false unless x has a default value set. But I don't want to set default values for each property. It would be best if I even could do this without creating instance of MyClass.
除非 x 设置了默认值,否则它返回 false。但我不想为每个属性设置默认值。如果我什至可以在不创建 MyClass 实例的情况下做到这一点,那将是最好的。
采纳答案by NineBerry
TypeScript is compiled to JavaScript before execution. In the final JavaScript code, the type information from TypeScript is not available anymore. So there is no possibility to check TypeScript type information at runtime.
TypeScript 在执行前被编译为 JavaScript。在最终的 JavaScript 代码中,来自 TypeScript 的类型信息不再可用。因此无法在运行时检查 TypeScript 类型信息。
If you want to do a check at compile-time, you can use interfaces as in the following example:
如果要在编译时进行检查,可以使用接口,如下例所示:
interface IHasX
{
x:any;
}
class MyClassA {
x:number;
y:number;
}
class MyClassB {
y:number;
}
function Test(param: IHasX)
{
// Do something with param.x
}
var objA = new MyClassA();
var objB = new MyClassB();
Test(objA);
Test(objB);
The last line Test(objB);
will fail to compile because the TypeScript compiler knows that objB
is of type MyClassB
and that that class has no property named x
as required by the interface IHasX
.
最后一行Test(objB);
将无法编译,因为 TypeScript 编译器知道它objB
是类型MyClassB
,并且该类没有x
按接口要求命名的属性IHasX
。
回答by pcan
Now you can do this. I released an enhanced version of the TypeScript compiler that allows you to list all members of a class or an interface. For example you can do the following:
现在你可以这样做了。我发布了一个增强版的 TypeScript 编译器,它允许你列出一个类或一个接口的所有成员。例如,您可以执行以下操作:
export class Person {
name: string;
surname: string;
age: number;
country: string;
}
function printMembers(clazz: Class) {
let fields = clazz.members.filter(m => m.type.kind !== 'function'); //exclude methods.
for(let field of fields) {
let typeName = field.type.kind;
if(typeName === 'class' || typeName === 'interface') {
typeName = (<Class | Interface>field.type).name;
}
console.log(`Field ${field.name} of ${clazz.name} has type: ${typeName}`);
}
}
//then, somewhere in your code:
let p = new Person();
printMembers(p.constructor.getClass());
and this is the output:
这是输出:
$ node main.js
Field name of Person has type: string
Field surname of Person has type: string
Field age of Person has type: number
Field country of Person has type: string
You can find all the details you need here
您可以在此处找到所需的所有详细信息