Home > Mobile >  Type 'number | null' is not assignable to type 'number'. Type 'null' i
Type 'number | null' is not assignable to type 'number'. Type 'null' i

Time:11-26

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?

CodePudding user response:

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

CodePudding user response:

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!

  • Related