How to check for null value inline and throw an error in typescript?

As long as TypeScript doesn’t support this natively, you could write a function similar to this one: function throwExpression(errorMessage: string): never { throw new Error(errorMessage); } which would then allow you to throw an error as expression: const myString = nullableVariable ?? throwExpression(“nullableVariable is null or undefined”)

How to check whether all properties of an object are null or empty?

You can do it using Reflection bool IsAnyNullOrEmpty(object myObject) { foreach(PropertyInfo pi in myObject.GetType().GetProperties()) { if(pi.PropertyType == typeof(string)) { string value = (string)pi.GetValue(myObject); if(string.IsNullOrEmpty(value)) { return true; } } } return false; } Matthew Watson suggested an alternative using LINQ: return myObject.GetType().GetProperties() .Where(pi => pi.PropertyType == typeof(string)) .Select(pi => (string)pi.GetValue(myObject)) .Any(value => string.IsNullOrEmpty(value));

Comparing a generic against null that could be a value or reference type?

I’m purposely only checking against null because I don’t want to restrict a ValueType from being equal to its default(T) That is a good insight, but don’t worry, you are already covered there. It is not legal to compare a T against default(T) using == in the first place; overload resolution will not find a … Read more

JavaScript null check

An “undefined variable” is different from the value undefined. An undefined variable: var a; alert(b); // ReferenceError: b is not defined A variable with the value undefined: var a; alert(a); // Alerts “undefined” When a function takes an argument, that argument is always declared even if its value is undefined, and so there won’t be … Read more