@mvst/ts-unions
Version:
TypeScript union types for Maybe and RemoteData with pattern matching
58 lines (57 loc) • 2.82 kB
TypeScript
export type RemoteData<TSuccess, TError = Error> = {
type: "Error";
error: TError;
} | {
type: "Loading";
} | {
type: "NotAsked";
} | {
type: "Success";
value: TSuccess;
};
type Pattern<TSuccess, TError, TResult> = {
_?: () => TResult;
error?: (error: TError) => TResult;
loading?: () => TResult;
notAsked?: () => TResult;
success?: (value: TSuccess) => TResult;
};
declare function notAsked<TSuccess, TError = Error>(): RemoteData<TSuccess, TError>;
declare function loading<TSuccess, TError = Error>(): RemoteData<TSuccess, TError>;
declare function success<TSuccess, TError = Error>(value: TSuccess): RemoteData<TSuccess, TError>;
declare function error<TSuccess, TError = Error>(error: TError): RemoteData<TSuccess, TError>;
declare function isSuccess<TSuccess, TError = Error>(remoteData: RemoteData<TSuccess, TError>): remoteData is {
type: "Success";
value: TSuccess;
};
declare function isError<TSuccess, TError = Error>(remoteData: RemoteData<TSuccess, TError>): remoteData is {
type: "Error";
error: TError;
};
declare function isLoading<TSuccess, TError = Error>(remoteData: RemoteData<TSuccess, TError>): remoteData is {
type: "Loading";
};
declare function isNotAsked<TSuccess, TError = Error>(remoteData: RemoteData<TSuccess, TError>): remoteData is {
type: "NotAsked";
};
interface CurryWithDefault {
<TSuccess, TError = Error>(defaultValue: TSuccess, remoteData: RemoteData<TSuccess, TError>): TSuccess;
<TSuccess, TError = Error>(defaultValue: TSuccess): (remoteData: RemoteData<TSuccess, TError>) => TSuccess;
}
declare const withDefault: CurryWithDefault;
interface CurriedWhen {
<TSuccess, TError, TResult>(pattern: Pattern<TSuccess, TError, TResult>, remoteData: RemoteData<TSuccess, TError>): TResult;
<TSuccess, TError, TResult>(pattern: Pattern<TSuccess, TError, TResult>): (remoteData: RemoteData<TSuccess, TError>) => TResult;
}
declare const when: CurriedWhen;
interface CurriedMap {
<TSuccess, TError, TResult>(fn: (value: TSuccess) => TResult, remoteData: RemoteData<TSuccess, TError>): RemoteData<TResult, TError>;
<TSuccess, TError, TResult>(fn: (value: TSuccess) => TResult): (remoteData: RemoteData<TSuccess, TError>) => RemoteData<TResult, TError>;
}
declare const map: CurriedMap;
interface CurriedAndThen {
<TSuccess, TError, TResult>(fn: (value: TSuccess) => RemoteData<TResult, TError>, remoteData: RemoteData<TSuccess, TError>): RemoteData<TResult, TError>;
<TSuccess, TError, TResult>(fn: (value: TSuccess) => RemoteData<TResult, TError>): (remoteData: RemoteData<TSuccess, TError>) => RemoteData<TResult, TError>;
}
declare const andThen: CurriedAndThen;
export { andThen, error, isError, isLoading, isNotAsked, isSuccess, loading, map, notAsked, success, when, withDefault, };