省略typescript接口中的函数属性



我想省略一个接口中的所有函数

interface Human {
name: string;
age: number;
walk: () => void;
talk: (word: string) => Promise<void>
}
type HumanWithoutFunctions = RemoveFunctions<Human>
/* expected result:
HumanWithoutFunctions {
name: string;
age: number;
}
*/

给你:

type RemoveFunctions<T> = {
[K in keyof T as T[K] extends Function ? never : K]: T[K]
}

游乐场

最新更新