Core Types, Narrowing and Strict Mode

MODULE 05 · LESSON 5.1

Model known states and narrow unknown values before use.

Practice-firstBeginner-friendlyProduction-aware

The production problem this solves

Good work on Core Types, Narrowing and Strict Mode leaves evidence: a visible behavior, a stable contract or a repeatable operational check. This decision shapes the frontend boundary: what is rendered, what becomes interactive, and which state is allowed to cross into another component or route.

Here, that decision supports a specific checkpoint: Convert the tracker data layer to strict TypeScript. A reviewable result should include a focused component test, an accessibility check and a before/after browser trace rather than a claim that the feature simply works.

Core Types, Narrowing and Strict Mode workflowA four-step visual showing type inference, unions, narrowing, strict mode.Core Types, Narrowing and Strict Mode workflow1Type Inference2Unions3Narrowing4Strict Mode

Core Types, Narrowing and Strict Mode workflow

  1. 1Type Inference
  2. 2Unions
  3. 3Narrowing
  4. 4Strict Mode
Core Types, Narrowing and Strict Mode workflow: a practical sequence used in this lesson.

A practical model for core types, narrowing and strict mode

Model known states and narrow unknown values before use. The useful unit of understanding is the boundary: who owns the decision, which input crosses it, what result is visible and how a failure is reported.

  • Type Inference: Compare the simplest correct approach with one credible alternative.
  • Unions: State the assumption this concept relies on and show how the system behaves when it is false.
  • Narrowing: Connect this concept to the module checkpoint and identify the evidence a reviewer should expect.
  • Strict Mode: Explain the concept without framework jargon, then point to it in the working example.

Explain each moving part

The sample is intentionally narrow. Its job is to expose type inference without hiding the decision behind unrelated setup.

TYPESCRIPT
type LessonStatus = 'locked' | 'ready' | 'complete';
function label(status: LessonStatus): string {
  return status === 'complete' ? 'Done' : 'Continue';
}
Review it as someone else's change

Explain what the sample proves, what it does not prove, and which test would increase your confidence in unions.

Trace the implementation boundary

  1. 1
    Type Inference

    Keep names tied to the product rule so a reviewer can follow the change without decoding abbreviations.

  2. 2
    Unions

    Add a regression check close to the boundary where this behavior can fail.

  3. 3
    Narrowing

    Describe the behavior in one sentence, then choose the smallest input that can prove it.

  4. 4
    Strict Mode

    Add this responsibility at the narrowest sensible boundary; do not pull an unrelated layer into the change.

Mistakes that create hidden coupling

  • Treating type inference as vocabulary instead of defining the behavior it must produce.
  • Testing the expected path while ignoring an empty, invalid, repeated or unauthorized case around unions.
  • Allowing narrowing to cross a boundary without an explicit contract or useful error.
  • Changing several layers before capturing the first piece of evidence, which makes the original cause harder to see.

Debug from the boundary inward

  1. Reduce the problem to the smallest failing Core Types, Narrowing and Strict Mode case.
  2. Capture the actual input and output at the type inference boundary.
  3. Read the first relevant error, request, trace or query rather than the loudest downstream symptom.
  4. Test one explanation for the failure in unions; avoid changing two variables together.
  5. Keep a regression check that would expose the same defect if it returned.

Security decision

Assume data from props, storage, URLs and APIs can be malformed. Do not expose secrets in client bundles, and do not treat hidden UI as authorization.

Performance decision

Measure shipped JavaScript, rendering work and network waterfalls. Move work off the client only when the measured trade-off supports it.

PRACTICE

Build something you can inspect

Replace ambiguous strings with unions and enable strict compiler options.

Stretch challenge

Ask another person to run the exercise from your README. Fix the first place where their result differs from yours.

Definition of done

  • The behavior around type inference works with realistic input.
  • A failure involving unions is handled clearly and without leaking sensitive detail.
  • The implementation remains keyboard-usable when it produces an interface.
  • Your evidence directly supports the claim made in the exercise.
  • The README records the important trade-off without pretending the solution is universal.

Check your reasoning

Why is unknown safer than any at an external boundary?

Answer by naming the expected type inference behavior, the layer responsible for it and the evidence that would confirm your explanation.

Where would you investigate the first failure?

Start where unions crosses a boundary. Compare the actual input and output there before following downstream symptoms.

What would make this work reviewable?

Show the focused change, repeatable steps, the result of your check and one honest trade-off connected to narrowing.

What to carry into the next lesson

  • Model known states and narrow unknown values before use.
  • Keep type inference visible at the boundary where it can be tested.
  • Use evidence from unions before widening the implementation.

References and related reading

Progress is stored only in this browser.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.