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

ts-type-safe

v1.4.1

Published

Utilities for a more type-safe TypeScript

Downloads

212

Readme

view on npm NPM

Modules

Functions

types

types~Prettify : Prettify

Kind: inner typedef of types

types~DeepRequired : DeepRequired

Kind: inner typedef of types
Example

type Settings = {
  org: string;
  repo?: string;
  owner?: {
      profileUrl?: string;
      contact: {
        name: string;
        mail: string;
      } | undefined;
  };
};
type ReqSettings = DeepRequired<Settings>;
// type ReqSettings = {
//     org: string;
//     repo: string;
//     owner: {
//         profileUrl: string;
//         contact: {
//             name: string;
//             mail: string;
//         };
//     };
// }

types~DeepPartial : DeepPartial

Kind: inner typedef of types
Example

type Foo = {
    bar: string;
    baz: {
        foo: [
            bam: string;
        ]
    }
}

type Foo2 = DeepPartial<Foo>;

// type Foo2 = {
//     bar?: string | undefined;
//     baz?: {
//         foo?: [bam?: string | undefined] | undefined;
//     } | undefined;
// }

types~ValuesOf : ValuesOf

Kind: inner typedef of types
Example

const Foo = { A: "a", B: "b"} as const;
type FooVals = ValuesOf<typeof Foo>;
// type FooVals = "a" | "b"

// equivalent to: type FooVals = (typeof Foo)[keyof typeof Foo];

types~KeysOf : KeysOf

Kind: inner typedef of types
Example

const Foo = { A: "a", B: "b"};
type FooKeys = KeysOf<typeof Foo>;
// type FooKeys = "A" | "B"

// equivalent to: type FooKeys = keyof typeof Foo;

types~DeepKeysOf<TBase, : DeepKeysOf

Kind: inner typedef of types
Example

type Settings = {
  org: string;
  repo?: string;
  owner?: {
      profileUrl?: string;
      contact: {
        name: string;
        mail: string;
      } | undefined;
  };
  messages: {
    title: string;
    body: string;
  }[];
};
type SettingKey = DeepKeysOf<Settings>;
// type SettingKey = "org" | "owner" | "owner.contact" | "owner.contact.name" | "owner.contact.mail" | "owner.profileUrl" | "repo" | "messages" | "messages[0]" | "messages[0].title" | "messages[0].body";

TypeScript Playground Link: https://www.typescriptlang.org/pt/play/?#code/KYDwDg9gTgLgBDAnmYcAixhgErAI4CuAllMACYA8AKgHxwC8cVcoMwAdmQM5wBiB7AMYwiEdgCg4cAPxNJcAFxMWINpx4BBKFACGiCkXYAzYFDgBVGvNlbd+jFlyES5CpatSlzVh25wIAEYAVsDC1nAA3nAA2gDScIZwANbAiBBGTAC6ALTSSg44+MSklAByYqUEADZVOgFVwNRxmTR0AL7yXgDc4uJIKOiYYLGpXADyRgASwFUoUNQAQjpcwAA0TAAKpEZEICpqflwwUIYA5gxwAESXdIwR8nEJ7Mmp6UxLKzl578vAzfu+HgACkMJjM5gAlNFMvIpLJzAD1P5gqEYLCpDJ0RiMQAfOAAAwAJBEqFtgDsQG1ifEAGRwI4ndinNr4rHYuB4okkskUqkRWn045nNrRYnsAgAWwCpjamVZ7PZeIKI0Q4ymMzmbnWXNJ212fIFDOFooi4qlMsyADp8R52UodTz9dS4HSjUyRWLJdKoLL5RivB8-rFMoi-IEQmEFdI2djOcTdeSnfyXYLGcy-QqpEqhiq1dNZqZFr9mtr447Kc7XUL3dbbdj7WW9RXk1W0yyeiKUmkMlRA5keuJQJBYAhkKhlaMJtRboMsLmJvnNQUnMVXLQaAP+qgAMrAGAiJk8O7yaCnJRu049KSkSDfC9X-wAd3Ypm+9wVYCg6SIDXMUCqd7VpebKCGIMA6MISjvpm7A6BKwDnkBD4KhKOg-ohabIRibQcnAAhkImL5kA+bQPvBXBcDopzAFwb6wiIMANBhZzIQEEBkIgzFMiR0IdpuY5wLu+5nCqFwTqqU5CQepxcBuvSgewRz0nu0kqgAjEoUkiakFyXBAz6mJaCngcIlqoT+lw9ApSkrMJTIqgATJpKnaYgunkZR1FcNEakOQAzFaDENJZQA

types~PrefixedKeys<T, : PrefixedKeys

Kind: inner typedef of types
Example

const Foo = { A: "a", B: "b"};
type FooType = typeof Foo;
// type FooType = { "A": string; "B": string; }

type PrefixedFooKeys = PrefixedKeys<typeof Foo, 'foo.'>;
// type PrefixedFooKeys = { "foo.A": string; "foo.B": string; }

types~PartialBy<T, : PartialBy

Kind: inner typedef of types
Example

type Person = {
  id: string;
  name: string;
  age: number;
};

type NewPerson = PartialBy<Person, 'id'>
//    ^? type NewPerson = Omit<Person, "id"> & Partial<Pick<Person, "id">>
type PrettyNewPerson = Prettify<NewPerson>;
//    ^? type PrettyNewPerson = { name: string; age: number; id? : string | undefined; }

Example

type NewPerson = PartialBy<Person, 'id' | 'age'>;
//    ^? type NewPerson = Omit<Person, "id" | "age"> & Partial<Pick<Person, "id" | "age">>

types~RequiredBy<T, : RequiredBy

Kind: inner typedef of types
Example

type Person = {
  id?: string;
  name?: string;
  age?: number;
};

type NewPerson = RequiredBy<Person, 'id' | 'name'>
//    ^? type NewPerson = Omit<Person, "id" | "name"> & Required<Pick<Person, "id" | "name">>
type PrettyNewPerson = Prettify<NewPerson>;
//    ^? type PrettyNewPerson = { name: string; age: number; id? : string | undefined; }

classNames

classNames~classNames(...names)

Kind: inner method of classNames

| Param | Description | | --- | --- | | ...names | Array of string, undefined or false |

Example

<div className={cns('primary', !isValid && 'disabled')} />

validators

validators~isObject(value)

Kind: inner method of validators

| Param | Description | | --- | --- | | value | to check |

validators~isDefined(value)

Kind: inner method of validators

| Param | Description | | --- | --- | | value | to check |

validators~hasOwnProperty(obj, propKey)

Kind: inner method of validators

| Param | Description | | --- | --- | | obj | to check | | propKey | which may or may not exist on the obj |

validators~hasOwnProperties(obj, propKeys)

Kind: inner method of validators

| Param | Description | | --- | --- | | obj | to check | | propKeys | list of @propKeys which may or may not exist on the obj |

validators~isNonEmptyArray(obj)

Kind: inner method of validators

| Param | Description | | --- | --- | | obj | to check |

validators~isEmptyArray(obj)

Kind: inner method of validators

| Param | Description | | --- | --- | | obj | to check |

validators~isEnumKey(enumType, value)

Kind: inner method of validators

| Param | Description | | --- | --- | | enumType | the type to check against | | value | some value to check if it is a key of the given @enumType |

Example

enum MyEnum {
 Thing1 = 'thing one',
 Thing2 = 'thing two',
}

function onlyKeys(key: keyof typeof MyEnum) {
  console.log(key, MyEnum[key]);
}

const testStr = "Thing2";

if (isEnumKey(MyEnum, testStr)) {
  // compiler knows that testStr is of type `keyof typeof MyEnum`
  onlyKeys(testStr);
}

validators~isEnumValue(enumType, value)

Kind: inner method of validators

| Param | Description | | --- | --- | | enumType | the type to check against | | value | some value to check if it is a value of the given @enumType |

Example

enum MyEnum {
 Thing1 = 'thing one',
 Thing2 = 'thing two',
}

function onlyVals(val: MyEnum) {
  console.log("onlyVals", val);
}

const testStr = "thing two";

if (isEnumValue(MyEnum, testStr)) {
  // compiler knows that testStr is of type `MyEnum`
  onlyVals(testStr);
}

isMathematicalNumber()

Kind: global function


© 2024 Hans Krebs