打字稿引用自对象键

时间:2021-04-16 05:34:26

标签: typescript types self-reference

我正在尝试构建一个 typescript 类型,它可以推断当前对象的键并将它们作为 selfMethod 函数的参数。 是否可以引用当前对象的键?我知道 Typescript 不支持自引用,但我相信它不会执行无限递归,因此希望找到解决方案。

const obj = {
    act1(selfMethods) {
        return 1;
    },
    act2(selfMethods) {
        // Intellisense should say "return type is number" amd only allow "act1" | "act2" as possible argument of selfMethods
        return selfMethods('act1');
    }
}

给定类似(用某些东西替换Self

type SelfKeysReference = {[key: string]: (selfMethods: <K extends keyof Self>(key: K) => ReturnType<Self[K]>) => any}

感谢您的帮助!

0 个答案:

没有答案