Socket
Socket
Sign inDemoInstall

ts-type-safe

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-type-safe

Utilities for a more type-safe TypeScript


Version published
Weekly downloads
10
decreased by-41.18%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

ts-type-safe

Usefuly utility functions and types for a more type-safe TypeScript

view on npm NPM

Modules

types

Helper types to improve type-safety.

classNames

Helper function to simplify type-safe interaction with classNames.

validators

Validators to improve type-safety.

Functions

isMathematicalNumber()

consider mathematical number if:

  • typeof number
  • can parse to int without need to remove anything (i.e. leading zeroes)
  • can parse to float without need to remove anything

types

Helper types to improve type-safety.

  • types
    • ~Prettify : Prettify
    • ~ValuesOf : ValuesOf
    • ~KeysOf : KeysOf
    • ~PrefixedKeys<T, : PrefixedKeys
    • ~PartialBy<T, : PartialBy

types~Prettify : Prettify

Helper type to show all properties of a complex base-type

Kind: inner typedef of types

types~ValuesOf : ValuesOf

Helper type to generate values of a given type

NOTE: not for enum-types!

To create values of an enum-type, use:

type EnumVals = `${EnumType}`;

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

Helper type to generate keys of a given type

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~PrefixedKeys<T, : PrefixedKeys

Helper type to generate prefixed keys of a given type

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

Helper type to make a single property optional

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

classNames

Helper function to simplify type-safe interaction with classNames.

classNames~classNames(...names)

Joins classes and avoids complicated checks and usage of nasty string-literals.

note: exported also as cns-shorthand

Kind: inner method of classNames

ParamDescription
...names

Array of string, undefined or false

Example

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

validators

Validators to improve type-safety.

validators~isObject(value)

Checks if value is not null and of object-type.

Kind: inner method of validators

ParamDescription
value

to check

validators~isDefined(value)

Returns true if value is not undefined and not null.

Kind: inner method of validators

ParamDescription
value

to check

validators~hasOwnProperty(obj, propKey)

Checks existence of @propKey on an object and retypes the @obj as an object having that property of unknown-type.

Kind: inner method of validators

ParamDescription
obj

to check

propKey

which may or may not exist on the obj

validators~hasOwnProperties(obj, propKeys)

Checks existence of @propKeys on an object and retypes the @obj as an object having these properties, all of which of unknown-type.

Kind: inner method of validators

ParamDescription
obj

to check

propKeys

list of @propKeys which may or may not exist on the obj

validators~isNonEmptyArray(obj)

Checks if @obj is an array with at least one entry.

Kind: inner method of validators

ParamDescription
obj

to check

validators~isEmptyArray(obj)

Checks if @obj is an array with zero entries.

Kind: inner method of validators

ParamDescription
obj

to check

validators~isEnumKey(enumType, value)

Typeguard for enums-keys

note: not for number-enums

Kind: inner method of validators

ParamDescription
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)

Typeguard for enum values

note: not for number-enums

Kind: inner method of validators

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

consider mathematical number if:

  • typeof number
  • can parse to int without need to remove anything (i.e. leading zeroes)
  • can parse to float without need to remove anything

Kind: global function


© 2023 Hans Krebs

Keywords

FAQs

Last updated on 02 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc