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});
}
Answer from Acevail on Stack OverflowThe 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});
}
If you are sure that serial could not be undefined you can use the ! post-fix operator
luminaireReplaceLuminaire({serial: this.selectedSerialForReplace()!, newserial: response.output});
Declaring types as String | null | undefined
Dumb noob question: Why is undefined in quotes?
String.Format returning "undefined"
How do I check for an empty/undefined/null string in JavaScript? - Stack Overflow
Videos
What do people think of the ability to have multiple types like this? I found it super annoying when mapping between objects. If the same property names are used but one is string but the other property from the other class is string | null, the IDE complains. Are there any situations where you've found it helpful to be able to declare at type like this?
There's no isEmpty() method, you have to check for the type and the length:
if (typeof test === 'string' && test.length === 0){
...
The type check is needed in order to avoid runtime errors when test is undefined or null.
Ignoring whitespace strings, you could use this to check for null, empty and undefined:
var obj = {};
(!!obj.str) // Returns false
obj.str = "";
(!!obj.str) // Returns false
obj.str = null;
(!!obj.str) // Returns false
It is concise and it works for undefined properties, although it's not the most readable.
This is a simple function which involves the use of a function to evaluate the strings. This way you can remove the part of cases' "switch". Be aware that this handles also assignments to global variables, so I recommend it only if you know anytime where is the source from(don't allow users to use this function!)
var convertType = function (value){
try {
return (new Function("return " + value + ";"))();
} catch(e) {
return value;
}
};
You can see the jsfiddle here.
How about:
var convertType = function (value){
var values = {undefined: undefined, null: null, true: true, false: false}
,isNumber = !isNaN(+(value));
return isNumber && +(value) || !(value in values) && value || values[value];
};
convertType('null'); //=> null
convertType('something'); //=> "something"
convertType('57.321'); //=> 57.321
convertType('undefined'); //=> undefined
This seems faster @ jsPerf
var convertType = function (value){
var v = Number (value);
return !isNaN(v) ? v :
value === "undefined" ? undefined
: value === "null" ? null
: value === "true" ? true
: value === "false" ? false
: value
}