Package Exports
- narrowing
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (narrowing) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
🤖 Narrowing
TypeScript tiny narrowing helpers you better use.
Install
npm i narrowing # yarn add narrowingUsage
let a: unknown;
if (isXXX(a)) {
// TypeScritp know your type here!
}Functions
- isString
- isNumber
- isBigInt
- isBoolean
- isSymbol
- isUndefined
- isNull
- isNil
- isFunction
- isInstance
- isArray
- has
- kind
import {
isString,
isNumber,
isBigInt,
isBoolean,
isSymbol,
isUndefined,
isNull,
isFunction,
isInstance,
isArray,
isNil,
has,
kind
} from 'narrowing';
let a: unknown;
if (isString(a)) a.toLocaleLowerCase();
if (isNumber(a)) a.toFixed();
if (isBigInt(a)) a.toString();
if (isBoolean(a)) a.valueOf();
if (isSymbol(a)) a.description;
if (isUndefined(a)) {
a; // undefined
}
if (isNull(a)) {
a; // null
}
if (isNil(a)) {
a; // null | undefined
}
function test(a: string, b: number): boolean {
return true;
}
if (isFunction<typeof test>(a)) {
a('11', 1);
}
if (isInstance(a, Date)) {
a.getFullYear();
}
isInstance(a, DelayNode);
class TestClass {
m() {}
}
if (isInstance(a, TestClass)) {
a.m();
}
if (isArray<string>(a)) {
a[0].trim();
}
let b: TestClass | undefined | null;
// b.m(); // Error: Object is possibly 'null' or 'undefined'.ts(2533)
if (!isNil(b)) {
b.m(); // no Error any more
}Type predicates: has()
type Bird = { fly: () => {} };
type Cat = { run: () => {}; meow: () => {} };
type Dog = { run: () => {} };
let pet = {} as any;
// type predicates
const isBird = has<Bird>('fly');
const isDogOrCat = has<Dog | Cat>('run');
const isCat = has<Cat>('run', 'meow');
if (isBird(pet)) {
pet.fly(); // Bird
}
if (isDogOrCat(pet)) {
pet.run(); // Dog | Cat
}
if (isCat(pet)) {
pet.meow(); // Cat
}Discriminated unions: kind()
interface Square {
kind: 'square';
size: number;
}
interface Rectangle {
kind: 'rectangle';
width: number;
height: number;
}
interface Circle {
kind: 'circle';
radius: number;
}
const isSquare = kind<Square>('square');
const isRectangle = kind<Rectangle>('circle');
const isCircle = kind<Circle>('circle');
let s = {} as any;
if (isSquare(s)) {
console.log(s.size);
}
if (isRectangle(s)) {
console.log(s.height);
}
if (isCircle(s)) {
console.log(s.radius);
}Version
- 1.1.0
- add
has()
- add
- 1.1.1
has()accept multiple params
- 1.2.0
- add
kind()
- add