I have a multiple variable null check:
const path = window.location.pathname
const regex_list = path.match(/\d+/g) // 100% have at least 1 number
const a = document.querySelector('')
const b = document.querySelector('')
if (![regex_list, a, b].includes(null)) {
regex_list[0]
}
TS gives error:
'regex_list' is possibly 'null'. (tsserver 18047)
Even here:
if (![regex_list].includes(null)) {
regex_list[0]
}
Info about regex_list:
const regex_list: RegExpExecArray | null
If it is not null than it must be RegExpExecArray, but TS doesn't think so. Is it because it only supports ==/!=/===/!== operators? If yes, then can I safely ignore this error with // @ts-ignore or regex_list![0]?
Update:
Since there are already answers to the main question, I also found an answer to the sub-question about ignoring the error. Instead, an assertion can be used to narrow the type: (regex_list as RegExpExecArray), but it has to be done for every reference of the regex_list (answer).
Array.include()is defined as following :As you can see, there is no predicate, so typescript won't narrow down and remove the
nullfrom the type.