Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type narrowing to expect #6219

Open
dandv opened this issue Nov 29, 2024 · 0 comments
Open

Add type narrowing to expect #6219

dandv opened this issue Nov 29, 2024 · 0 comments
Labels
expect suggestion a suggestion yet to be agreed

Comments

@dandv
Copy link
Contributor

dandv commented Nov 29, 2024

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

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:

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.

@kt3k kt3k added suggestion a suggestion yet to be agreed expect labels Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expect suggestion a suggestion yet to be agreed
Projects
None yet
Development

No branches or pull requests

2 participants