denoland / denoland/std

Add type narrowing to expect

Open
#6,219 1 comment 0 reactions 0 assignees View on GitHub
expect suggestion
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.