typescript2.0 - tslint no-null-keyword and typescript's lib.d.ts with strict null checking -
i've been using tslint quite time no-null-keyword
enabled. recently, i've upgraded typescript 2.0 , enabled --strictnullchecking
. however, looking @ typescript's lib.d.ts
, appears impossible keep no-null-keyword
enabled (at first glance), since result of calls can null
. example:
const result: regexpexecarray | null = regex.exec(regexstr); if (result === null) { // <-- tslint complains check throw new error("foo location: result of regex null."); } // or // if (result !== null) { // ...do // }
the question the-right-thing-to-do?
disable no-null-keyword
tslint?
use hack (?):
const result: regexpexecarray = regex.exec(regexstr)!; if (result == undefined) { // check if result undefined or null throw new error("foo location: result of regex null."); }
or else?
no-null-keyword
lint rule. it's primary purpose prevent added complexity of using both undefined , null similar purpose.
but doesn't remove vendor codebase, , used in lot of libraries.
result == undefined
indeed valid js idiom if need check null , undefined values. not considered hack (afaik) , lot preferable simple dangerous falsy checking: if (!result) {..}
.
tslint allows exception ===
rule:
"triple-equals": [true, "allow-undefined-check"]
Comments
Post a Comment