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)
   }

```
This is currently not possible (see [this issue](https://github.com/microsoft/TypeScript/issues/12184)).

Here's a workaround. It assigns `wrappedFoo` a type which either contains a string and true, or null and false. After checking for the boolean value, Typescript knows which of those two it is:

```js
const foo: string | null = Math.random() > .5 ? null : 'bar'
const wrappedFoo = foo === null ? { foo, isString: false as const } : { foo, isString: true as const }

if (wrappedFoo.isString){
    console.log(wrappedFoo.foo.length)
} else {
    console.log(wrappedFoo.foo.length) // Error Object is possibly 'null'
}

```