How to assign string | undefined to string in TypeScript?

The typescript compiler performs strict null checks, which means you can’t pass a string | undefined variable into a method that expects a string.

To fix this you have to perform an explicit check for undefined before calling luminaireReplaceLuminaire().

In your example:

private selectedSerialForReplace(): string | undefined {
    return this.selectedSerials.pop();
}

luminaireReplaceLuminaire(params: {  "serial": string; "newserial": string; }, options?: any): FetchArgs {
    ............
}

const serial = this.selectedSerialForReplace();
if(serial !== undefined) {
    luminaireReplaceLuminaire({serial, newserial: response.output});
}

Leave a Comment

tech