Install via npm package:
yarn add jtruthy
import truthy from 'jtruthy'
// or
const truthy = require('jtruthy')
const arr = [1, 2, false && 3] as const
const a = arr.filter(truthy) // type: (1 | 2)[]
const b = arr.filter(Boolean) // type: (false | 1 | 2)[]
This package was originally made to make Array.prototype.filter()
's return type better.
Recently, I found that we can do it better by just including the following type def in any ".d.ts" file in your project.
type Truthy<T> = T extends false | '' | 0 | 0n | null | undefined ? never : T
interface Array<T> {
filter(predicate: BooleanConstructor, thisArg?: any): Truthy<T>[]
}
interface ReadonlyArray<T> {
filter(predicate: BooleanConstructor, thisArg?: any): Truthy<T>[]
}
Referred from ts-reset does it better.