[英]Using keyof Type to lookup property on Type
I have the following types我有以下类型
type Type = {
prop1: number;
prop2: string;
prop3: someOtherType
}
type Props = keyof Type
now I know you can use an "indexed access type" to get the type of propN, ie type Num = Type["prop1"]
, however I would like to use the following in a function现在我知道您可以使用“索引访问类型”来获取 propN 的类型,即type Num = Type["prop1"]
,但是我想在 function 中使用以下内容
function(p: Props) {
// ...
let something = somethingElse as Type[p]
// Typescript can't infer the type
// ...
}
so that when i call这样当我打电话时
let a = function("prop1");
// a should be of type number
However that is not possible, and throws然而,这是不可能的,并抛出
'p' refers to a value, but is being used as a type here. Did you mean 'typeof p'?
however if I use typeof p
, then Type[typeof p]
gives me the union type number | string | someOtherType
但是,如果我使用typeof p
,那么Type[typeof p]
会给我联合类型number | string | someOtherType
number | string | someOtherType
number | string | someOtherType
. number | string | someOtherType
。
Is there some way to fix this.有没有办法解决这个问题。 I have tried to look at using generices, but it doesn't seem possible.我曾尝试过使用泛型,但似乎不可能。
So I think what you are trying to achieve can be done using a generic function like so:所以我认为你想要实现的目标可以使用通用的 function 来完成,如下所示:
function foo<T extends keyof Type>(p: T) {
let something = somethingElse as T
}
More complete example:更完整的例子:
type ValueOf<T> = T[keyof T];
type Type = {
prop1: number;
prop2: string;
prop3: boolean;
}
type Props = keyof Type;
function foo<T extends Props>(p: T): Type[T] {
let something = "";
return something as Type[T];
}
const a = foo("prop1"); // a is number
const b = foo("prop2"); // b is string
const c = foo("prop3"); // c is boolean
When you call typeof p
you get the Typescript type of the Javascript variable p
.当您调用typeof p
时,您会得到 Javascript 变量p
的 Typescript 类型。 You have said right here function(p: Props)
that the type of p
is Props
.您在这里已经说过function(p: Props)
的类型p
是Props
。 That type means that it can be any of the keys of Props
.该类型意味着它可以是Props
的任何键。
You must use a generic if you want your code to know which key p
is.如果您希望您的代码知道哪个键p
是,您必须使用泛型。 Otherwise that information is discarded and the type of p
is widened to Props
by function(p: Props)
.否则,该信息将被丢弃,并且p
的类型通过function(p: Props)
扩大到Props
。
You can use your indexed access type assertion if the type of p
is generic.如果p
的类型是通用的,则可以使用索引访问类型断言。
function myFunc<P extends Props>(p: P) {
// ...
let something = somethingElse as Type[P]
// Typescript can't infer the type
// ...
}
But you might not even need to assert with as
if everything is typed properly.但是您甚至可能不需要断言 with as
好像所有内容都已正确键入一样。
type Type = {
prop1: number;
prop2: string;
}
type Props = keyof Type
function myFunc<P extends Props>(p: P) {
const type: Type = { prop1: 0, prop2: "" };
return type[p]; // type is inferred as `Type[P]`
}
const one = myFunc("prop1"); // type number
const two = myFunc("prop2"); // type string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.