Close the earlier solutions before this checkpoint. Rebuild a familiar unknown-to-Result decoder, then derive its output type. This combines intermediate validation with conditional inference rather than asking you to invent another runtime abstraction.
Decoder<T> is a function contract. Decoded<D> matches that contract to recover its success value type. The decoder still has to inspect its input. A conditional type can describe what the decoder promises, but cannot prove its body validates correctly.
Use a count that allows zero and rejects negative, fractional and nonnumeric inputs. Keep a distinct error branch. Your success type should follow from typeof decodeCount rather than repeating number at the consumer. Work for about twenty-five minutes before opening hints.
Predict, check, and run
Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.
type Input<F> = F extends (value: infer V) => unknown ? V : never;
function label(title: string): string {
return title.trim();
}
const raw: Input<typeof label> = " Types ";
console.log(label(raw));Enable JavaScript to edit and type-check this example.
Check your prediction
TypesAcceptance cases before you code
The warm-up above extracted a function's parameter type. Your extractor matches a different shape: a Decoder, to recover its success type. Predict each case before you code.
| Case to try | What your result should demonstrate |
|---|---|
| Valid zero | Return success and preserve its derived numeric type |
| Text zero or fractional number | Reject at runtime |
| Consumer assigns wrong value type | Compiler rejects the derived contract |
| Keep the Decoder signature but weaken its body | Type extraction still succeeds; an invalid-input runtime test exposes the defect |
The last row is the point of the checkpoint: the type extractor and the runtime decoder can fail independently, so each needs its own evidence.
Build it yourself
Checkpoint: Decoder Types: practice
Implement decodeCount and Decoded. Derive Count using typeof decodeCount, check zero succeeds and "0" fails, then print 0 and false. Assert that negative and fractional inputs also fail.
// Start from the stated contract. Add a valid call and an edge case.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
The runtime numeric checks and the type extractor are separate responsibilities.
Hint 2: a first step
Declare decodeCount with an explicit Decoder type annotation first. For Decoded, ask what shape you want to match and where in that shape the success type appears; that is where infer goes.
Show one solution
type Result<T> =
| {
ok: true;
value: T;
}
| {
ok: false;
message: string;
};
type Decoder<T> = (value: unknown) => Result<T>;
type Decoded<D> = D extends Decoder<infer T> ? T : never;
const decodeCount: Decoder<number> = value => {
if (
typeof value !== "number" ||
!Number.isFinite(value) ||
!Number.isInteger(value) ||
value < 0
) {
return { ok: false, message: "Invalid count" };
}
return { ok: true, value };
};
type Count = Decoded<typeof decodeCount>;
const zero: Count = 0;
const valid = decodeCount(zero);
for (const input of [-1, 1.5, Infinity]) {
if (decodeCount(input).ok) {
throw new Error("Invalid count accepted");
}
}
console.log(valid.ok ? valid.value : valid.message);
console.log(decodeCount("0").ok);Enable JavaScript to edit and type-check this example.
0
falseDecoded<D> matches D against Decoder<infer T>, so Decoded<typeof decodeCount> is number, read from the decoder's own annotation; change the decoder to Decoder<string> and const zero: Count = 0 stops compiling with no edit to the consumer. That extraction is pure type information. The loop over -1, 1.5 and Infinity, and the "0" line, are the only evidence that the body actually enforces the count rule: weaken it and the types stay exactly as they are.
Change it and test it
Add null and NaN cases. Then deliberately weaken the runtime predicate while leaving Decoder<number> intact. Explain why type extraction still passes and a behavioral test must catch the defect.
Completion is saved in this browser only. It does not measure mastery.