npm.io
1.0.2 • Published 19h ago

@isbl/ts-utils

Licence
MIT
Version
1.0.2
Deps
0
Size
3 kB
Vulns
0
Weekly
0

@isbl/ts-utils

MIT License npm dependencies Releases

Few utils for working in typescript which I found myself writing over and over again.

notNull

Equivalent to a => a !== null, but also works as type assertion. Usage example:

function removeTwos(input: number[]) {
  return input.map(item => item === 2 ? null : item).filter(notNull)
}

Return type of this function is number[], not (number | null)[] as it would be otherwise.

ArrayElement

Generic type which given array type returns its element. More readable alternative to ArrayType[0], except that it also checks if incoming type is an array. Works for readonly arrays.

type Thing = ArrayElement<string[]> // thing is string