[Typescript] Infer from an Interface
In this exercise we have an interface MyComplexInterface which is acting as a type helper.
The interface takes arguments for Event, Context, Name, and Point, each being put into getEvent, getContext, getName, and getPoint, respectively.
interface MyComplexInterface<Event, Context, Name, Point> {
getEvent: () => Event;
getContext: () => Context;
getName: () => Name;
getPoint: () => Point;
}
We want a GetPointtype, just return what getPointholds
type GetPoint<T> = T extends MyComplexInterface<any, any, any, infer TPoint>
? TPoint
: never;
import { Equal, Expect } from "../helpers/type-utils";
interface MyComplexInterface<Event, Context, Name, Point> {
getEvent: () => Event;
getContext: () => Context;
getName: () => Name;
getPoint: () => Point;
}
type Example = MyComplexInterface<
"click",
"window",
"my-event",
{ x: 12; y: 14 }
>;
type GetPoint<T> = T extends MyComplexInterface<any, any, any, infer TPoint>
? TPoint
: never;
type tests = [Expect<Equal<GetPoint<Example>, { x: 12; y: 14 }>>];

浙公网安备 33010602011771号