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 Overflow
🌐
Qiita
qiita.com › typescript
TypeScript - How to Cast null to string? #TypeScript - Qiita
April 4, 2022 - Sometimes you will get the return value type string | null from function, then reassign the return value to a string type value. And the ...
Discussions

Converting nullable object values to strings in Typescript
Warning: `value` prop on `input` should not be null. Consider using an empty string to clear the component or `undefined` for uncontrolled components. Any help or suggestions are appreciated! This is using Typescript version 3.1.6 More on stackoverflow.com
🌐 stackoverflow.com
TypeScript Type 'string | null' is not assignable to type 'string'
In this fragment I Have the next error : Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'. TS2322 export async function GetCertainCoinByType( More on stackoverflow.com
🌐 stackoverflow.com
Maybe<string> vs string | null

I think it's mostly a matter of preference. Folks coming from Scala/Haskell invariable reach for an Option/Maybe, nearly everyone else uses the if/else.

Scala and Haskell have distinct syntax to make dealing with Maybe a little more concise, TypeScript doesn't at least in part because in most cases Maybe is redundant with strict null checks.

IMO, it's more idiomatic TypeScript to rely on type guards with strict null checks.

More on reddit.com
🌐 r/typescript
51
33
August 28, 2020
typescript - Convert variable from number to string only if not null, otherwise leave as null - Stack Overflow
0 Convert string to number in typescript without losing leading zero without using an array solution · 0 How to convert a number type value to empty string? 1 Convert String to Number. Must always be NaN if conversion fails · 2 Type 'string | number | null | undefined' is not assignable to ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-type-null-is-not-assignable-to-type-string
Type 'string or null' is not assignable to type string (TS) | bobbyhadz
When you use this approach, you basically tell TypeScript that this value will never be null or undefined. This is very similar to a type assertion and should only be used when you're absolutely sure that the value is of the expected type. ... Copied!interface Employee { id: number; name: string | null; } const emp: Employee = { id: 1, name: 'Bobby Hadz', }; const name: string = emp.name as string; // 👈️ type assertion console.log(name); // 👉️ "Bobby Hadz"
🌐
C# Corner
c-sharpcorner.com › UploadFile › c63ec5 › string-in-typescript
String in TypeScript
October 9, 2019 - Here I am defining two ways to create an empty string. The code is given below: ... A null string references the one and only value of the Null type. It is not possible to directly reference the Null type itself.
🌐
Stack Overflow
stackoverflow.com › questions › 53505041 › converting-nullable-object-values-to-strings-in-typescript
Converting nullable object values to strings in Typescript
It should check each key in object, and if it is a falsy one (null || undefined) assign it an empty string. ... Sign up to request clarification or add additional context in comments. ... Sorry the i variable was a typo. Meant to use value. Updated the original question.
Find elsewhere
🌐
Webdevtutor
webdevtutor.net › blog › typescript-string-null-to-string
Converting TypeScript String to String when dealing with Null Values
Optional Chaining: Another way to achieve this conversion is by using optional chaining, which allows you to access properties even when the value is null or undefined: let myString: string | null = null; const myStringLength = myString?.length; // Returns undefined if myString is null · Type Guards: Type guards are a powerful feature in TypeScript that enable you to narrow the type of a variable based on certain conditions.
🌐
typescriptlang.org
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
There won’t be an exception or null generated if the type assertion is wrong. TypeScript only allows type assertions which convert to a more specific or less specific version of a type. This rule prevents “impossible” coercions like: ... x = "hello" as number;Conversion of type 'string' to ...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-type-null-is-not-assignable-to-string
A Guide to Resolving TypeScript's Type 'null' is Not Assignable to 'string'
The error message indicates that TypeScript cannot assign a value of type null to a variable or property that has been declared as a string. This is because null is not considered a subtype of string.
🌐
Pine
pineco.de › snippets › casting-null-values-string
Casting Null Values to String - Pine
July 5, 2021 - // Cast null to string // Then you can perform any "string" action (value || '').toString(); // Chain the methods after this
🌐
DEV Community
dev.to › gabrielanhaia › from-string-to-string-null-undefined-for-kotliners-2ic8
From `String?` to `string | null | undefined` for Kotliners - DEV Community
May 2, 2026 - Narrowing replaces smart-cast. Kotlin smart-casts on if (x != null). TypeScript narrows on a few forms. The strict version is if (x !== null && x !== undefined). The shorthand if (x != null) uses loose equality and narrows both at once. A typeof x === "string" check narrows by type.
🌐
TutorialsPoint
tutorialspoint.com › how-null-is-converted-to-string-in-javascript
How null is converted to String in JavaScript?
August 11, 2022 - The String() in JavaScript is used to convert a value to a String. To convert the null into a String just pass the null into this method.
🌐
GeeksforGeeks
geeksforgeeks.org › explain-the-concept-of-null-and-its-uses-in-typescript
Explain the concept of null and its uses in TypeScript - GeeksforGeeks
March 2, 2022 - In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string. TypeSc · 4 min read Explain the concept of null and its uses in TypeScript
🌐
Pinter Computing
pinter.org › home › knowledge base
Type ‘null’ is not assignable to type ‘string’ – Pinter Computing
export type Incident = { id: number; name: string; start_date_utc: string | null; end_date_utc: string | null; description: string; } ... DevOps Engineering part 1. (Mac) - Make your Macintosh easier to use June 25, 2026
Top answer
1 of 4
41

After reviewing my previous answer, it seems a complete overhaul of my previous answer is necessary. I was way over complicating it, as the short answer is that these are standards-specified special cases.

The specification for String() (String used as a function):

15.5.1.1 String ( [ value ] )

Returns a String value (not a String object) computed by ToString(value). If value is not supplied, the empty String "" is returned.

The ToString function (that exists internally, not in userland) is defined as follows (9.8):

"The abstract operation ToString converts its argument to a value of type String according to Table 13"

Argument Type | Result
Null | "null"
Undefined | "undefined"

This means that String(null) and String(undefined) go into this special table of types and just return the string values valued "null" and "undefined".

A user-land pseudo-implementation looks something like this:

function MyString(val) {
    if (arguments.length === 0) {
        return "";
    } else if (typeof val === "undefined") {
        return "undefined";
    } else if (val === null) {
        return "null";
    } else if (typeof val === "boolean") {
        return val ? "true" : "false";
    } else if (typeof val === "number") {
        // super complex rules
    } else if (typeof val === "string") {
        return val;
    } else {
        // return MyString(ToPrimitive(val, prefer string))
    }
}

(Note that this example ignores the constructor case (new MyString()) and that it uses user-land concepts rather than engine-land.)


I got a bit carried away and found an example implementation (V8 to be specific):

string.js:

// Set the String function and constructor.
%SetCode($String, function(x) {
  var value = %_ArgumentsLength() == 0 ? '' : TO_STRING_INLINE(x);
  if (%_IsConstructCall()) {
    %_SetValueOf(this, value);
  } else {
    return value;
  }
});

macros.py:

macro TO_STRING_INLINE(arg) = (IS_STRING(%IS_VAR(arg)) ? arg : NonStringToString(arg));

runtime.js:

function NonStringToString(x) {
  if (IS_NUMBER(x)) return %_NumberToString(x);
  if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
  if (IS_UNDEFINED(x)) return 'undefined';
  return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
}

The NonStringToString (which is essentially what is of interest), is luckily defined in psuedo-JS-land. As you can see, there is indeed a special case for null/true/false/undefined.

2 of 4
2

There is probably just some extra checks and handling for special cases like null and undefined.

MDN says:

It's possible to use String as a "safer" toString alternative, as although it still normally calls the underlying toString, it also works for null and undefined.

🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
TypeScript for Beginners, Part 2: Basic Data Types | Envato Tuts+
January 28, 2022 - After reading the introductory ... it and then compile it to JavaScript. In this tutorial, you will learn about different kinds of data types available in TypeScript. JavaScript has seven different data types: Null, Undefined, Boolean, Number, String, Symbol (introduced in ES6), ...