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

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

Open
dandv opened this issue Dec 8, 2024 · 2 comments · May be fixed by #6245
Open

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

dandv opened this issue Dec 8, 2024 · 2 comments · May be fixed by #6245
Labels
bug Something isn't working needs triage

Comments

@dandv
Copy link
Contributor

dandv commented Dec 8, 2024

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
@kt3k
Copy link
Member

kt3k commented Dec 10, 2024

Why not just call assertExists for narrowing? Narrowing by these assertions feels like obscure side effects to me.

@kt3k
Copy link
Member

kt3k commented Dec 10, 2024

Another note: #6245 is a breaking change to nullable input in the current form. We currently don't have plan to make major upgrade to std packages in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
2 participants