I wonder if there's a way of extracting a type of an existing type(that already had an extended type to it)
Example:
type GameInfo = {
id: number,
name: string,
version: string
} & HttpRequestError;
type HttpRequestError = {
timestamp?: string,
status?: number,
status_code?: number,
error?: string,
message?: string,
};
Now GameInfo has the extended type HttpRequestError. I would like to undo that and only get GameInfo type.
Something like:
const GameInfoWithoutHttpType = GameInfo extract HttpRequestError;
Is this possible?
ThanKS!
CodePudding user response:
Yes, you can use a combination of the Omit
utitlity type with keyof HttpRequestError
type GameInfo = {
id: number,
name: string,
version: string
} & HttpRequestError;
type HttpRequestError = {
timestamp?: string,
status?: number,
status_code?: number,
error?: string,
message?: string,
};
type WithoutHttp = Omit<GameInfo, keyof HttpRequestError>
CodePudding user response:
If you're using a version >= 3.5, you can achieve that using Omit :
type GameInfoWithoutHttpType = Omit<GameInfo, keyof HttpRequestError>;
CodePudding user response:
Create a utility type that decomposes arbitrary intersection types
type RemoveIntersection<I, R> = I extends R & infer T ? T : I;
And then then apply it to the type in question
type GameInfoWithoutHttpError = RemoveIntersection<GameInfo, HttpRequestError>;