TS2531: Object is possibly null

Hi guys,

I have an error on the following line of code that says “TS2531 Object is possibly null”

this._shadowEmitter.getShadowMap().refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;

Is there a way I can fix this, or perhaps structure this differently to clear the error?

Thanks in advance

You can do:

this._shadowEmitter.getShadowMap()!.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;

if you know this._shadowEmitter.getShadowMap() won’t never return null.

1 Like

What Evgeni said, but if you don’t know then you need a guard clause:

if (this._shadowEmitter.getShadowMap() !== null) {
  this._shadowEmitter.getShadowMap()!.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
   ...
}

the easiest in TS to prevent a double call to the function:

const shadowMap = this._shadowEmitter.getShadowMap();
if (shadowMap) {
  shadowMap.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
}
1 Like

Thanks again