It depends on your compilerOptions. The strictNullChecks flag, to be precise.
strictNullChecks: false
function get(): string {
return null; // ok
}
function get(): string {
return undefined; // ok
}
In this scenario, you need extra checks against null and undefined.
strictNullChecks: true
The return value must be of type string. null and undefined are not allowed. No extra checks are necessary.
function get(): string {
return null; // Compile-time error
}
function get(): string {
return undefined; // Compile-time error
}
Check your tsconfig.json and set compilerOptions.strictNullChecks the way you prefer.