Restrict keyof 'Type' according to return type

岁酱吖の 提交于 2020-12-13 04:55:10

问题


How can I restrict the keys of an object to only those that return a certain type?

In the example below I want to ensure that the type of the property is a function, so that I can execute obj[key]().

interface IObj{
  p1:string,
  p2:number,
  p3:()=>void
}

const obj:IObj = {
  p1:'str',
  p2:5,
  p3:()=>void 0
}

function fun<TKey extends keyof IObj>(key: IObj[TKey] extends ()=>void? TKey:never){
  const p = obj[key]
  p(); // This expression is not callable.
       // Not all constituents of type 'string | number | (() => void)' are callable.
       // Type 'string' has no call signatures.(2349)
}

playground


回答1:


You can map the type to property name/never and then index into it, you have half of that already:

type FunctionKeys = {
    [K in keyof IObj]: IObj[K] extends () => void ? K : never
}[keyof IObj]; // <-

function fun(key: FunctionKeys) {
    const p = obj[key];
    p();
}

The indexing will perform a union over all the types of the properties and A | never is just A.

(Extracted the type because it's so long. Maybe there is a more elegant method.)



来源:https://stackoverflow.com/questions/64484348/restrict-keyof-type-according-to-return-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!