DEV Community

Cover image for Trick or Type Guard? Surviving `unknown` with is-kit πŸŽƒ

Trick or Type Guard? Surviving `unknown` with is-kit πŸŽƒ

nyaomaru on October 31, 2026

Hoi hoi! πŸ‘‹ I'm @nyaomaru, a frontend engineer who is scared of horror things πŸ™€ Happy Halloween! πŸŽƒπŸ‘» Tonight, we're exploring a haunted TypeScript...
Collapse
 
stackmasteraliza profile image
Aliza Ali •

A vampire called any is the most accurate final boss TypeScript has. One spooky footnote for the zombie hunters: a hand-written type guard is a promise the compiler never checks. If isZombie returns true for the wrong shape, everything downstream narrows confidently to the wrong type, which is exactly why small, composable, well-tested guards beat one big hand-rolled one. Bonus treat: since TS 5.5, simple filters like arr.filter(x => x !== null) infer the predicate for you. Happy Halloween, and congrats again on 100 stars!

Collapse
 
nyaomaru profile image
nyaomaru •

That’s such a good footnote πŸŽƒ

A type guard really is a promise to the compiler, so if the guard lies, everything after it can become confidently wrong. That’s exactly why I prefer small, composable guards with focused tests instead of one giant hand-written validator. πŸ”§

Happy Halloween, and thank you again for the 100-star congrats! 😸⭐

Collapse
 
johnnylemonny profile image
𝗝𝗼𝗡𝗻 • • Edited

"The real monster isn't unknown. It's unchecked any." β€” Count Any has definitely claimed a few production servers over the years! πŸ§›πŸ’€

Love the Halloween framing here, nyaomaru. Explaining the difference between a runtime type guard (proving the value is safe) vs. a type assertion (as any just convincing the compiler to look the other way) through these spooky examples makes it super intuitive. Composing small guards with struct and or keeps the code so clean without needing a heavy schema setup.

Collapse
 
nyaomaru profile image
nyaomaru •

Thx, john! 😸

I’m really glad the runtime guard vs type assertion difference came through clearly. That was one of the main things I wanted the story to make intuitive. πŸ‘

And yes, I really like keeping guards small and composable with things like struct and or.

any really is a monster. Terrifying πŸ™€

Another one that scares me just as much is @ts-ignore... but I think that’s a different monster for another story. πŸ˜ΉπŸ‘»

Collapse
 
mudassirworks profile image
Mudassir Khan •

safeParse returning just { valid: false } is the part i would poke at. on a real webhook payload with 30 fields you want to know which key failed, otherwise the first production bug is a log line that says invalid and nothing else.

also struct allowing extra properties by default is a quiet footgun at a trust boundary. we turned exact on for anything crossing a service edge and it surfaced two producers sending fields nobody had documented.

do you plan to add a path or reason to the failed result, or is plain boolean on purpose?

Collapse
 
nyaomaru profile image
nyaomaru •

That’s a fair point 😸

The plain { valid: false } is intentional for now. is-kit is mainly focused on small composable type guards and narrowing, rather than rich validation diagnostics.

So TypeScript helps us after the value is narrowed, but of course it can’t tell us which field in an external runtime payload was invalid.

For production service boundaries, I usually keep validation centralized and use { exact: true } when the contract should reject extra fields.

A path/reason API could definitely be useful, but I’d want to add it without making the simple guard model much heavier. πŸ‘€

Collapse
 
mp3sss31 profile image
mp3sss •

Fun Halloween framing, and the composable guards hit the real pain point β€” this maps almost one-to-one onto Electron IPC: every payload crossing the contextBridge boundary arrives as unknown, and a small set of composable guards is exactly what keeps that trust boundary from becoming a security hole. One tip: centralize the checks in schema-style validators at the boundary instead of scattering typeof checks through the renderer, so one malformed message can't silently narrow its way past.

Collapse
 
nyaomaru profile image
nyaomaru •

That’s a great example 😸

Electron IPC is exactly the kind of boundary where unknown + reusable guards makes sense.

In production services, I usually create a clear boundary in utils or lib and use is-kit there, rather than scattering checks throughout the app.

And I agree, centralizing validation at the boundary is much safer. πŸ‘€

Collapse
 
technogamerz profile image
π“π‘πž π‹πšπ³π² 𝐆𝐒𝐫π₯ • • Edited

Happy Halloween!! πŸ‘»πŸŽƒπŸ§Ÿβ€β™€οΈπŸ§›πŸ»β€β™€οΈβš°οΈπŸšοΈ

Collapse
 
nyaomaru profile image
nyaomaru •

Happy Halloween!!! πŸ‘»πŸŽƒπŸ˜Έ
Hope your night is full of treats and zero Count Any! πŸ§›πŸ’€πŸ¬

Collapse
 
technogamerz profile image
π“π‘πž π‹πšπ³π² 𝐆𝐒𝐫π₯ •

😰

Collapse
 
paullovelace profile image
Paul Lovelace •

The vampire called any as final boss is annoyingly accurate β€” and the footnote about a hand-written guard being a promise the compiler never checks is the part that actually worries me. Small composable guards help, but only if they're tested like they can lie. Do you test the guards themselves against wrong shapes, or is that the bit everyone assumes and nobody does?