NoInfer and inference control
Relay’s job machine is configured with a list of states and an initial state:
createMachine({ states: ["idle", "running"], initial: "stoped" }). The typo compiles. The
function’s type parameter S sits on both properties, so "stoped" counts as one more candidate,
S becomes "idle" | "running" | "stoped", and the check against the list passes trivially.
Lesson 2 of M3 showed how inference collects candidates. This lesson is about the other half: telling TypeScript that some positions must not add candidates.
Inference sites
Section titled “Inference sites”Every place where a type parameter appears in a parameter type is an inference site. Each
argument adds a candidate from the sites it matches. With a constraint like S extends string,
literal candidates are kept and joined into a union:
declare function createMachine<S extends string>(config: { states: readonly S[]; initial: S }): S
const s = createMachine({ states: ["idle", "running"], initial: "stoped" })// S = "idle" | "running" | "stoped": the typo widened the type instead of failingYou want one site to define S (the list) and the other to be checked against it.
NoInfer
Section titled “NoInfer”NoInfer<T> (TypeScript 5.4) marks a position that must be checked but not inferred from. The
compiler infers S from the other sites, then checks this argument against the result:
declare function createMachine<S extends string>(config: { states: readonly S[] initial: NoInfer<S>}): S
const ok = createMachine({ states: ["idle", "running"], initial: "idle" })// ok: "idle" | "running"
// @ts-expect-error: '"stoped"' is not assignable to '"idle" | "running"'createMachine({ states: ["idle", "running"], initial: "stoped" })At runtime NoInfer<S> is just S. It only changes inference.
The same fix works for defaults. A function that picks one of several values, with a fallback:
declare function withDefault<T extends string>(values: readonly T[], fallback: NoInfer<T>): T
const level = withDefault(["info", "warn"], "info")// level: "info" | "warn"
// @ts-expect-error: '"debug"' is not assignable to '"info" | "warn"'withDefault(["info", "warn"], "debug")Without NoInfer, "debug" would simply join the union and the call would be accepted.
Priority, and the older trick
Section titled “Priority, and the older trick”Not every site is equal. Arguments that are plain values are inferred first. Inline callbacks with
untyped parameters are postponed until the other arguments have fixed what they can (in practice, the compiler
infers left to right and delays context-sensitive functions), which is why
mapAll(items, (x) => ...) works in M3. NoInfer lets you take any position out of the vote.
Before TypeScript 5.4 the documented workaround was an extra type parameter, D extends C, for
the site that should not vote. Community tricks such as T & {} lowered the priority of a site
through the type system. They work, but nobody can read them. If you see one in an older library, it was doing what NoInfer now says by name.
When not to use it
Section titled “When not to use it”Add NoInfer after you see a wrong call compile, not in advance. It is a signal for readers
that “this argument is only checked”, so use it where that is true. Don’t wrap every parameter:
if several parameters are unmarked and all define the type, a conflict gives a clear error
already.
Exercise
Section titled “Exercise”Two Relay helpers accept bad input today. Mark the argument that must only be checked so a typo
in initial or in the fallback is an error. The runtime code already works, the types don’t.
- A type parameter is inferred from every site it appears in. With a string constraint, literal candidates join into a union, so a typo becomes part of the type.
NoInfer<T>marks a position that is checked againstTbut gives no candidate.- It has no runtime effect, and it is TypeScript 5.4 and later.
- The old
T & {}trick did the same thing less clearly. - Use it when a wrong call compiles, not by default.
Further reading
Section titled “Further reading”- TypeScript 5.4: the
NoInferutility type.