V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  tobemaster56  ›  全部回复第 9 页 / 共 9 页
回复总数  162
1  2  3  4  5  6  7  8  9  
2022-01-10 18:43:43 +08:00
回复了 tobemaster56 创建的主题 TypeScript ts 中,访问对象的属性,如何获得更准确的类型
@chouchoui 正常工作,谢谢
2022-01-10 18:43:22 +08:00
回复了 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  
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   5341 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 10ms · UTC 07:45 · PVG 15:45 · LAX 23:45 · JFK 02:45
♥ Do have faith in what you're doing.