简体   繁体   English

使用 keyof Type 在 Type 上查找属性

[英]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)的类型pProps 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

Typescript Playground Link Typescript 游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 keyof 运算符获取打字稿类的属性类型 - Getting type of a property of a typescript class using keyof operator 使用泛型和keyof时推断回调参数的属性类型 - Deduce property type of callback argument when using generics and keyof Typescript 使用获取<t> (key: keyof T): T[key] --- 返回类型是 T 上属性的具体类型,而不是 T[keyof T]</t> - Typescript using get<T>(key: keyof T): T[key] --- return type to be specifically the type of the property on T, not T[keyof T] 使用过滤的 keyof 类型索引多态“this”类型 - Indexing polymorphic "this" type using filtered keyof type Typescript:在相邻属性类型中使用 keyof 值 - Typescript: Use keyof value in adjacent property type 如何使用 Zod 实现“Property in keyof Type”形状? - How to implement "Property in keyof Type" shape with Zod? Angular:类型“this[keyof this]”上不存在属性“next” - Angular: Property 'next' does not exist on type 'this[keyof this]' 如何使用 keyof 属性定义通用打字稿类型? - How to define generic typescript type with keyof property? Typescript:通过通用类型的缩小键索引时的对象属性类型 - Typescript: object property type when indexed by narrowed keyof generic type TypeScript:使用 keyof 索引到 object 类型 - TypeScript: Indexing into an object type using keyof
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM