type-ops
Type-ops -- a collection of useful operators to make type-level programming in TypeScript easier.
Table of contents
- Compatibility
- Install
- Requirements
- Dependencies
- License
- TODO
- Features
Compatibility
TypeScript ~2.9.0
, ^3.0.1
.
Install
npm i type-ops
Requirements
For some operators to work properly, strict mode must be enabled.
Dependencies
None.
License
MIT.
TODO
- Use
dtslint
.
Features
Testing & checking utilities
These types can be used to test an arbitrary type for being conformant to a certain constraint.
expect
Induce a compilation error if TActual
does not resolve to the type of expected
.
Definition
declare ;
Usage
// There will be a compilation error if `IsSameT<I1, I2>' does not resolve to `false':expect.toBefalse;
ExpectT
Induce a compilation error if TActual
does not resolve to TExpected
.
Definition
;
Usage
// Compilation will fail if `IsSameT<I1, I2>' does not resolve to `false':;
IsFalseT
Check if T
is false
.
Does not distribute over unions.
Definition
;
IsNeverT
Check if T
is never
.
Does not distribute over unions.
Definition
;
IsNullableT
Check if T
is nullable.
Does not distribute over unions.
Definition
;
IsOptionalT
Check if T
is optional.
Does not distribute over unions.
Definition
;
IsSameT
Check if T
and U
are of the same shape.
Does not distribute over unions.
Definition
;
IsSubtypeOfT
Check if T
is a subtype of U
.
Does not distribute over unions.
;
IsTrueT
Check if T
is true
.
Does not distribute over unions.
Definition
;
Property selectors
These types can be thought of as "filters" on property keys of a particular type.
KeyofT
Extract all properties of T
.
Distributes over unions.
Definition
;
NotPropertiesOfSubtypeT
Extract all properties of T
which are not a subtype of U
.
Definition
;
NotPropertiesOfTypeT
Extract all properties of T
which are not of the same shape as U
.
Definition
;
OptionalPropertiesT
Extract all optional properties of T
.
Definition
;
PropertiesOfSubtypeT
Extract all properties of T
which are a subtype of U
.
Definition
;
PropertiesOfTypeT
Extract all properties of T
which are of the same shape as U
.
Definition
;
RequiredPropertiesT
Extract all required properties of T
.
Definition
;
Type modifiers
These are just mapped conditional types.
JsonT
Represent T
after JSON serialization round-trip.
Distributes over unions.
Definition
;;;
OmitT
Omit properties K
from T
.
Definition
;
OverwriteT
Overwrite properties K
of T
with matching properties of U
and add properties which are unique to U
.
Definition
;
Usage
;;
PartialDeepT
Recursively make all properties of T
optional.
Definition
;;
ReadonlyDeepT
Recursively make all properties of T
readonly.
Definition
;;
ReplaceT
Replace properties K
of T
with matching properties of U
.
Definition
;
Usage
;;
RequiredDeepT
Recursively make all properties of T
required.
Definition
;;
WithMutableT
Make properties K
of T
mutable.
Definition
;
WithPartialT
Make properties K
of T
optional.
Definition
;
WithReadonlyT
Make properties K
of T
readonly.
Definition
;
WithRequiredT
Make properties K
of T
required.
Definition
;
WritableDeepT
Recursively make all properties of T
mutable.
Definition
;;
MutableT
Make all properties of T
mutable.
Definition
;
Aliases and interfaces
ConstructorT
A constructor of TInstance
s from TArguments
.
Definition
;
DictT
A dictionary of TValue
s.
Definition
FunctionT
A function mapping TArguments
to TResult
.
Definition
;
NullableT
Make T
nullable.
Definition
;
OptionalT
Make T
optional.
Definition
;
PrimitiveT
A primitive.
Definition
;
TaggedT
Make tagged type from T
using tag TTag
.
It can be used to create an opaque alias of T
.
Definition
declare ;declare ;;
Usage
// `I1' is an opaque type alias of `string' tagged w/ `A':;// Type assertion must be used to assign raw `string' to its opaque typedef:;i1 = 'v2' as I1;// i1 = 'v3'; // Compilation will fail.// Underlying raw type (`string') can be retrieved through lookup type:; // `I2' has the same shape as `I1':;;i2 = i1;i1 = i2;; // `I3' is an opaque type alias of `string' tagged w/ `B'.;;// `I1' and `I3' are incompatible:// i1 = i3;// i1 = i3 as I1; // Type assertion will not make any difference.// i3 = i1;// i3 = i1 as I3; // Ditto.; // `I4' has the same shape as `I1':;;i4 = i1;i1 = i4;
RawT
Extract raw type of tagged T
.
Distributes over unions.
Definition
;
TagT
Extract tag from tagged T
.
Distributes over unions.
Definition
;
RetaggedT
Retag a tagged T
using tag TTag
.
Distributes over unions.
Definition
;
Logical operators
AndT
Logical "and" of T
and U
.
Distributes over unions.
Definition
;
NotT
Logical "not" of T
.
Distributes over unions.
Definition
;
OrT
Logical "or" of T
and U
.
Distributes over unions.
Definition
;
XorT
Logical "xor" of T
and U
.
Distributes over unions.
Definition
;
Miscellaneous utilities
NoDistributeT
Prevent distribution over T
.
Definition
;
NoInferT
Prevent type inference on T
.
Definition
;
Usage
declare ;f1, ; // An error sneaks in. declare ;// f2({ p1: 'v1', p2: 'v2' }, { p1: 'v1' }); // Causes compilation error.f2, ;
SelectT
Extract a member of a tagged union T
using TTagKey
as a tag property and TValue
as its type.
Definition
;
Usage
declare ;;; // Resolves to `I1'.