I’m currently handling a simple Typescript API with express who handle simple tasks.
For one case, I need to send a property which is of type:
export Toto = {
...
rate: number | null
...
};
But the rate
property may also be NaN
. In fact, this property is a mere number with 2 special value: null
and NaN
.
The issue is even if I explicitly send NaN
at the very end, my front end will receive null
. After some investigation I discovered JSON.stringify()
was replacing all my NaN
entries by null
, but I need both of them.
I do know a simple:
JSON.stringify(obj, (k, v) => Number.isNaN(v) ? "NaN" : v)
can solve this problem, but how to adapt it to express.js ?
I’m answering using:
res.status(200).json(obj);