1.0.0 • Published 5 years ago

frlluc-utils v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

TS utils

Build Status Coverage Status npm version Dependency status Dev Dependency Status License

This little project contains a few quality-of-life utils.

A. Installation

If you are using npm, then simply run the npm install frlluc-utils CLI command to get the latest version.

If you are using yarn, then go with the yarn add frlluc-utils command.

B. Types

There are a few util types and interfaces:

  • Optional\<T> - represents a union type of T and undefined.

  • Nullable\<T> - represents a union type of T and null.

  • Const\<T> - represents a deep readonly T type.

  • IConstArray\<T> - an interface that extends the ReadonlyArray<Const<T>> interface.

  • IConstMap\<K, V> - an interface that extends the ReadonlyMap<Const<K>, Const<V>> interface.

  • IConstSet\<T> - an interface that extends the ReadonlySet<Const<T>> interface.

C. Functions

Let's assume we have two classes, Foo and Bar, where Bar extends Foo.

There are two util functions:

  • reinterpretCast\<T> - allows to force cast an object to the specified type.Be very careful with this function, because you can do stuff like this:
const bar = new Bar();
// yep, this works!
const result = reinterpretCast<string>(bar);
  • dynamicCast\<T> - allows to safely cast an object to the specified type of which it is an instanceof, otherwise returns null.
const bar = new Bar();
// returns bar as a Foo object
const foo = dynamicCast(Foo, bar);
// returns null
const str = dynamicCast(String, bar);