sindresorhus

ts-extras

Essential utilities for TypeScript projects

by sindresorhus

TypeScript

TypeScript100%

Commit activity

Last 12 weeks · 0 commits

Full graph →

Community health

4 of 6 standards met

Community profile →
71
✓README✓License✓Contributing✓Code of Conduct○Issue Template○PR Template

Recent PRs & issues

Quiet · 1 discussion · Last activity 3mo ago
See all on GitHub →
jroru

Description and currently accept any type: export function assertPresent(value: T, message?: string): asserts value is NonNullable export function assertDefined(value: T | undefined, message?: string): asserts value is T This means the following is allowed and is a no-op at the type level: const s: string = 'hello'; assertPresent(s); // OK, but unnecessary assertDefined(s); // OK, but unnecessary I would expect these functions to only accept types that can actually be nullish, so the call is rejected when the value is already known to be present/defined. That would catch accidental no-op assertions at compile time. Proposed signature Something like: export function assertPresent( value: null extends T ? T : undefined extends T ? T : never, message?: string, ): asserts value is NonNullable export function assertDefined( value: undefined extends T ? T : never, message?: string, ): asserts value is NonNullable Or the rest-parameter version for slightly more reliable inference: export function assertPresent( value: T, message?: string, ..._mustBeNullish: null extends T ? [] : undefined extends T ? [] : [never], ): asserts value is NonNullable Behavior Why Makes the intent of the call clearer (you only assert when the type system thinks it might be missing). Catches accidental calls on already-narrowed values. Aligns with the common expectation that an assertion function is only useful when the type can still be nullish. Alternatives considered Leave the signature as-is and rely on a custom ESLint rule (more friction for consumers). Only document the recommended usage (doesn’t prevent the no-op case). Happy to open a PR with the change + tests if this direction looks good.

jroru · 2d ago
fregante
Assert booleansClosedIssue

What do you think about a boolean assertion? I think at least an could be flexible enough to allow all other assertions ()

fregante · 3mo ago
fregante

Would be great to have the assertions accept an error message like native APIs do https://developer.mozilla.org/en-US/docs/Web/API/console/assert_static https://nodejs.org/api/assert.html#assertvalue-message

fregante · 3mo ago
Structured data for AI agents

Repository: sindresorhus/ts-extras. Description: Essential utilities for TypeScript projects Stars: 810, Forks: 24. Primary language: TypeScript. Languages: TypeScript (100%). License: MIT. Latest release: v1.3.0 (3mo ago). Open PRs: 0, open issues: 1. Last activity: 3mo ago. Community health: 71%. Top contributors: sindresorhus, younho9, jonahsnider, ifiokjr, Max10240, carlrosell, fregante, tychenjiajun, mehcode.