JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 39
  • Score
    100M100P100Q39894F
  • License ISC

Utilities for a more type-safe TypeScript

Package Exports

  • ts-type-safe
  • ts-type-safe/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-type-safe) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-type-safe

Utilities and types for a more type-safe TypeScript

view on npm

Modules

types

Helper types to improve type-safety.

classNames

Helper function to simplify type-safe work with classNames.

validators

Validators to improve type-safety.

types

Helper types to improve type-safety.

types~Prettify : Prettify

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

Kind: inner typedef of types

types~ValuesOf : ValuesOf

Helper type generates values of a given type

NOTE: not for enum-types! Use type EnumVals = `${EnumType}`; to create values of an enum-type

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 generates 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;

classNames

Helper function to simplify type-safe work with classNames.

classNames~classNames()

Helper-Function to join multiple classes and to avoid usage of nasty string-literals

Kind: inner method of classNames

validators

Validators to improve type-safety.

validators~hasOwnProperty()

Checks existence of @propKey on an object and retypes the @obj

Kind: inner method of validators

validators~isEnumKey()

Typeguard for enums-keys

Kind: inner method of validators
Example

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

function testKeys(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`
  testKeys(testStr);
}

validators~isEnumValueGenerator()

https://stackoverflow.com/questions/58278652/generic-enum-type-guard

Kind: inner method of validators

validators~isEnumValue(enumType, value)

Typeguard for enum values

Kind: inner method of validators
Todo:: take care of number-Enums

Param
enumType
value

Example

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

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

const testStr = "thing two";

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

© 2023 Hans Krebs