Typescript assign null to a type don't trigger error

Viewed 4153

Can anyone explain me, please, why this code dont trigger an error?

import { Injectable } from '@angular/core';

interface Animal{
  name: string;
}

@Injectable()
export class AnimalService {
  lion: Animal = null;
  constructor() {}
  get(){
   return this.lion;
 }
}
2 Answers

use variable: Type | null for specific cases where nulls can be allowed.

ex: name:string| null

I personally turn on strictNullChecks and use the specific cases.

Related