0

I am wondering if there is an option that allows null to be used like a 0 in typescripts compiler options.

I know I can cast null to a number by doing Number(null), but I would prefer just being able to use variables that can be null without doing that.

A similar solution to Number(null) (null??0) (answered here: Allow nullable operands in less- or greater-than comparisons in TypeScript) is a code work around; however, I am looking for a typescript compiler option that allows for this.

example of what I am looking to be able to do. (without getting a type error)

const x = null

console.log(x>0)
console.log(x<0)
console.log(x<=0)
console.log(x>=0)

Javascript has no problem treating null as 0.

(turning strict mode off allows for this, but I want to be able to use strict mode and JUST have this turned off)

Daniel
  • 1,392
  • 1
  • 5
  • 16
  • 3
    Why are you looking to do this? What problem are you trying to solve? – Obsidian Age Mar 25 '22 at 03:27
  • 1
    Permitting code that looks like that would be quite confusing, at least to me... – CertainPerformance Mar 25 '22 at 03:28
  • The database I am using has a lot of null/number values used for linking together tables (its a sql database) and I would like the types to be correct, however while converting over to typescript there are a lot of tableId > 0 and tableId > 1 type if statements to see if they have extra information in another table that can be loaded. And because of latter I cant just do a nullish comparison. – Daniel Mar 25 '22 at 03:30
  • Does this answer your question? [Allow nullable operands in less- or greater-than comparisons in TypeScript](https://stackoverflow.com/questions/59472015/allow-nullable-operands-in-less-or-greater-than-comparisons-in-typescript) – Robby Cornelissen Mar 25 '22 at 03:34
  • @CertainPerformance I don't think it is any more confusing than `if (Number(tableId) > 1)` – Daniel Mar 25 '22 at 03:36
  • @RobbyCornelissen I am more looking for a typescript setting that I can turn off, not a code workaround. – Daniel Mar 25 '22 at 03:38
  • What is the result you expect from every evaluation? is null bigger, smaller or equal to zero? – HuLu ViCa Mar 25 '22 at 03:40
  • You can turn off `strictNullChecks`, but that has a broader impact than just this number comparison case. – Robby Cornelissen Mar 25 '22 at 03:41
  • @HuLuViCa null is defined to be shallowly equal to zero in javascript – Daniel Mar 25 '22 at 03:41
  • @RobbyCornelissen Yeah, strict: false lets me do the number comparisons and still have strictNullChecks partially working (like warnings on undefined arrays), which made me think it was a more modular option than that. – Daniel Mar 25 '22 at 03:45
  • I didn't mean turning off `strict` mode entirely, just `strictNullChecks`. – Robby Cornelissen Mar 25 '22 at 03:47
  • @RobbyCornelissen I know, I was implying that that broader impact you mentioned was a little bit too much because of the potential undefined problems. Sorry I can see how what I said was confusing. – Daniel Mar 25 '22 at 03:51
  • There is a linked answer in one of the previous comments. and that has a mention of [Non-Null Assertion Operator (!)](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-). What do you think about using it? With that, you should be able to do `console.log(x! > 0);`... However, I'm sure you already would have noticed `js` behavior with `null` and `relational operators`, `x! > 0` and `x! < 0`, both returns `false`, which could have catastrophic implications on the correctness of your programs. – Nalin Ranjan Mar 25 '22 at 04:50

1 Answers1

1

You could set strictNullChecks into your tsconfig.json, but you have to annotate the type

const x: number = null

But please, pay attention, x is and will be null for JS.

Nullndr
  • 1,624
  • 1
  • 6
  • 24