npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@toreda/types

v2.16.0

Published

Common mappings, aliases, and types used in Toreda TypeScript packages.

Downloads

1,201

Readme

Toreda

CI Coverage Sonar Quality Gate

GitHub package.json version (branch) GitHub Release Date GitHub issues

license

 

@toreda/types

Improve readability, reduce redundancy. Functional & Expressive Types used in Toreda packages.

 

Contents

 

Object API

Resettable

Interface indicating implementer provides a reset method.

Example

import type {Resettable} from '@toreda/types';

class MyObj implements Resettable {
	public reset(): void {
		console.log('boop');
	}
}

const o = new MyObj();
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

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();

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

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();

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 --dev

NPM

$ yarn add @toreda/types --D

 

Legal

License

MIT © Toreda, Inc.

Copyright

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

Website

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