如何检索 React 上下文值的类型?

Gab*_*bin 5 typescript reactjs react-context

我使用的库上下文不导出其值的类型。它看起来是这样的:

type LocationContextObject = { /* some properties */ };

export type LocationContext = React.Context<LocationContextObject>;
Run Code Online (Sandbox Code Playgroud)

我需要访问LocationContextObject,是否可以从中提取它LocationContext

这是我所做的:

type LocationContextObject = ComponentProps<typeof LocationContext.Provider>['value'];
Run Code Online (Sandbox Code Playgroud)

它确实有效,但看起来像黑客。所以我想知道是否有更优雅的解决方案。

Ale*_*pel 5

import { ContextType } from 'react';

type LocationContextObject = ContextType<typeof LocationContext>;
Run Code Online (Sandbox Code Playgroud)