Frontory

Open code, practice, and notes →

Download source with English comments · for reading

TypeScript · 07 unknown and Safe Access

Series contents · Technology map

Stage: Type Checking and Data Models · Editable example · Storage ID: typescript-step-07

Goals and learning outcomes

Prerequisites

Work and experiment records from Unit 6, “Function Arguments and Return Contracts”

Key concepts and explanations

The unknown Boundary

unknown prevents property access before checking. any bypasses checking itself, losing information at the boundary.

Basic Guards

Combine typeof and null checks to narrow values. Handle the fact that typeof null is object separately.

Practice setup

First check the execution scope guidance on the example screen. The following environment is for extending the assignment in a separate project. Implement it yourself in a separate local project and check it with browser developer tools and tsc --noEmit. Record the installed tsc version in a separate TypeScript project. Enable strict in tsconfig.json and check with tsc --noEmit, then open the runtime exercise using compiled output or a local development server. Do not assume the browser executes TS type annotations directly.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. In unknown.ts, prepare conditions for a string, a number, null, and an object.
  2. Write a function that accepts unknown and returns a string length using typeof branches.
  3. Run with a string, a number, null, and an object. Results to check: Record success or failure for all four inputs. Pass tsc checking without any or unconditional assertions. Record expected and actual results separately.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
InputstringnullPre-run hypothesis: the value will be rejected before property access. Keep other conditions fixed and record the actual result separately after running.

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. Can typeof object alone exclude null?
  2. Does using unknown generate runtime checking code?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

The preview is a JavaScript runtime boundary experiment. It does not run TypeScript diagnostics. Check lesson.ts locally with tsc --strict --noEmit --lib ES2023,DOM.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Function Arguments and Return Contracts · Next: Modeling Async State with Discriminated Unions

Open code, practice, and notes →