Skip to content

HyphnKnight/ts-is

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-is

Utility for typescript typechecking.

What is it?

Exports a single func is which has the following type notation:

declare const is: <type>(func?: (x: any) => boolean) => (unknown: any) => unknown is type;

I use it often to create typeChecking functions for my typescript projects. For example if I have a vector class.

type Vector {
  x: number;
  y: number;
}

export const isVector = is<Vector>(u => typeof u.x === 'number' && typeof u.y === 'number');

const three = 3;
isVector(three) // returns false;

const aVector = {x:0,y:3};
if(isVector(aVector)){
  // compiler knows aVector is a vector here
}

How do I use it?

Start by installing it in your project.

npm install --save ts-is

then any of the following should work

const is = require('ts-is');
const { is } = require('ts-is');
import is from 'ts-is';
import { is } from 'ts-is';

Good luck and I hope this is usefull for you! (:

About

Utility TypeChecker function

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published