Skip to content

samhuk/type-helpers

Repository files navigation

type-helpers

Useful Common Typescript Types

license npm version

Overview

This package provides some useful common Typescript types.

Usage Overview

This section shows simple usage examples for some of the types.

OmitTyped - type-enforced version of Typescript's built-in Omit.

import { OmitTyped } from '@samhuk/type-helpers'

type A = { foo: string, bar: string, fizz: string, buzz: string }
type B = OmitTyped<A, 'fizz'> // OK
type C = OmitTyped<A, 'notAProp'> // tsc error

IsAny - determines if a type is explicitly any or not.

import { IsAny } from '@samhuk/type-helpers'

type A = IsAny<any> // true
type B = IsAny<1> // false

DictValues - converts a dictionary type to a union of it's values.

import { DictValues } from '@samhuk/type-helpers'

type A = { a: 1, b: 2, c: 3, d: 4 }
type B = DictValues<A> // 1 | 2 | 3 | 4

StringKeysOf - converts a dictionary type to a union of it's keys, ensuring that they are all strings. This is useful because keyof will tell typescript that your union will be values of string, number, or symbol, which is quite often not what you want.

import { StringKeysOf } from '@samhuk/type-helpers'

type A = { a: 1, b: 2, c: 3, d: 4 }
type B = StringKeysOf<A> // 'a' | 'b' | 'c' | 'd'

Development

Contributions are welcome! See ./contributing/development.md.


Package generated from ts-npm-package-template

About

Useful Common Typescript Types

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages