With `strictNullChecks` enabled (part of `strict` mode), `null` and `undefined` are not automatically assignable to other types — you must explicitly include them in a union.
ts
function findWallet(id: string): string | undefined {
const found = id === "1" ? "0xabc" : undefined;
return found;
}
const w = findWallet("1");
console.log(w.toUpperCase()); // error: w may be undefined
if (w) {
console.log(w.toUpperCase()); // OK, narrowed to string
}The non-null assertion operator `!` tells the compiler "trust me, this isn't null/undefined" — use sparingly, since it bypasses the safety strict null checks provide. Optional chaining (`?.`) and nullish coalescing (`??`) are the idiomatic way to work with possibly-absent values.