Sunday, October 5, 2025
Latest:
  • German Pronouns with Nominative, Accusative, and Dative cases
  • German articles for the nominative, accusative, and dative cases
  • Der kleine Vogel und die Sonne
  • German alphabet pronunciation
  • Command design pattern
Upokary

Upokary

Quality information

  • Health
  • Life
  • Fashion
  • Food
  • Code
  • English
  • বাংলা
Technology 

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

Published: December 1, 2021 JavaScript, TypeScript

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; 
  }   
}

You May Also Like

What are some good Node.js frameworks?

September 17, 2019 beroza

How to raise exception for http errors in python?

September 2, 2019 beroza

Prefer dependency injection to hardwiring resources

January 22, 2020 beroza

How to get JavaScript fetch http response status?

March 2, 2020 beroza

Uncaught Error: Invariant Violation: Element type is invalid: expected a string or a class/function but got: object

December 16, 2021 beroza

How to use WordPress REST API?

July 5, 2019 beroza

TypeError: #Object is not a function

March 25, 2019 beroza

How to check if an array contains a value in JavaScript?

August 6, 2021 beroza

How to render and return html using render_to_string in rails?

May 27, 2020 beroza

Why Upokary?

Upokary.com is a useful application in everyday life. It provides very handy information regarding every aspect of life. Each and every tip of upokary.com is meant to make life better.

Upokary tools

  • Youtube thumbnail
  • Youtube video downloader

Useful Links

  • Handy code
  • Technology
  • People
  • Programming
  • Quote
  • Travel
  • WordPress
  • IELTS
  • Kids

Pages

  • Sitemap
  • Privacy policy
  • About us
  • Contact us

Support

For any kind of query or question please drop a message in the following email: upokary@gmail.com
Copyright © 2025 Upokary. All rights reserved.