A TypeScript Metaprogramming Challenge

preview_player
Показать описание
Just a cool TypeScript thing I've come across in my day-to-day work!

Рекомендации по теме
Комментарии
Автор

I solved this without having this duplicate typing of props:

type Prop<T> = ReturnType<typeof createProp<T>>;

type TypeMap = Record<PropertyKey, any>;

type Props<K extends TypeMap = TypeMap> = {
[P in keyof K]: Prop<K[P]>
};

type GetTypeMap<T extends Props> = {
[K in keyof T]: ReturnType<T[K]["get"]>
};

function asProps<T extends Props>(props: T) {
return props as Props<GetTypeMap<T>>;
}

function x<K extends keyof typeof props>(k: K) {
const p = asProps(props);
const y = p[k];
const z = y.get();
y.set(z);
}

antonpieper
welcome to shbcf.ru