{
  "id": 117690,
  "title": "TypeScript `asserts` and Type Predicates in 2026: Writing Guards That Actually Narrow Correctly",
  "url": "https://urgent.news/2026/08/04/typescript-asserts-and-type-predicates-in-2026-writing-guards-that",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-04T06:27:59.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/jsmanifest/typescript-asserts-and-type-predicates-in-2026-writing-guards-that-actually-narrow-correctly-3kid"
  },
  "original_language": "en",
  "account": null,
  "summary": "TypeScript's type system relies heavily on guards to ensure that runtime values conform to expected types. These guards come in two primary forms: type predicates and assertion functions. The distinction between these two is crucial for writing guards that actually narrow types correctly. Type predicates, denoted by `value is Type`, return a boolean value and enable conditional narrowing. When the predicate returns `true`, TypeScript narrows the type of the input parameter to the specified type within the scope where the guard is evaluated. This mechanism allows developers to write more precise and safe code. On the other hand, assertion functions, denoted by `asserts value is Type`, do not return a value but instead throw an error if the condition is not met. This unconditional narrowing ensures that the type safety is enforced throughout the remainder of the scope where the guard is used. The key takeaway from this article is that developers often mistakenly use boolean-returning functions as type predicates, which leads to validation theater—code that performs checks but does not genuinely narrow types. To avoid this pitfall, it is essential to use the appropriate syntax (`value is Type` for predicates and `asserts value is Type` for assertions) based on whether the guard should return a boolean or throw an error. This distinction is particularly important in handling runtime validation, where failing to narrow types correctly can result in silent bugs that only surface in production.",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}