I'm encountering a linting error on fileNameMatches[0]
in the following code snippet. Strangely, the error doesn't appear on the Boolean()
check. Even if I remove that check, the issue remains unresolved. Can anyone suggest a solution?
protected getLocalFilename(): string {
const fileNameMatches: string[] | null = this.filePath.match(/\/(.+\.js)$`/);
if (Boolean(fileNameMatches) && fileNameMatches[0] !== null) { // TS2531: Object is possibly 'null'
return fileNameMatches[0]; // TS2531: Object is possibly 'null'
} else {
throw Error("No filename found during regex matching");
}
}
Another Attempt:
if (typeOf fileNameMatches !== "null") {
The above test also triggers the "Object is possibly 'null'"
warning.