Improve readability, reduce redundancy. Functional & Expressive Types used in Toreda packages.
Interface indicating implementer provides a reset
method.
import type {Resettable} from '@toreda/types';
class MyObj implements Resettable {
public reset(): void {
console.log('boop');
}
}
const o = new MyObj();
o.reset();
Interface indicating implementer provides a clear()
method. Callers expect true
to be returned when clear
call is successful and false
when it was not successful, or there was nothing to clear.
import type {Clearable} from '@toreda/types';
class MyObj implements Clearable {
public clear(): boolean {
console.log('boop');
return true;
}
}
const o = new MyObj();
const result = o.clear();
Interface indicating implementer provides a toString()
method which returns the object contents as a string. Typically used for serialization although usage may vary.
import type {Stringable} from '@toreda/types';
class MyObj implements Stringable {
public a: string;
public b: string;
constructor() {
this.a = 'aaaa';
this.b = 'bbbb';
}
public toString(): string | null {
const o = {
a: this.a,
b: this.b
};
let result: string | null = null;
try {
result = JSON.stringify(o);
} catch (e){
if (e instanceof Error) {
console.log(`toString exception: ${e.message}.`);
}
}
return result;
}
}
const o = new MyObj();
const result = o.clear();
Types & aliases provide shorthand to reduce code duplication and simplify statements.
Recursively require all properties on object & children.
Implementer's type is any JavaScript primitive.
import {Primitive} from '@toreda/types'
const myValue: Primitive = null;
Implementer's contents can be converted to a string by calling toString()
.
import {Stringable} from '@toreda/types'
export class MyClass implements Stringable {
public toString(): string {
return 'stringified_contents_here';
}
}
Example
// Simple generic mapping. When used only once, exporting a type is overkill, but when used repeatedly
// using a common definition reduces chances for mistakes and reduces line lengths.
export type Data<T> = Record<string, T | T[] | null>;
Express value intent & purpose with type definitions.
import {BitMask} from '@toreda/types'
// Declare and initialize number while also expressing the value's purpose.
let mask: BitMask = 0x1;
// Becomes more clear when expecting values:
function useValue(mask: BitMask): void {
...
}
// versus:
function useValue(mask: number): void {
...
}
Example
// Expressive Type alias.
export type BigId = string;
// BigId is an expressive alias replacing the use of 'string' for id's type. It makes no
// functional difference, however id types often impose character and length limitations meaning
// the value cannot be an arbitrary string. BigId gives the caller context for what string values
// are actually valid & accepted.
function validateId(id: BigId): void {
...
}
$ yarn add @toreda/types --dev
$ yarn add @toreda/types --D
MIT © Toreda, Inc.
Copyright © 2019 - 2022 Toreda, Inc. All Rights Reserved.
Toreda's company website can be found at toreda.com