1.1.0 • Published 4 months ago

ts-type-safe v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

view on npm NPM

Modules

Functions

types

types~Prettify : Prettify

Kind: inner typedef of types

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

classNames

classNames~classNames(...names)

Kind: inner method of classNames

ParamDescription
...namesArray of string, undefined or false

Example

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

validators

validators~isObject(value)

Kind: inner method of validators

ParamDescription
valueto check

validators~isDefined(value)

Kind: inner method of validators

ParamDescription
valueto check

validators~hasOwnProperty(obj, propKey)

Kind: inner method of validators

ParamDescription
objto check
propKeywhich may or may not exist on the obj

validators~hasOwnProperties(obj, propKeys)

Kind: inner method of validators

ParamDescription
objto check
propKeyslist of @propKeys which may or may not exist on the obj

validators~isNonEmptyArray(obj)

Kind: inner method of validators

ParamDescription
objto check

validators~isEmptyArray(obj)

Kind: inner method of validators

ParamDescription
objto check

validators~isEnumKey(enumType, value)

Kind: inner method of validators

ParamDescription
enumTypethe type to check against
valuesome 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

ParamDescription
enumTypethe type to check against
valuesome 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


© 2023 Hans Krebs

1.1.0

4 months ago

1.0.13

8 months ago

1.0.12

8 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago