Using a juggling-check, you can test both null and undefined in one hit:
if (x == null) {
If you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:
if (x === null) {
You can try this with various values using this example:
var a: number;
var b: number = null;
function check(x, name) {
if (x == null) {
console.log(name + ' == null');
}
if (x === null) {
console.log(name + ' === null');
}
if (typeof x === 'undefined') {
console.log(name + ' is undefined');
}
}
check(a, 'a');
check(b, 'b');
Output
Answer from Fenton on Stack Overflow"a == null"
"a is undefined"
"b == null"
"b === null"
Using a juggling-check, you can test both null and undefined in one hit:
if (x == null) {
If you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:
if (x === null) {
You can try this with various values using this example:
var a: number;
var b: number = null;
function check(x, name) {
if (x == null) {
console.log(name + ' == null');
}
if (x === null) {
console.log(name + ' === null');
}
if (typeof x === 'undefined') {
console.log(name + ' is undefined');
}
}
check(a, 'a');
check(b, 'b');
Output
"a == null"
"a is undefined"
"b == null"
"b === null"
if( value ) {
}
will evaluate to true if value is not:
nullundefinedNaN- empty string
'' 0false
typescript includes javascript rules.
Videos
TypeScript types are only enforced at compile time and do not exist at runtime. Given this, if I define a TypeScript function that accepts a string parameter, is it necessary to perform null checks? This question arises because if the API returns null, it could lead to runtime errors.
Consider the following code snippet:
const capitalise = (val: string) => {
// Question: Is this check redundant due to TypeScript's type checking?
if (!val) {
return '';
}
return val.toUpperCase();
};
// Assume 'name' is fetched from an API or another source that could return null
const name: string = /* fetch from API or another source */;
capitalise(name);What are the best practices in such scenarios to ensure runtime safety in TypeScript applications?
I'm coming at this as a typescript beginner and haven't had a deep dive into the docs. So apologies if I'm missing something very obvious.
During development specifically, I feel like I'm adding null checks to my code too early to make TS happy, and this makes it harder to reason about my code. It'll go like this:
-
I perform an operation on a variable that can be null sometimes
-
TS alerts me to this, so I add a null check with an elvis operator/ternary/if
-
The variable is in fact null in an unexpected scenario, so I have a functional error in my app
-
Because I'm null-checking to shush the compiler, I don't immediately know where the bad value originates from and have to dig around for it
I can imagine this getting very annoying once I'm several layers of calls deep. So, how do I think about this? Should I be checking for null values differently? Should I always be doing something in the control flow to indicate an unexpected null was passed? Should I be throwing more?
Advice welcome. Thanks!