Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
feat: add omittable type
Browse files Browse the repository at this point in the history
  • Loading branch information
hanayashiki committed Mar 20, 2022
1 parent 53cf520 commit f0c5017
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"url": "https://github.com/MonoidDev/reform"
},
"private": false,
"version": "1.0.3",
"version": "1.1.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './react/factories';
export * from './fields/stringField';
export * from './fields/numberField';
export * from './fields/booleanField';
export * from './types/UtilityTypes';
2 changes: 1 addition & 1 deletion src/types/LiteralType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { makeLeft, makeRight } from './Either';
import { ErrorMessage } from './ErrorMessage';
import { makeResolver, Resolver } from './Resolver';

export type LiteralValue = string | number | boolean;
export type LiteralValue = string | number | boolean | null | undefined;

export interface TLiteral<T extends LiteralValue>
extends Resolver<unknown, T, ErrorMessage, 'literal'> {
Expand Down
5 changes: 5 additions & 0 deletions src/types/UtilityTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AnyResolver, literal } from '..';
import { union } from './CombinationTypes';

export const omittable = <R extends AnyResolver>(r: R) =>
union([r, literal(undefined)]);
26 changes: 26 additions & 0 deletions test/utilityTypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { omittable } from '../src';
import {
any,
array,
boolean,
nullType,
number,
object,
string,
undefinedType,
} from '../src/types/BasicTypes';
import { InputOf, OutputOf } from '../src/types/infer';
import { literal } from '../src/types/LiteralType';
import { struct } from '../src/types/StructType';
import { expectLeft, expectRight } from './utils';

describe('utility types', () => {
it('omittable', () => {
expectRight(omittable(string()).resolve('x'));
expectRight(omittable(string()).resolve(undefined));
expectLeft(omittable(string()).resolve(1));

expectRight(omittable(number()).resolve(1));
expectRight(omittable(number()).resolve(undefined));
});
});

1 comment on commit f0c5017

@vercel
Copy link

@vercel vercel bot commented on f0c5017 Mar 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

The most recent charge for your active payment method has failed. Please update it here: https://vercel.com/teams/monoid-dev/settings/billing.

Please sign in to comment.