I have a simple check where I want to check if the given variable is >=0.
public print(value: any): void {
if(value >= 0) {
console.log('Greater than zero')
}
}
The catch here is when the ining variable has value null, then it will bee truthy and log the statement. Is there a clean way to avoid it, but not adding extra checks?
I have a simple check where I want to check if the given variable is >=0.
public print(value: any): void {
if(value >= 0) {
console.log('Greater than zero')
}
}
The catch here is when the ining variable has value null, then it will bee truthy and log the statement. Is there a clean way to avoid it, but not adding extra checks?
Share Improve this question asked Jun 20, 2020 at 19:33 Madhur MauryaMadhur Maurya 1,0705 gold badges20 silver badges45 bronze badges 10-
9
What's wrong with an extra check?
if(value !== null && value >= 0) {
– ibrahim mahrir Commented Jun 20, 2020 at 19:36 -
2
Number(null)
evaluates to0
so any slick tricks won't apply – Józef Podlecki Commented Jun 20, 2020 at 19:39 -
1
There is
if(value?.valueOf() >= 0)
if you want your coworkers to throw stationery in your direction. – adiga Commented Jun 20, 2020 at 19:42 -
2
How about
if (typeof value === 'number' && value >= 0)
for the check? – Alberto Rivera Commented Jun 20, 2020 at 19:44 -
1
just use
parseInt
check if not NaN and don't bother – Józef Podlecki Commented Jun 20, 2020 at 19:52
4 Answers
Reset to default 2You can employ a type guard that will assure the piler that you're not handling a null
but a number. Moreover, it will make the code more correct, since with value: any
this means you might get a boolean or a string passed in:
public print(value: any): void {
if (typeof value === "number") {
//value is definitely a number and not null
if (value >= 0) {
console.log('Greater than zero')
}
}
}
Playground Link
Now the code specifically verifies that you do get a number and then checks if it's more than or equal to zero. This means that a null
or a non-number value would not be processed.
The type guard condition can be bined with the other for brevity:
public print(value: any): void {
if (typeof value === "number" && value >= 0) {
console.log('Greater than zero')
}
}
Playground Link
Or extracted on its own to just reduce the nesting:
public print(value: any): void {
if (typeof value !== "number")
return;
//value is definitely a number and not null
if (value >= 0) {
console.log('Greater than zero')
}
}
Playground Link
I don't see why you don't want to add a null-check.
An alternative is to use number
instead of any
but it will work only if your ts.conf
enables strict null checks.
function print(value: number): void {
if(value >= 0) {
console.log('Greater than zero')
}
}
print(null) // won't pile with strict null checks
If you codebase does not allow the use of null
, just use undefined
and use an implicit conversion, like so:
public print(value: any): void {
if(value != undefined && value >= 0) {
console.log('Greater than zero')
}
}
This works because null == undefined
(the double equals creates a type conversion, while the triple equals does not).
In JavaScript, I usually use the following:
`${value}` >= 0
// or
parseInt(value) >= 0
in TypeScript you can most likely use:
public print(value: any): void {
if (+`${value}` >= 0) {
console.log('Not less than zero')
}
}