Skip to content

tranvansang/jtruthy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jtruthy: typescript-typed Boolean() function

Install

Install via npm package:

yarn add jtruthy

Usage

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)[]

Deprecated note

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published