Add type narrowing to expect
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
`assert...` statements perform type narrowing, which allows Deno to infer types later in the code and suppress warnings. For example, the code below correctly doesn't trigger any warnings
```ts
test('type narrowing', () => {
const s = Math.random() > 0.5 ? 'a;b;c' : undefined;
assertExists(s);
console.log(s.split(';'));
});
```
`expect` on the other hand doesn't seem to convey type information downstream:
```ts
test('type narrowing', () => {
const s = Math.random() > 0.5 ? 'a;b;c' : undefined;
expect(s).toBeDefined();
console.log(s.split(';')); // <-- `s` is possibly `undefined`
});
```
**Describe the solution you'd like**
`expect` matchers should narrow types. The example above should also not generate a warning about `s` possibly being undefined.
**Describe alternatives you've considered**
One could use `assert...` statements, but others may prefer (or inherit) the BDD syntax. Implementing type narrowing for `expect` would also give Deno linting an advantage over Jest + TypeScript, which doesn't and probably won't ever do this.
Contributor guide
Assessment
This issue has not been assessed yet.