Package Exports
- @toreda/types
- @toreda/types/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@toreda/types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@toreda/types
Improve readability, reduce redundancy. Functional & Expressive Types used in Toreda packages.
Contents
Functional Types
Types & aliases provide shorthand to reduce code duplication and simplify statements.
DeepRequired<T>
Recursively require all properties on object & children.
Primitive
Implementer's type is any JavaScript primitive.
Import
import {Primitive} from '@toreda/types'Use
const myValue: Primitive = null;
Stringable
Implementer's contents can be converted to a string by calling toString().
Import
import {Stringable} from '@toreda/types'Use
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>;
Expressive Types
Express value intent & purpose with type definitions.
BitMask
Import
import {BitMask} from '@toreda/types'Use
// 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 {
...
}
Install
Yarn
$ yarn add @toreda/types --devNPM
$ yarn add @toreda/types --D
Legal
License
MIT © Toreda, Inc.
Copyright
Copyright © 2019 - 2022 Toreda, Inc. All Rights Reserved.
