【发布时间】:2019-07-27 19:44:41
【问题描述】:
在 TypeScript 中,下面的 useContext 是否可以从 Provider 中选择并返回值类型?
function Provider<Props = {value: number}>(props: Props) { }
function useContext<Value>(context: Context<Value>): Pick<Value, 'value'> { return 0 }
这样“Consumer”在使用时会返回type: number
function Consumer<Props>(props: Props) { return useContext(Provider) }
...有关其他上下文:
interface Component<Props = {}> { (props: Properties<Props>): Node }
interface Context<Value> extends Component<{value: Value}> {}
或者,给定
function Provider({value, children}: {value: string, children: any}) {
return h(Context, {value: 'value'}, children)
}
const value = useContext(Provider)
假设函数“h”返回一个接口,如何将常量“值”归因于对象 {value: 'value'} 中的“值”:
interface {type: Context, props: {value: 'value}, children: children}
我尝试挑选,道具>,其中类型是“提供者”,然后从挑选的结果中挑选,道具>,'值'>。
然而这似乎不起作用。
【问题讨论】:
标签: typescript typescript-typings