TS演示:https://tsplay.dev/mLO04N , 问题:当我将reduce
中返回对象的children
注释去掉,返回的类型就变成any
了,请问正确姿势应该是?
interface LocationItem {
code: string;
name: string;
cities?: Record<string, LocationItem>;
districts?: Record<string, string>;
}
interface Option {
value: string | number;
label: string;
children?: Option[];
}
const objectEntries = <T extends object, K = keyof T>(obj: T) => Object.entries(obj) as Array<[K, T[keyof T]]>;
const transform = (data: Record<string, LocationItem | string>|undefined = {}) =>
objectEntries(data).reduce((buf, [key, value]) => {
if (typeof value === "string") {
return [...buf, { label: value, value: key }];
}
const { code, name, cities, districts } = value;
const _cities = transform(cities);
const _districts = transform(districts);
const children = _cities.length ? _cities : _districts.length ? _districts : undefined;
return [
...buf,
{
label: name,
value: code,
// children,
},
];
}, [] as Option[]);
我把提取的值像这样复制给一个对象,获取的类型是对的,但是如果把这个对象作为返回数据类型就不对了
请不要试图类似用下面断言的方式来解决,我试图通过正确的姿势获取数据类型:
const _cities = transform(cities) as Options[];
const _districts = transform(districts) as Options[];