In Trick or Type Guard? Surviving unknown with is-kit 🎃, nyaomaru uses a Halloween story to explain how TypeScript programs can check data from outside their type system. The examples use is-kit, a library that combines runtime predicates and tells TypeScript what it can safely assume when those checks succeed.
Put the check where the data enters
The article assigns parsed JSON to a variable typed as unknown. That gives the caller a place to check the value before accessing a property or calling a method. A string guard makes an uppercase conversion possible once the check succeeds. Nyaomaru also gives the built-in typeof check as a reasonable choice for that single-field example. The library becomes easier to justify when the checks have to describe a whole object and several levels of properties.
Decide how strict the object boundary should be
The pumpkin example uses struct to compose guards for a name, a list of strings, a finite number, and a nested object. It uses optionalKey for a property that may be absent. A caller can use the successful branch of safeParse to work with the inferred shape. The failure example sends a string where the guard expects a list. That value fails validation, and the caller needs a separate policy for what to do with a rejected payload.
The article also points out a choice that matters when integrating with an API. The default shape check tolerates additional properties. Strict checking with exact: true rejects extra own enumerable string keys. This makes strictness a decision about the consumer's contract. A consumer that reads a known subset can tolerate fields a producer adds later. A consumer enforcing an exact payload shape has a different reason to turn the option on.
Narrow a union before handling it
Nyaomaru uses or to accept a string or number through one composed predicate. An object takes the rejection branch. The example makes the success condition visible at the call site, where TypeScript can narrow the value to those two types. A later example illustrates the consequence of casting a parsed value to any. The compiler permits a string method call on a field whose runtime value is numeric, so the call fails when it runs.
Where would you keep a boundary as a small handwritten predicate, and when would you share a composed guard across callers?