Skip to content

Repository files navigation

Toreda

CIGitHub issues

GitHub package.json version (branch)GitHub Release Date

license

@toreda/shared-types

Common shared types and expressive aliases for TypeScript packages.

Contents

Object API

Resettable

Interface indicating implementer provides a reset method.

Example

importtype{Resettable}from'@toreda/shared-types';classMyObjimplementsResettable{publicreset(): void{console.log('boop');}}consto=newMyObj();o.reset();

Clearable

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.

Example

importtype{Clearable}from'@toreda/shared-types';classMyObjimplementsClearable{publicclear(): boolean{console.log('boop');returntrue;}}consto=newMyObj();constresult=o.clear();

Stringable

Interface indicating implementer provides a toString() method which returns the object contents as a string. Typically used for serialization although usage may vary.

Example

importtype{Stringable}from'@toreda/shared-types';classMyObjimplementsStringable{publica: string;publicb: string;constructor(){this.a='aaaa';this.b='bbbb';}publictoString(): string|null{consto={a: this.a,b: this.b};letresult: string|null=null;try{result=JSON.stringify(o);}catch(e){if(einstanceofError){console.log(`toString exception: ${e.message}.`);}}returnresult;}}consto=newMyObj();constresult=o.clear();

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/shared-types'

Use

constmyValue: Primitive=null;

Stringable

Implementer's contents can be converted to a string by calling toString().

Import

import{Stringable}from'@toreda/shared-types'

Use

exportclassMyClassimplementsStringable{publictoString(): 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.exporttypeData<T>=Record<string,T|T[]|null>;

Expressive Types

Express value intent & purpose with type definitions.

BitMask

Import

import{BitMask}from'@toreda/shared-types'

Use

// Declare and initialize number while also expressing the value's purpose.letmask: BitMask=0x1;// Becomes more clear when expecting values:functionuseValue(mask: BitMask): void{
...
}// versus:functionuseValue(mask: number): void{
...
}

Example

// Expressive Type alias.exporttypeBigId=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.functionvalidateId(id: BigId): void{
...
}

Legal

License

MIT © Toreda, Inc.

Copyright

Copyright © 2019 - 2026 Toreda, Inc. All Rights Reserved.

Website

Toreda's company website can be found at toreda.com

About

Expressive type aliases and functional types commonly used in Toreda packages.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages