frlluc-utils v1.0.0
TS utils
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
andundefined
.Nullable\<T> - represents a union type of
T
andnull
.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 returnsnull
.
const bar = new Bar();
// returns bar as a Foo object
const foo = dynamicCast(Foo, bar);
// returns null
const str = dynamicCast(String, bar);
5 years ago