@detachhead/ts-helpers v8.1.2-ed4eeb021944556e05bec24ff3e8da42032a01ca
ts-helpers
various typescript helper functions and utility types
features
value tracking
this library includes many helper types and functions such as add, subtract, and substring which allow the values
to be known at compile time

noUncheckedIndexedAccess support
the noUncheckedIndexedAccess compiler flag is epic, but there's room for improvement. for example, the following
issue:
if (foo.length > 2) {
const bar: string = foo[1] //error: string | undefined not assignable to string
}can be solved with lengthGreaterThan
import { lengthGreaterThan } from '@detachhead/ts-helpers'
if (lengthGreaterThan(foo, 2)) {
const bar: string = foo[1] //no error, foo is casted to [string, string]
}there's also lengthLessThan, lengthGreaterOrEqual, etc.
most of the array functions in this library keep track of the length, mostly thanks
to this TupleOf utility type
date formatter type
this library contains a helper type and function for formatting dates using
the date-fns format.
const date = formatDate(new Date(), 'dd-MM-yyyy')
assert(date === '01/01/2021') //compile error, wrong date formatyou can use any date format that date-fns accepts, and the FormatDate utility type will generate a template literal
type to match your desired date format.
Type Testing
With the exactly function you can test if types or values are an exact match to a type
const a: 1 | 2 = 1
//values (also does a runtime assertion on the values)
exactly(1 as number, a) // error as `1 | 2` is not an exact match of `number`
exactly(1 as number, a as number) // no error
exactly(1 as 1 | 2, a) // no error
// mixed
exactly<number>()(a) // error as `1 | 2` is not an exact match of `number`
exactly<number>()(a as number) // no error
exactly<1 | 2>()(a) // no error
// types
type Foo = 1 | 2
exactly<1, Foo>() // error as `1 | 2` is not an exact match of `1`
exactly<1 | 2, Foo>() // no errorThe Equals type allows you to check if two types are equal at the type level
Equals<number, 1 | 2> // false
Equals<any, 10> // false
Equals<unknown, never> // false2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago