
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
Author : lessu
A flexable type checker.
tested under typescript 2.3.4 and 2.4.2, 2.8.1; es5 is now supported
npm install ts-check --save
mocha tests/index.js
Flexable,easy-to-extend type checker for complex object.
example
TypeChecker.checkType(sku,"SetSkuItem[]",{
SetSkuItem : {
key : "string",
stock : "number",
unit : "number",
price : "number",
cost_price : "number",
options : "ItemOptions[]"
},
ItemOptions : {
filed_id : "number",
filed_options:["number","string"]
}
});
v0.2.3
1.New feature added : now we can get failed log message by access lastError variable.
2.Fix type error synax under typescript 2.8.1
import * as TypeChecker from "ts-checker";
Basic type include
export enum BasicTypes{
number = "number",
string = "string",
object = "object",
any = "any",
null = "null",
undefined = "undefined"
}
Check basic types :
TypeCheker.checkType(undefined,"undefined");
TypeCheker.checkType(null,"any");
TypeCheker.checkType(123,"number");
//Notice a string number is also a number type in weak Number mode;
TypeCheker.checkType("123","number",{},{weakNumber:true});
any length
assert(TypeCheker.checkType([1,2,3,4,5,6],"number[]"));
check length
assert(TypeCheker.checkType([1,2,3],"number[3]"));
assert(!TypeCheker.checkType([1,2,3],"number[2]"));
array array
assert(TypeCheker.checkType([[1],[2],[3]],"number[1][3]"));
assert(TypeCheker.checkType([[1],[2],[3]],"number[][3]"));
Signature
((value:any)=>boolean);
Usage
TypeCheker.checkType( { a : "1" },(value)=>{
return typeof value["a"] == "string";
});
TypeCheker.checkType({a:"1"},{
a : "string"
});
TypeCheker.checkType({
a:"1",
b:{c:1},
c:{
d:{
e:1
}
}
},{
a : "string",
b : function(value:any){
return typeof value.c == "number";
},
c : {
d : {
e : "number"
}
}
});
The checked value can be string, number or {a:number};
TypeCheker.checkType({a:1},["string","number",{
a : "number"
}]);
//definedTypes is a {customType => check} object.
TypeCheker.checkType(value,type,definedTypes);
TypeCheker.checkType(
//to check
{
biggerThan0 : 2
},
//type
{
biggerThan0 : ">0"
},
//custom type,define >0 as a custom type , and can be used in typeChecker
{
">0" : function( value : any ){
if(typeof value == "number"){
return value > 0;
}else{
return parseInt(value) > 0
}
}
});
//custom type can be nested
TypeCheker.checkType({
biggerThan0 : [1,2,3,4]
},{
biggerThan0 : ">0[]"
},{
">0" : function(value:any){
if(typeof value == "number"){
return value > 0;
}else{
return parseInt(value) > 0
}
}
});
//or a real life example
TypeChecker.checkType(sku,"SetSkuItem[]",{
SetSkuItem : {
key : "string",
stock : "number",
unit : "number",
price : "number",
cost_price : "number",
options : "ItemOptions[]"
},
ItemOptions : {
filed_id : "number",
filed_options:["number","string"]
}
});
If a DefinedType is a function, args are supported;
Notice,Args will be convert to pure string ; every , is recognized as a splitor,
So DO NOT call like CustomType(a,"1,2,3"), It will convert to
[
"a",
"\"1",
"2",
"3\""
]
assert(!
TypeCheker.checkType({
biggerThan0 : 6
},<any>{
biggerThan0 : "range(0,5)"
},{
"range" : function(value:any,args:string[]){
if(args.length==0){
return false;
}else if(args.length == 1){
return value> parseFloat(args[0]);
}else{
return value >= parseFloat(args[0]) && value <= parseFloat(args[1]);
}
}
})
);
Custom type can be add to default defaultDefinedChecker object; so that can be used any where.
TypeCheker.defaultDefinedChecker[">0"] = function(value:any){
if(typeof value == "number"){
return value > 0;
}else{
return parseInt(value) > 0
}
};
assert(TypeCheker.checkType({a:1},{a:">0"}));
assert(!TypeCheker.checkType({a:-1},{a:">0"}));
MIT
FAQs
Type checker for typescript
The npm package ts-check receives a total of 118 weekly downloads. As such, ts-check popularity was classified as not popular.
We found that ts-check demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.