denoland / denoland/std

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

Open
#6,244 2 comments 0 reactions 0 assignees View on GitHub
bug needs triage
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Steps to Reproduce**

```ts
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

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.