my-only-either
Version:
Only support either interface
70 lines (69 loc) • 2.31 kB
TypeScript
interface ILeft<T> {
type: "left";
left: T;
}
interface IRight<T> {
type: "right";
right: T;
}
type Either<TLEFT, IRIGHT> = ILeft<TLEFT> | IRight<IRIGHT>;
declare function right<IRIGHT>(value: IRIGHT): IRight<IRIGHT>;
declare function left<TLEFT>(value: TLEFT): ILeft<TLEFT>;
declare function isLeft<TLEFT>(value: Either<TLEFT, any>): value is ILeft<TLEFT>;
declare function isRight<IRIGHT>(value: Either<any, IRIGHT>): value is IRight<IRIGHT>;
type TPickLeft<T extends Either<any, any>> = [
T
] extends [
Either<infer U, any>
] ? U : never;
type TPickRight<T extends Either<any, any>> = [
T
] extends [
Either<any, infer U>
] ? U : never;
type TPickILeft<T extends Either<any, any>> = [
T
] extends [
Either<infer U, any>
] ? ILeft<U> : never;
type TPickIRight<T extends Either<any, any>> = [
T
] extends [
Either<any, infer U>
] ? IRight<U> : never;
interface IPass<T> {
type: "pass";
pass: T;
}
interface IFail<T> {
type: "fail";
fail: T;
}
type PassFailEither<TFAIL, IPASS> = IFail<TFAIL> | IPass<IPASS>;
declare function pass<TPASS>(value: TPASS): IPass<TPASS>;
declare function fail<TFAIL>(value: TFAIL): IFail<TFAIL>;
declare function epass<TPASS>(value: TPASS): IPass<TPASS>;
declare function efail<TFAIL>(value: TFAIL): IFail<TFAIL>;
declare function isFail<TFAIL>(value: PassFailEither<TFAIL, unknown>): value is IFail<TFAIL>;
declare function isPass<TPASS>(value: PassFailEither<unknown, TPASS>): value is IPass<TPASS>;
type TPickFail<T extends PassFailEither<unknown, unknown>> = [
T
] extends [
PassFailEither<infer U, unknown>
] ? U : never;
type TPickPass<T extends PassFailEither<unknown, unknown>> = [
T
] extends [
PassFailEither<any, infer U>
] ? U : never;
type TPickIFail<T extends PassFailEither<any, any>> = [
T
] extends [
PassFailEither<infer U, any>
] ? IFail<U> : never;
type TPickIPass<T extends PassFailEither<any, any>> = [
T
] extends [
PassFailEither<any, infer U>
] ? IPass<U> : never;
export { ILeft, IRight, Either, right, left, isLeft, isRight, TPickLeft, TPickRight, TPickILeft, TPickIRight, IPass, IFail, PassFailEither, pass, fail, epass, efail, isFail, isPass, TPickFail, TPickPass, TPickIFail, TPickIPass };