1

I check this.props.data.loading, and if it's done (and no error) then I'd like to directly use this.props.data. However, TS tells me that it may be null b/c of the way Apollo types are implemented.

What's the best way to avoid needing to write something like this every time?

(this.props.data as any).myData.foobar

For example, I can throw if it's null (which it should never be). But this is an annoying solution because if I have many places that render differently based on whether data is loaded, then this code would be all over the place.

if (!this.props.data) { throw new Error("Data is null!"); }

Fairly new to Apollo and I think this is a fairly basic question.

Freewalker
  • 6,329
  • 4
  • 51
  • 70

1 Answers1

1

The non-null assertion operator ! looks like it will do exactly what I want.

this.props.data!.myData.foobar

Documented in TypeScript 2.0.

(Thanks to this and this SO answers.)

Freewalker
  • 6,329
  • 4
  • 51
  • 70