#isNil not supporting type guards for undefined?

12 messages · Page 1 of 1 (latest)

remote shale
#

hi, I have a question about 'isNil' function inside commons/utils/shared.utils.ts

Expected behavior

Its type guard must contain checks for both undefined and null values.

Current status:

The source code contains guards for both,
but the compiled d.ts file contains only the null type guard.

Proof:

https://github.com/nestjs/nest/blob/v10.2.10/packages/common/utils/shared.utils.ts

When I see this code, it contains

export const isNil = (val: any): val is null | undefined =>
  isUndefined(val) || val === null;

However, if I see the compiled version of it, it somehow weirdly erases the 'undefined' part of the type guard.

export declare const isNil: (val: any) => val is null;

You can check this out here https://www.npmjs.com/package/@nestjs/common/v/10.2.10?activeTab=code
and go to common/utils/shared.utils.d.ts

Why is this happening?

old willow
#

btw such helpers are intended to be used internally, they are not part of the public API
So don't use it

steel remnant
#

there is a simple vanilla js work around you can use

const someVar = null
// works with: null undefined and empty strings ""
if(!someVar) {
// error
}
old willow
magic sapphireBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.

steel remnant
old willow
steel remnant
old willow
steel remnant
old willow
#

man, the bugs I've seen just because of ! on falsy values.. but yeah