1

我想实现一个概念上简单的事情:将返回的函数映射any(或者,为了示例,number)转换为返回的函数映射void,同时保留其他类型信息。

用代码表示,我想做这样的事情:

type Source = {
    [fn: string]: (...args: any[]) => number
}

type Output = {
    [fn: string]: (...args: any[]) => void
}

function proxy(source: Source): Output {
    // Do something with the source...
    return source
}

const result = proxy({
    answerToLifeTheUniverseAndEverything: (question?: string): number => 42
})

// Should work, and the method should be suggested as part of the auto-completion.
result.answerToLifeTheUniverseAndEverything()

// The compiler should complain.
result.answerToLifeTheUniverseAndEverything(false)
4

0 回答 0