SimplyTyped
Yet another typing library.
This differs by aiming to be less experimental than others, driven by industry use cases.
Many of the exposed types are a very thin layer above built in functionality.
The goal is to provide all of the building blocks necessary to make concise, yet complex types.
Conditionals
Some (somewhat common) implementations of conditional type logic
If
type x = If<True, string, number>
type y = If<False, string, number>
And
type x = If<And<True, True>, string, number>
type y = If<And<True, False>, string, number>
...
Or
type x = If<Or<True, False>, string, number>
type y = If<Or<False, False>, string, number>
...
Not
type x = Not<True>
type y = Not<False>
Objects
type obj1 = { w: string, x: string, y: number }
type obj2 = { y: string, z: number }
Keys
No different than keyof
, but can look a bit nicer when nesting many types deep
type x = keyof obj1
type y = Keys<obj1>
ObjectType
On its own, not that interesting.
Takes an object and makes it an object type.
Is useful when combined with &
intersection types (as seen next).
type x = ObjectType<obj1>
CombineObjects
Takes the intersection between two objects, and flattens them.
This can make extremely complex types look much nicer.
type x = obj1 & obj2
type y = CombineObjects<obj1, obj2>
SharedKeys
Gets all of the keys that are shared between two objects (as in keys in common).
type x = SharedKeys<obj1, obj2>
DiffKeys
Gets all of the keys that are different from obj1 to obj2.
type x = DiffKeys<obj1, obj2>
type y = DiffKeys<obj2, obj1>
AllKeys
Gets all keys between two objects.
type x = AllKeys<obj1, obj2>
Omit
Gives back an object with listed keys removed.
type x = Omit<obj1, 'w' | 'x'>
Merge
Much like _.merge
in javascript, this returns an object with all keys present between both objects, but conflicts resolved by rightmost object.
type x = Merge<obj1, obj2>
DeepPartial
Uses Partial
to make every parameter of an object optional (| undefined
).
type x = DeepPartial<obj1>
Tuples
A tuple can be defined in two ways: [number, string]
which as of Typescript 2.7 has an enforced length type parameter: [number, string]['length'] === 2
or using this libraries Tuple<any>
which can be extended with any length of tuple: function doStuff<T extends Tuple<any>>(x: T) {}
.
Tuple
function doStuff<T extends Tuple<any>>(x: T) {}
doStuff(['hi', 'there']);
UnionizeTuple
type x = UnionizeTuple<[number, string]>
Strings
Diff
Get the differences between two unions of strings.
type x = Diff<'hi' | 'there', 'hi' | 'friend'>