Various helpful type definitions and type assertion helpers. Includes many tricky recursive and functional types.
npm install @ts-std/types@ts-std/types> Various helpful type definitions and type assertion helpers. Includes many tricky recursive and functional types.
#### function tuple
Let's you quickly create a tuple without requiring a type assertion.
``ts
// the old way
const yuck = [1, 'a', true] as [number, string, boolean]
// the better way
// especially nice if you need to create many tuples in a file
import { tuple as t } from '@ts-std/types'
const yay = t(1, 'a', true)
`
#### type Unshift
Very useful for prepending items to tuples.
`ts
const a: Unshift
function join
return [item, ...list] as Unshift
}
`
#### type Head
Gets the first element from a tuple type.
`ts`
assert_is_type>(true)
assert_is_type>(true)
#### type Tail
Gets all but the Head from a tuple type.
`ts`
assert_is_type
assert_is_type
assert_is_type
#### type HasTail
Checks whether the tuple type has a tail.
`ts`
assert_boolean_type
assert_boolean_type
assert_boolean_type
#### type Last
Gets the final item from a tuple type.
`ts`
assert_is_type
assert_is_type
assert_is_never
#### type OnlyOne
Checks whether a tuple type has only one element.
`ts`
assert_boolean_type
assert_boolean_type
assert_boolean_type
These are useful for testing if your library functions return the correct types or not. Typically used in a testing suite to ensure that unexpected type returns break the build.
#### type IsType
Checks that two types are exactly the same.
#### type IsNever
Checks that a type is never.
#### function assert_boolean_type
Checks a boolean conditional type.
`ts`
// type IsString
assert_boolean_type
assert_boolean_type
#### function assert_is_type
Checks that two types are exactly the same.
`ts`
assert_is_type
assert_is_type
#### function assert_is_never
Checks that a type is never.
`ts`
assert_is_never>(true)
assert_is_never>(false)
#### function assert_type
Checks that the value is of type.
`ts`
assert_type
#### function assert_value_types
Checks that two values have the same type.
`ts
assert_value_types(
[1, 'a', true] as [number, string, boolean],
t(1, 'a', true),
true,
)
assert_value_types(
[1, 'a', true],
t(1, 'a', true),
false,
)
`
#### type KeysOfType
Gets the keys from T that are of U.
`ts`
type Data = { a: number, b: string, c: boolean }
type A = KeysOfType // -> 'a'
type B = KeysOfType // -> 'b'
type Both = KeysOfType // -> 'a' | 'b'
#### type PickOfType
Creates a new type from T with only the keys that are of U.
`ts`
type Data = { a: number, b: string, c: boolean }
type A = PickOfType // -> { a: number }
type B = PickOfType // -> { b: string }
type Both = PickOfType // -> { a: number, b: string }
#### type SingleParameter
Pulls the single input type from a function.
`ts`
assert_is_type
assert_is_type
#### type FoldingFunctions
Requires that a tuple of functions have inputs and outputs that "chain" from one to the next, and gives the final output type.
Useful for situations where functions will be called in order with the output of the previous.
`ts
assert_is_type
assert_is_type
assert_is_type
assert_is_never
assert_is_never
assert_is_never
``