0

We're learning TypeScript and while declaring a variable we tried to specify its type too like this

let a: number = 5;
let b: boolean;
let c: string;
let d: any;

let e: number[] = [1, 2, 3, 4, 5];
let f: any[] = [true, 'foo', 4.09, []];

let g: null;

These any and null were new to us, we were able to understand the concept of any, but it's confusing with null. This is the first time we are hearing about null as a data type, we've heard null as a value in Java. The really confusing thing is why someone would need a data type as null.

In Java, we declare variables as

int a = 0;
double b = 0.0;
char c = 'c';
String x = "abc";

But if null is a data type, it will like this

null a;

We're finding it difficult to understand this concept! If null is assigned to a variable its cool, nothing is there that's it, but what is the purpose of having null as a data type? So we looked at some websites

Data types in TypeScript - here its says It is used when an object does not have any value, but does it mean assign null if an object doesn't have a value or specify null as a type to those objects which don't have any value?

Here in How to declare a type as nullable in TypeScript? and TypeScript: Define type that can be bool or null, the discussion was about assigning/returning null to some variable.

So can anybody help us with clearing this concept of null being used as a data type?

1 Answer 1

3

In JavaScript and in TypeScript, null is a perfectly good value. You're right that it represents nothingness, more or less, but it is good to have a value that stands for the absence of something. Consider:

let supervisor = "None"    // I have a supervisor named "None"
let supervisor = null      // I DON'T HAVE a supervisor
let supervisor = undefined // Maybe I have a supervisor, maybe
                           // I don't, I don't know or care, or
                           // none of your business

Now you asked about null as a type. Great! It is easy to get confused between the value null and the type null. But here's how you can understand it. TypeScript has unit types, which are types that have only a single value. Did you know that 5 is a type? (It is the type whose only value is the number 5. Did you know that "dog" is a type? (The type whose only value is "dog". Yep, you can say:

let x: "dog" = "dog";
let y: 5 = 5;

Now since null is a value, there exists a type called null also. It is the type whose only value is null. It's a unit type, just like the two above.

let z: null = null;

Now if your question was "why would I ever declare such a type in a real application?" that's a good question too. Perhaps you might not explicitly declare a type like that, but you very well might want a type like:

number | null

This could be the result type for a function that converts an string to a number. Note the union type T1 | T2, where T1 and T2 are types. If there was no such type as null, then number | null would not be the union of two types, and things would be weird. Things are so much more clean when you have unit types! And since null is a value, then there needs to be a type null containing just the value null.

3
  • Wow, that's really interesting its entirely a different view, let x: "dog" = "do"; gave error and will only compile when written like let x: "dog" = "dog";. Cool and thanks, it says we got a long way to go :):) Sep 21, 2018 at 6:33
  • 1
    For more information on unit types (and all the other types) I recommend this section of Anders Hejlsberg's talk at Build 2018 I think it will help you a lot. Cheers.
    – Ray Toal
    Sep 21, 2018 at 6:41
  • @ArunSudhakaran what is also good is having stuff like function favorite(pet:"cat"|"dog") {} so you could only enter "cat" or "dog" but could not enter "fish".
    – WORMSS
    Dec 7, 2018 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.