I have the following express route :
app.get('/:id', function (req, res) {
var idNum: number = Number(req.params.id);
var idCast: number = +req.params.id;
var id: number = req.params.id;
console.log('typeof idNum ', typeof idNum , ' ', 'idNum== 0 : ', idNum== 0 , ' ', 'idNum=== 0 : ', idNum=== 0);
console.log('typeof idCast', typeof idCast, ' ', 'idCast == 0 :', idCast == 0, ' ', 'idCast === 0 :', idCast === 0);
console.log('typeof id ', typeof id , ' ', 'id == 0 : ', id == 0 , ' ', 'id === 0 :' , id === 0);
res.json({});
});
this returns :
typeof idNum number idNum== 0 : true idNum=== 0 : true
typeof idCast number idCast == 0 : true idCast === 0 : true
typeof id string id == 0 : true id === 0 : false
I understand that typescript provide only compile-time type-checking, and I guess it means it's not aware that req.params provide arguments as strings.
Is there any way I can automatically cast my params to Number ? or at least raise an error of I didn't done it manually ? Else, it seems that tympescript, unless used in a full-typescript environnement is useless.
Endly, is there any "big" open source project using TypeScript with ExpressJS that I could read the source from ?