Type ‘null’ is not assignable to type ‘T’

We got into this issue when we tried to return null from a generic function. See the code segment below:

function bar(x: T): T {
  if(x === null){
     return null; 
  }   
}

Solution:

To solve this issue, we can amend the code like below:

function bar(x: T): T | null{
  if(x === null){
     return null; 
  }   
}

OR

function bar(x: T): T {
  if(x === null){
     return null as any; 
  }   
}