Exploring a scenario where I have a generic interface that requires a type argument extending another generic type.
export interface IPoint<TX, TY>
{
x: TX;
y: TY;
}
export interface ISeries<TPoint extends IPoint>
{
points: Array<TPoint>;
}
The challenge arises when defining TX
and TY
for IPoint
.
An inquiry emerges: is there a way to automatically deduce these types? For instance, like this:
export interface ISeries<TPoint extends IPoint<infer TX, infer TY>>
{
points: Array<TPoint>;
}
Currently, the only workaround I've discovered is specifying TX
and TY
as type parameters for ISeries
, which can be cumbersome due to repeated type specifications.
Alternatively, using IPoint<any, any>
lacks precision regarding the actual types of x
and y
.
To provide more context on the desired outcome, consider this example:
export interface ISeries<TPoint extends IPoint<infer TX, infer TY>>
{
points: Array<TPoint>;
transformYValues?: (yValue: TY) => number;
}
In this case, requiring TY
to robustly type transformYValues
becomes essential.
Thank you for your assistance.
UPDATE: Managed to find a solution (credit to captain-yossarianfromUkraine).
export interface ISeries<TPoint extends IPoint<any, any>>
{
points: Array<TPoint>;
transformYValues?: (yValue: TPoint['y']) => number;
}
The key lies in TPoint['y']
, leveraging Indexed Access Types to aptly define methods/properties within the interface. (See https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html).
To sum up => employ any
typing in generic type constraints, then utilize indexed access types for precise typing of methods/properties within the interface.