-1

I am learning typescript and I see a few counter-intuitive practices when it comes to null and undefined.

const a: null[] = null;
const b: null[] = [null];
const c: null[] = undefined;
const d: null[] = [undefined];

All the above statements do not cause any errors upon compiling. Although looking at it, it seems like it should be causing type errors. I have a feeling this has to do something with my understanding of null and undefined, but can someone please explain why this does not cause any errors and runs perfectly?

I thought it has something to do with not including "strict": true in tsconfig.json, but it seems to work even after including it.

EDIT: enter image description here

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
shishir
  • 41
  • 4
  • Which version of ts are U using? all except one statements cause errors https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhgLhmArgGzQbQLowLzLpoDcMA9GTAKYBONINAUKJLAEZKoY74yZdpspCjBABrZuGgxgnIjwIowAEyoAzAJZgqy4ZVr0mLacrndcBTEtWbtyoeX10GQA – captain-yossarian from Ukraine May 05 '21 at 10:12
  • Seems to work with `strict: true` here https://www.typescriptlang.org/play?strict=true#code/MYewdgzgLgBAhgLhmArgGzQbQLowLzLpoDcAUKJLAEZKoY74yZ1rZkXQzC1EMEpgAJgFMAZgEswwwe3CdBPergKYBIiVMFtSQA - `= null` and `= undefined` are not allowed. Nor is `= [undefined]`. – VLAZ May 05 '21 at 10:13
  • That's what I figured but it doesn't seem to cause issues for me. Please refer to the image I added just now. – shishir May 05 '21 at 10:36
  • 1. What are the tsconfig options you use? 2. Are you sure the tsconfig options you have *are* used? – VLAZ May 05 '21 at 10:45

1 Answers1

0

Adding "strict": true under "compilerOptions" fixed it.

shishir
  • 41
  • 4