typescript 使用 keyof 运算符获取打字稿类的属性类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45894524/
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
Getting type of a property of a typescript class using keyof operator
提问by bmdelacruz
As stated in the documentation of Typescript about the keyof
operator, one can get a property of an object instance using the function below.
正如 Typescript 的关于keyof
操作符的文档中所述,可以使用下面的函数获取对象实例的属性。
function getProperty<T, K extends keyof T>(o: T, name: K) {
return o[name];
}
Of course, one can get the type of the property by replacing return o[name]
into return typeof o[name]
. Is there a way to retrieve the type of the property without passing any object instance?
当然,可以通过替换return o[name]
成来获得属性的类型return typeof o[name]
。有没有办法在不传递任何对象实例的情况下检索属性的类型?
function getPropertyType<T>(name: keyof T) {
// something like T[name]?
}
回答by Oscar
Is this what you're looking for?
这是你要找的吗?
type PropType<TObj, TProp extends keyof TObj> = TObj[TProp];
and get type of an object property by doing:
并通过执行以下操作获取对象属性的类型:
type MyPropType = PropType<ObjType, '<key>'>;
which is the same as the way of using Pick
in typescript, and it can report compile error if there's any invalid key
passed in.
与Pick
typescript中的使用方式相同,如果key
传入无效,则可以报告编译错误。
回答by jcalz
Of course, one can get the type of the property by replacing
return o[name]
intoreturn typeof o[name]
.
当然,可以通过替换
return o[name]
成来获得属性的类型return typeof o[name]
。
Not really... if you did that:
不是真的......如果你这样做:
function getTypeofProperty<T, K extends keyof T>(o: T, name: K) {
return typeof o[name];
}
You would get a return value of the type "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
, because you're using the JavaScript typeof
operatorat runtime, which returns a string like "object"
, not the compile-timetype seen by TypeScript.
您将获得 type 的返回值"string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
,因为您在运行时使用JavaScripttypeof
运算符,它返回一个类似 的字符串,而不是TypeScript 看到的编译时类型。"object"
TypeScript also uses the keyword typeof
as the compile-time type query operator. You can tell the difference by looking at the places where it appears. The type query typeof
only appears in places where you are writing a type. For example:
TypeScript 还使用关键字typeof
作为编译时类型查询运算符。您可以通过查看它出现的位置来区分。类型查询typeof
仅出现在您编写类型的地方。例如:
const a = { foo: 0 };
const b: Array<typeof a> = [{ foo: 1 }, { foo: 2 }, {foo: 3}];
const c: string = typeof a; // "object"
In b
, you can see that typeof a
appears where you would write a type expression, so it is a TypeScript type query, and Array<typeof a>
is evaluated as Array<{foo: number}>
. On the other hand, in c
, typeof a
appears where you would write an value expression, so it is the JavaScript typeof
operator, and will evaluate to the string "object"
at runtime.
在 中b
,您可以看到typeof a
出现在您将要编写类型表达式的位置,因此它是一个 TypeScript 类型查询,并且Array<typeof a>
被评估为Array<{foo: number}>
。在另一方面,在c
,typeof a
出现在那里你会写一个值表达式,所以它是JavaScripttypeof
操作,并且将评估字符串"object"
在运行时。
As @k0pernikus mentioned, TypeScript doesn't (and doesn't intend to) allow you to get compile-time type information at runtime. So there is no typeof
operator which acts at runtime and returns compile-time information.
正如@k0pernikus 所提到的,TypeScript 不允许(也不打算)允许您在运行时获取编译时类型信息。所以没有typeof
在运行时起作用并返回编译时信息的运算符。
Of course, if you want type information about a property at compile time, you cando that, using what's called lookup types. Let's examine the return value of that getProperty()
function:
当然,如果您想在编译时获得有关属性的类型信息,您可以使用所谓的查找类型来实现。让我们检查该getProperty()
函数的返回值:
function getProperty<T, K extends keyof T>(o: T, name: K) {
return o[name];
} // hover over getProperty to see that the return value is of type T[K]
That T[K]
in a type position means "the type of property with a key of type K
on an object of type T
". Since this is a type-level operation, you can do it at compile-time without declaring any values of the type T
or K
. For example,
这T[K]
在类型位置的装置“的属性类型的密钥的类型K
类型的对象上T
”。由于这是一个类型级别的操作,因此您可以在编译时执行此操作,而无需声明类型T
或 的任何值K
。例如,
type RegExpFlags = RegExp['flags']; // RegExpFlags is string
const flags: RegExpFlags = 'ig';
Here, you are looking up the "flags"
key of the type of RegExp
objects and getting back the string
type, and you can declare a value of type RegExp['flags']
without having a value of type RegExp
anywhere.
在这里,您正在查找对象"flags"
类型的键RegExp
并取回string
类型,并且您可以声明类型的值而RegExp['flags']
无需在RegExp
任何地方都有类型的值。
That's the closest I can come to answering your question without more information about what you need. Hope that helps. Good luck!
这是我在没有更多有关您需要的信息的情况下回答您的问题的最接近的方法。希望有帮助。祝你好运!