1

I'm getting the error when transpiling Typescript with npm run build:

Type 'number | null' is not assignable to type 'number'.
  Type 'null' is not assignable to type 'number'.

Index.ts

public init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container:HTMLDivElement)
    {
        // the increment value defaults to 1 if no increment input is detected, or it is zero.
        this._incrementValue = 1;
        if(context.parameters.incrementValue != null){
            if(context.parameters.incrementValue.raw != 0){
                this._incrementValue = context.parameters.incrementValue.raw;
            }
        }

On this part:

this._incrementValue

Is there a way to avoid the compiler to complaint about dealing with nulls?

unstuck
  • 563
  • 2
  • 12
  • 29
  • does using !== help? see [link](http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html) or here [link](https://stackoverflow.com/a/523650/4083037) – Hannes F Nov 25 '22 at 14:25

2 Answers2

1

This looks like you need to also check context.parameters.incrementValue.raw !== null before assigning it to this._incrementValue;

dermeck
  • 131
  • 5
1

Seems this statement

this._incrementValue = context.parameters.incrementValue.raw;

assignes the union type(number | null) to number type. Changing the type of the property _incrementValue to the same union type should calm the TS compiler. "Casting" context.parameters.incrementValue.raw as number might also do the trick. Good luck!

Angel Dinev
  • 399
  • 4
  • 13