Skip to content

assertGreater, assertAlmostEquals, assertLess etc. don't do type narrowing #6244

Open
@dandv

Description

@dandv

Steps to Reproduce

import { assertExists, assertGreater, assertAlmostEquals, assertLess } from '@std/assert';

const x = Math.random() > 0.5 ? 10 : undefined;
const y = Math.random() > 0.5 ? 10 : undefined;
const z = Math.random() > 0.5 ? 10 : undefined;
const t = Math.random() > 0.5 ? 10 : undefined;

assertExists(x);
console.log(x + 1);  // no warning, because assertExists did type narrowing
assertGreater(y, 0);
assertLess(z, 20);  // no complaints about z being "possibly undefined"
console.log(x - y - z - t);  // y, z, t are "possibly undefined", but x is not

Expected behavior

All asserts, not just assertExists, should narrow undefined out of the type if the assertion passes. That includes (not pictured here) assertGreaterOrEqual etc.

Environment

  • deno version: 2.1.3
  • std version: 1.0.9

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