'Object is possibly 'null' even though it can't be null

I have a regex expression that searches for a date string :

const regexObject: Regexes = {
    REGEXP_PATTERN_DATE_NOSEP: /^\d{6,8}$/,
    REGEXP_PATTERN_DATE_SEP: /^\d{1,2}[\\\/ .:-]\d{1,2}[\\\/ .:-]\d{2,4}$/,
    REGEXP_PATTERN_DATE_YEARFIRST: /^\d{2,4}[\\\/ .:-]\d{1,2}[\\\/ .:-]\d{1,2}$/,
    REGEXP_PATTERN_DATE_MONTHNAME: /^\d{1,2}[\\\/ .:-]\D{3,8}[\\\/ .:-]\d{2,4}$/,
    REGEXP_PATTERN_DATE_NODAY_MONTHNAME: /^\D{3,8}[\\\/ .:-]\d{2,4}$/
}

The important regexp here is REGEXP_PATTERN_DATE_SEP.

I have a function that checks matches in a string for these regexes and already checks if null or not and calls another function that parses the date.

function getPattern(date: string) {
    let regexName: keyof typeof regexObject

    for (regexName in regexObject) {
        parseInput(date, regexName)
    }
}

function parseInput(date: string, pattern: keyof Regexes) {
    let match = date.match(regexObject[pattern])
    if (match) {
        let input: string = date
        console.log(CASES[pattern](input) as parsedDate)
    }
}

But to parse the date, I need to get the special character the input has (eg. \ or / or . or : or - or space). I do that in the CASES object methods :

const CASES: Regexes = {
    REGEXP_PATTERN_DATE_NOSEP: function (input: string) {
        let [day, dateWithoutDay] = stringSplice(input, 0, 2)
        let [month, year] = stringSplice(dateWithoutDay, 0, 2)
        return {
            day,
            month,
            year,
        }
    },
    REGEXP_PATTERN_DATE_SEP: function (input: string) {
        let reg = /[\\\/ .:-]/
        let specialCharacter = input.match(reg)[0]
        return {
            day: "",
            month: "",
            year: "",
        }
    },
    REGEXP_PATTERN_DATE_YEARFIRST: function (input: string) {
        return {
            day: "",
            month: "",
            year: "",
        }
    },
    REGEXP_PATTERN_DATE_MONTHNAME: function (input: string) {
        return {
            day: "",
            month: "",
            year: "",
        }
    },
    REGEXP_PATTERN_DATE_NODAY_MONTHNAME: function (input: string) {
        return {
            day: "",
            month: "",
            year: "",
        }
    },
}

But I have an ESLINT error at

let specialCharacter = input.match(reg)[0]

saying that "input.match(reg)" can be null even though I know it can't because I already check if there's a match for REGEXP_PATTERN_DATE_SEP in the function "parseInput"...

Do I really have to check for null in CASES or is it something I should open a issue for (on TS github)? I don't really want to disable strict nullity check nor I want duplicates in my code so I don't really know what I should do here... Could anyone help?



Solution 1:[1]

If you know it cannot POSSIBLY EVER be null (or undefined), you can assert it:

let specialCharacter = input.match(reg)![0];

However you should use this judiciously as it gives you a false sense of security.

It complains here because TypeScript has no guarantee that input and reg are the same as what lead to this step, or the association that if this step is executed then it must be non-null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hittingonme