Skip to content

Add type narrowing to expect #6219

Open
@dandv

Description

@dandv

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions