V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
V2EX  ›  tobemaster56  ›  全部回复第 9 页 / 共 9 页
回复总数  162
1  2  3  4  5  6  7  8  9  
2022 年 1 月 10 日
回复了 tobemaster56 创建的主题 TypeScript ts 中,访问对象的属性,如何获得更准确的类型
@chouchoui 正常工作,谢谢
2022 年 1 月 10 日
回复了 tobemaster56 创建的主题 TypeScript ts 中,访问对象的属性,如何获得更准确的类型
@wunonglin 这里是被我简化了,我的场景是获取 React 的 context 的值, 参考 2 楼的回答,已经解决了

```
import * as React from 'react';
import { ConfigConsumerProps, ConfigContext } from '../ConfigContext';

type ConfigConsumerKeys = keyof ConfigConsumerProps;

type ConfigType = Exclude<
ConfigConsumerKeys,
'rootPrefixCls' | 'iconPrefixCls' | 'locale' | 'theme'
>;

export function useComponentConfig(): ConfigConsumerProps;
export function useComponentConfig<T extends ConfigType>(
type: T
): ConfigConsumerProps[T];

export function useComponentConfig<T extends ConfigType = ConfigType>(
configType?: T
): ConfigConsumerProps[T] | ConfigConsumerProps {
const config = React.useContext(ConfigContext);
if (configType) {
return config[configType];
}
return config;
}
```
1  2  3  4  5  6  7  8  9  
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1356 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 9ms · UTC 16:41 · PVG 00:41 · LAX 09:41 · JFK 12:41
♥ Do have faith in what you're doing.