简体   繁体   English

如何让 TypeScript 通用 function 类型参数是 object 的键值的返回类型

[英]How to have TypeScript generic function with type argument being keyof an object have return type of that object's key's value

I have a several object types:我有几个 object 类型:

type Slave = {
  myKey:string
}

type AnotherSlave = {
  anotherKey:string
}

And a master type that contains some keys, and these object types as those keys' values as below:还有一个包含一些键的主类型,这些 object 类型作为这些键的值如下:

type Master = {
  key1: Slave
  key2: AnotherSlave
}

I have a generic function:我有一个通用的 function:

myFunc<T = keyof Master>(key:T){
  const someObj = {} // external call that returns "any" type
  return someObj; // I want to cast it to some strongly type here.
}

The someObj is guaranteed to have the type from Master[T] 's value, in other words: someObj 保证具有来自Master[T]值的类型,换句话说:

If the key is key1 , someObj is the type of Slave .如果 key 是key1someObjSlave的类型。 If the key is key2 , someObj is the type of AnotherSlave .如果 key 是key2someObjAnotherSlave的类型。 I've wrote the types depending on what's coming from the external call of someObj anyway so it's guaranteed to have that type.无论如何,我已经根据 someObj 的外部调用的内容编写了类型,因此可以保证具有该类型。

I naively tried return someObj as Master[T] but it errs Type 'T' cannot be used to index type 'Master' .我天真地尝试return someObj as Master[T]但它错误Type 'T' cannot be used to index type 'Master'

How can I make the function return strongly typed key's value type?如何使 function 返回强类型键的值类型? I'm on TypeScript 4.6.3.我在 TypeScript 4.6.3 上。

You can specify a return value and use type assertion on the returned someObj您可以指定返回值并在返回的someObj上使用类型断言

function myFunc<T extends keyof Master>(key:T): Master[T] {
  const someObj = {}
  return someObj as Master[T];
}

const test = myFunc("key1") // <-- type is Slave

Playground Link 游乐场链接

暂无
暂无

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

相关问题 使用接口的键索引 object 而值是具有该键的返回类型的 function - index an object with the keyof an interface while the value is a function with the return type of that key 如果打字稿是使用`keyof`的泛型函数,为什么打字稿不能推断出它的类型? - Why can't typescript infer an argument's type if it is a generic function using `keyof`? 打字稿:keyof返回值的泛型类型限制 - Typescript: generic type restriction on return value of keyof Typescript:- 使用 object 为 function 创建泛型类型,其中一个键作为值类型,返回类型是值 - Typescript :- create generic type for function taking object having one key as type of value and return type is value JSDoc:如何定义一个等于对象属性名称的类型——比如 Typescript 的 keyof? - JSDoc: how to define a type that's equal to object's property names- like Typescript's keyof? 通过 typescript 中函数的可选参数返回不同的泛型类型 - return different generic type via function's optional argument in typescript Typescript:使用泛型类型的键作为函数的参数 object 键 - Typescript: Use generic type's keys as function's argument object keys 如何将类型约束为通用 object 中的键,其值为字符串 - How to constrain type to be a key in a generic object who's value is a string 如何在打字稿中为对象的通用键类型分配值类型? - How assign a value type to a generic key type of an object in typescript? Typescript:通过通用类型的缩小键索引时的对象属性类型 - Typescript: object property type when indexed by narrowed keyof generic type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM