It's possible to reference a second arg
typescript
In JavaScript you can reference a previous argument from a function but on TypeScript the first argument can reference the second argument!
type Indices = 1 | 2 | 3;
type GroupedKeys<T> = T extends `${infer U}${Indices}` ? U : never;
function add<
Obj extends Record<`${BaseKey}${Indices}`, any>,
BaseKey extends GroupedKeys<keyof Obj>
>(_obj: Obj, _baseKey: BaseKey) {
return null as any;
}
const order = {
price1: 10,
price2: 20,
price3: 30,
};
add(order, "price");
The code above is avaiablable in this playground.
You can read the full article from Maxime
I learnt this from @maxime1992