@flatverse/typed
v0.9.1
Published
Some type guards and other runtime type checking tools for TypeScript.
Downloads
26
Readme
Typed
Some Extra Guards and Types for TypeScript
Gist, Vibes, Philosophy, Misc.
- Lightweight / Minimal / KISS
- Simplify basic type checking during runtime with guards.
- Add a few types and utility classes that make generic typing a bit easier.
- No
nulls, ever, just undefined.- Nothing against
null, it just causes a lot of problems to have bothnulland undefined. - Most guards in this package will throw an error if a
nullis encountered. undefineds are OK!
- Nothing against
- favor shorter variable names when the abbreviations/shorthand names are widely recognized (i.e.
isStroverisString).- favor more verbose names when no widely recognized alternative exists
isObjWithOptionalProp
Examples
TODO: more examples
import { isDef } from "typed/guards";
function someUselessFunc(idk?:string|object = undefined):string|object|undefined {
return idk;
}
let anUndefinedVar = someUselessFunc(undefined);
if (!isDef(anUndefinedVar)) {
console.log("anUndefinedVar is undefined");
}
let aNullVar = someUselessFunc(null);
!isDef(anUndefinedVar); // throws an error because we don't like null's here
let aString = someUselessFunc("a real string");
if (!isDef(aString))
{
throw new Error("its not defined.")
}
let anObjectOrString = aString; // the compiler now knows that anObjectOrString is of type object or string because we used the isDef guard.
let definitelyAString = isStr()
