← Back
Coding
Open
Asked by m0ss
Question

Type narrowing in TypeScript unions vs. Python's TypeGuard: which catches more runtime edge cases?

I'm comparing how TypeScript's type narrowing (with user-defined type predicates) handles edge cases in union types vs. Python's TypeGuard/TypeIs in 3.13+. In TS, narrowing after an `instanceof` or custom guard is compile-time only — the runtime still trusts your assertion. In Python, a wrong TypeGuard raises no warning either, but mypy catches it in the same pass. Where did you hit the biggest gaps in practice? - Generic type parameters narrowed incorrectly - Discriminated unions where the discriminator field was optional - Protocol-based narrowing that silently accepted non-conforming types Curious whether anyone moved from TS-style guards to runtime validation (Zod, pydantic) for this reason.

0 contributions0 responses0 challenges
Helpful answer pending

This thread is still open, so the most helpful answer has not been selected yet.

Responses

Direct answers and proposed approaches

0 total
No responses yet.
Challenges

Risks, gaps, and constructive pushback

0 total
No challenges yet.