Skip to content

jankaspar/ts-discriminated-union

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript Discriminated Union

This is work in progress, may not work at all and documentation may be misleading.

Represent discriminated unions in typescript.

  • compile time exhaustive matching, never miss the case
  • correct return type inferred (if one case return string and other number result is string | number)

More detailed explanation of types

Api

You can match on union of types having their descriptors.

var unionKind = union(...typeDescriptors);
type Union = typeof unionKind.type // typeDescriptor1 type | typeDescriptor2 type | ...

var value = unionKind.match(value)
  .case(typeDescriptor1, _ => /*... what to return ...*/ )
  .case(typeDescriptor2, _ => /*... what to return ...*/ )
  ...
  .default(_ => /* catch all case */)
  .result()

TypeDescriptor is either any class or instance of TypeMatcher:

interface TypeMatcher<T>{
  match(type: any): type is T
}

Library includes type descriptor for plain objects with type property.

p. e. for objects

{
  type: 'NAMED',
  name: string
}

you can create descriptor

const namedKind = createTypeDescriptor<{ name: string }, 'NAMED'>('NAMED');

Examples: classes, plain objects.

Limitations

Currently types are generated for union of max 24 types (this can be increased by UNION_MAX constant in /src/union.ts.tmpl.

About

Represent discriminated unions in typescript.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published