Skip to content

Instantly share code, notes, and snippets.

@gunzip
Created December 7, 2017 22:27
Show Gist options
  • Save gunzip/b8106b31fb8eeec1e27c9ec2c2ccbfb3 to your computer and use it in GitHub Desktop.
Save gunzip/b8106b31fb8eeec1e27c9ec2c2ccbfb3 to your computer and use it in GitHub Desktop.
function withDefault<T extends t.Any>(
type: T,
defaultValue: t.TypeOf<T>
): t.Type<any, t.TypeOf<T>> {
return new t.Type(
type.name,
(v: any): v is T => type.is(v),
(v: any, c: any) => type.validate(v !== undefined ? v : defaultValue, c),
(v: any) => type.serialize(v)
);
}
export type TimeToLiveWithDefault = t.TypeOf<typeof TimeToLiveWithDefault>;
export const TimeToLiveWithDefault = withDefault(TimeToLive, 3600 as any);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment