Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Handle a Possibly Null Variable … That I Know is Not Null?

If I am using (strict null checks) TypeScript, and I have some code with a possibly null variable:

let x = null;
if (someCondition) {
  x = { a: 1 };
}

doSomethingWith(x.a);

I get an error, as expected:

‘x’ is possibly ‘null’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can fix that error with a null check:

if (!x) return;
doSomethingWith(x.a);

but then I get:

Property ‘a’ does not exist on type ‘never’.

And really, instead of an if/return, what I’d like to write is an assert call:

const assert = value => { if (!value) throw Error(); }

assert(x);
doSomethingWith(x.a);

… but that doesn’t even get rid of the first error.

I figured out that I could fix the second error with:

(rootGroup as {id: number}).id

but I’m not sure if all that is the simplest solution, and it still doesn’t solve the problem with using an assert function.

My question is, how can I …?

  1. create a "maybe null" variable
  2. run a function that proves it’s not null (by throwing an error if it is)
  3. add (the least amount possible of) TypeScript code, and then
  4. use that variable, without any TS errors

>Solution :

You can use assertion signatures like this:

let x: { a: 1 } | null = null

if (someCondition) {
  x = { a: 1 }
}

// @ts-expect-error x maybe null
console.log(x.a)

assert(x) // asserting that x is truthy

console.log(x.a) // no error here

function assert(value: unknown): asserts value {
  if (!value) {
    throw new Error('foo')
  }
}

If you just want to throw on nullish values, you can do something like this:

function assertNotNull<T>(value: T): asserts value is NonNullable<T> {
  if (value == null) {
    throw new Error('bar')
  }
}

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading