type ArrayElement<ArrayType extends readonly unknown[]> =
ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type ELE = ArrayElement<typeof KeyToVal>;
像这样的ELE类型
输入图片描述
如何更改为
type Result = {
setTitle: () => number;
getName: (name: '123') => "123";
}
给定如下类型:
type ELE = {
readonly name: "setTitle"
readonly method: () => number
} | {
readonly name: "getName"
readonly method: (name: "123") => "123"
}
你可以通过映射联合得到想要的结果。
type Result = {
[E in ELE as E["name"]]: E["method"]
}
// type Result = {
// setTitle: () => number;
// getName: (name: "123") => "123";
// }
游乐场