I've been using tslint for quite some time with no-null-keyword
enabled. Recently, I've upgraded to Typescript 2.0 and enabled --strictNullChecking
. However, looking at the Typescript's lib.d.ts
, it appears impossible to keep no-null-keyword
enabled (at first glance), since the result of some calls can be null
. For example:
const result: RegExpExecArray | null = regex.exec(regexStr);
if (result === null) { // <-- tslint complains about this check
throw new Error("Foo location: result of regex is null.");
}
// or
// if (result !== null) {
// ...do something
// }
The question is what is the-right-thing-to-do?
Disable no-null-keyword
for tslint?
Use a hack (?):
const result: RegExpExecArray = regex.exec(regexStr)!;
if (result == undefined) { // Will check if result is undefined or null
throw new Error("Foo location: result of regex is null.");
}
Or something else?