首页›Stefan Judis›文章

The scope of type guards and assertion functions (#tilPost)

Stefan Judis · 2026年4月3日

AI 摘要 AI 生成,可能有误

The article explains the difference between TypeScript type guards and assertion functions when validating unknown data. A standard type guard (using 'value is User') narrows the type only inside the conditional scope, after which the variable reverts to its original type. In contrast, an assertion function (using 'asserts value is User') applies the type narrowing for the rest of the current scope, as long as the function doesn't throw an error.

I've just read Absorbing unknown Into the Type Realm. It's a good read, check it out.

However I also learned an important difference between TypeScript type guards and assertion functions. And to be fair, I didn't even know assertion functions were a thing in TypeScript.

Let's look at a standard type guard.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Stefan Judis

Web Weekly #204 (#blogPost)

AI 摘要Web Weekly #204 is a developer newsletter covering recent web platform news, including Firefox 161 shipping CSS Typed Object Model into Baseline, Microsoft working on CSS mixins for Chromium, Chrome's interestfor attribute adoption status, and Node.js abort signals for incoming requests. It also discusses the State of JS survey, an Open Web Advocacy petition urging Apple to open iOS to other browser engines under the DMA, compression dictionaries, and useful UI components from Vercel. The issue wraps up with a song recommendation, sponsor segment, community project highlights, and a reflection on the gap between the software humans could build versus what they actually build.

Web Weekly #203 (#blogPost)

AI 摘要Web Weekly #203 is a newsletter edition covering web development topics, including cross-browser support for scroll-driven animations, the CSS scroll-target-group property for scroll spy, and new fonts like Allo, Paper Mono, and Ductus. It also discusses concerns about missing MDN documentation for new browser features, the negative web performance impact of AI-generated sites, Google's Modern Web Guidance 1.0, and newly available features like the revert-rule CSS value and Intl.Segmenter. The issue rounds out with recommended tools and projects, a sponsor message, and a community song pick.

Knowember 2026 (#blogPost)

AI 摘要In this 'Knowember' post, the author shares their newly discovered passion for long-distance cycling, including participation in multiple 200km+ events such as the Berlin bike marathon. They reveal they have lived in Berlin, Germany for nearly twenty years, and mention that their blog, once focused on tech and frontend topics, may open up to any subject they feel like writing about.

Web Weekly #202 (#blogPost)

AI 摘要Web Weekly #202 is an indie newsletter roundup of web development news, headlined by Geoff Graham buying back CSS-Tricks from DigitalOcean and featuring topics like JPEG XL vs. AVIF, Safari's new adaptive dark mode, and the newly baseline-available ariaNotify. It also collects practical tips and resources, including modern FLIP animations with @property, workarounds for flex-wrap: balance, the Navigation API, the :state() pseudo-class, and various tools and projects. The issue closes with a manifesto against AI slop and a reflection that successful developers are those whose output others genuinely value.

Blake Watson on the state of the industry (#note)

AI 摘要The article discusses Blake Watson's conflicted feelings about AI in software development: he is excited to build things he never could before, but fears his hard-earned skills are becoming worthless and that he may be replaceable if prompting an AI is all the job requires. The author strongly agrees, noting that AI coding tools have advanced dramatically and 'personal software' is now real, while also sharing the same ambivalence—alternating between anxiety about being replaced and hope that everything will work out. Both conclude with the wish that Blake's optimism proves correct despite the unsettling changes in the industry.