Skip to content

Combine user-defined type guards / type predicates as unions and intersections.

License

Notifications You must be signed in to change notification settings

buschtoens/combine-type-predicates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

combine-type-predicates

CI npm version Download Total code style: prettier
Dependabot enabled dependencies Status devDependencies Status

Combine user-defined type guards / type predicates as unions and intersections.

import { isSome, isEvery } from 'combine-type-predicates';

type Foo = { foo: boolean; baz: number };
type Bar = { bar: symbol; baz: string };

const isFoo = (v: unknown): v is Foo
  => typeof v.foo === 'boolean' && typeof v.baz === 'number';
const isBar = (v: unknown): v is Bar
  => typeof v.bar === 'symbol' && typeof v.baz === 'string';

const isFooOrBar = isSome(isFoo, isBar);
// => (subject: unknown) => subject is Foo | Bar

const isFooAndBar = isEvery(isFoo, isBar);
// => (subject: unknown) => subject is Foo & Bar

const x: unknown = undefined;

if (isFooOrBar(x)) {
  x.baz; // => string | number

  if ('foo' in x) {
    x.foo; // => boolean
  } else {
    x.bar; // => symbol
  }
}

if (isFooAndBar(x)) {
  x.foo; // => boolean
  x.bar; // => symbol
  x.baz; // => never (no such thing as `string & number`)
}