7

util.isNullOrUndefined(object) has been depreciated and I cannot find any alternative for it. Can someone point out a feasible alternative for it?

if (isNullOrUndefined(this.activeAppKey) || this.activeAppKey.trim().length === 0) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}
  • `if (!this.activeAppKey || this.activeAppKey.trim().length === 0) { this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY); }` – vegemite4me Sep 18 '20 at 09:03

8 Answers8

7

import it from 'is-what'

so,

import { isUndefined, isNullOrUndefined } from 'util';

will become,

import { isUndefined, isNullOrUndefined } from 'is-what';

Thomas Mulder
  • 740
  • 10
  • 26
Manoj TM
  • 110
  • 1
  • 4
4

try using ! operator.

if (!this.activeAppKey) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}
Ramesh
  • 1,041
  • 15
  • 39
  • 3
    Using `!` operator can cause some troubles, when variable is number 0, empty string etc. See this answer: https://stackoverflow.com/questions/28975896/is-there-a-dedicated-function-to-check-null-and-undefined-in-typescript – Marcin Piotrowski Feb 11 '19 at 09:47
  • it produces errors for codes like ` if (!isNullOrUndefined(user.customRoles[this.activeAppKey])) { ui.role = user.customRoles[this.activeAppKey].roleKey; }` –  Feb 12 '19 at 05:57
  • Have you checked user itself is null? – Ramesh Feb 13 '19 at 06:28
  • 1
    Might be a good idea to explain [Javascript truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). – vegemite4me Sep 18 '20 at 09:02
  • you can't use `!` operator. because this will not allow you to enter in if condition body for this.activeAppKey value is `false` instead if `null` or `undefined`. – Karan Raiyani Sep 08 '21 at 12:49
  • Simply using `this.activeAppKey != null` would be enough. It's simple to type and read, and covers both `null` and `undefined`. – vgru Jan 27 '23 at 11:36
4

Why not simply create you own:

export function isNullOrUndefined(value: any) {
    return value === null || value === undefined;
}

Lets say you create a tools.ts file and write the above code:

You can use it in your component as follows :

import { isNullOrUndefined } from 'tools';

The above code will work no changes there. Plus it will reusable

Jatin Rane
  • 41
  • 2
1

You can use the isNullOrUndefined function from the core-util-is npm package.

It was deprecated from nodejs core because behaviour or api changes on these functions could break a lot of existing code without notifying the users. In a npm package, using semver, only a major version bump can break api/behaviour. A lot safer this way.

Romain Durand
  • 783
  • 1
  • 6
  • 22
1

Using a double equals on a null checks for null and undefined exclusively

if (this.activeAppKey == null || this.activeAppKey.trim().length === 0)

null == undefined // true
null == null      // true
null == 0         // false
null == ""        // false
null == false     // false
0

try this:

if (this.activeAppKey === null || this.activeAppKey === undefined || this.activeAppKey.trim().length === 0) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}
super IT guy
  • 195
  • 2
  • 12
0

I would go for this approach. Create some util class and add this as a static method.

export class Util {
  static isNullOrUndefined<T>(obj: T | null | undefined): obj is null | undefined {
    return typeof obj === 'undefined' || obj === null;
  }
}

b4hkid
  • 96
  • 5
0

From NodeJs reference [https://nodejs.org/api/util.html#utilisnullorundefinedobject] :

Deprecated: Use value === undefined || value === null instead.

This makes the answer given by super IT guy above correct (https://stackoverflow.com/a/56138823/6757814)

Rodjf
  • 177
  • 1
  • 12