Question: I am reading a file and looking for this string in the file.looking to capture the value(00:00:22) using regex.I have written regular expression but its not finding that value?

```
"20.10.02 00:00:22:135 INFO Running Cron : BatchJob"

Pattern p = Pattern.compile("([01][0-9]|2[0-3]):([01][0-9]|2[0-3])  INFO       Running Cron", Pattern.CASE_INSENSITIVE);
                        Matcher m = p.matcher(line);
                            System.out.println(m.find());

```

Answer: 
This is not possible in TS right now. because it adds considerable complexity to their Control-Flow Analyzer. other than explicitly putting `foo !== null` as the `if expression`, there two possible things to do:

1- using [Assertions](https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions):

```
if (test){
    // since test is equal to (foo !== null)
    foobar(foo as string)
}

```

But it's a bit bug-prone because you may change the `test` variable in the future then that type assertion would lead to bugs because foo is no longer for sure a `string`.

2- [User-Defined Type Guards](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards)

The better way in my opinion is using user-defined type guard as @VLAZ correctly mentioned.

```
   const isNotNull = <T>(x:T): x is Exclude(T, null) => x !== null

   if (isNotNull(foo)){
     foobar(foo)
   }

```
Next version of Typescript (4.4) [will support control flow analysis of conditional expressions](https://github.com/microsoft/TypeScript/pull/44730). The only change you'll need to do is `let foo` -> `const foo`, because

> 
> Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a `const` variable declaration with no type annotation, and the reference being narrowed is a const variable, a readonly property, or a parameter for which there are no assignments in the function body
> 
> 
> 

[Playground v4.4](https://www.typescriptlang.org/play?ts=4.4.0-dev.20210702#code/MYewdgzgLgBAZiEMC8MCyBDKALAdAJwzABMQBbACgEoYA%20GXAVhgH4YwBXAGy5gC4YAcgBGGfIICwAKFCRYIHAFN8ABXwgADihhR8HRdNnQdi46gRIAhMlSceMAGQOYC7MrWbpcDmGBQAluDwiKL4FAAeAtD4-mAA5jQA3jAAvtLSAPQZMBDYINzEMMrq%20F4hYhQWVOlSWTl5BewKRfgl0v5wMBRQplBJ0jCDwSChlYjVUilAA)