Typorithm · hard

15. Optional preview: TypeScript 7.1 beta

typescriptoptional-beta-previewinterviewindependent-practice
Saved on this device
Full lesson · 4 learning steps Open any exercise for focused practice
1

Step 1 of 4 · intro card

15. Optional preview: TypeScript 7.1 beta

0%

Concept and contract

Explore preview import-attribute declarations in an isolated compiler fixture.

This is an optional preview lesson reviewed on 5 November 2026. TypeScript 7.1 is beta, not the stable requirement for the course. Its examples use a separately pinned 7.1.0-beta compiler and must not silently upgrade the application or block completion of stable lessons.

Preview features can change before release. Keep their fixtures isolated and document the required compiler and host support. A declaration accepted by the beta compiler does not prove that a browser, Node version or bundler implements the corresponding runtime loading behavior.

Mechanism and worked example

The example imports text using an import attribute and a companion ambient declaration matching that attribute.

The example imports text using an import attribute and a companion ambient declaration matching that attribute. In assets.d.ts, declare module "*" with { type: "text" } exports a string. The source then receives the type associated with the attribute rather than only a filename extension.

The companion declaration belongs in a global declaration file without top-level imports or exports. Matching an ambient pattern does not verify that the file exists. A real build must still test file availability and the host’s interpretation of the requested attribute.

Complete worked example

typescript

import note from "./notes.txt" with { type: "text" };
const length: number = note.length;
Reasoning
The matching declaration is supplied in a separate global assets.d.ts fixture; runtime loading remains host-specific.

Counterexamples and common mistakes

The rejected assignment uses a numeric receiving variable for a string.

The rejected assignment uses a numeric receiving variable for a string. In the preview fixture, the more interesting contract is that the import’s declared result matches the selected attribute. Test wrong attribute or result assumptions in isolated files instead of broad assertions.

Do not publish catch-all ambient declarations casually in a shared library. They can conflict with consumers’ host libraries and future built-in declarations. The preview also introduces source-phase imports, new library declarations and a new compiler API, each with separate runtime and compatibility considerations.

Rejected counterexample: expected compiler diagnostic

typescript

const note: string = "TypeScript preview";
// @ts-expect-error A text import result is not numeric.
const length: number = note;
Reasoning
The result contract remains specific even in a preview experiment.

Independent practice

In an isolated beta workspace, add a bytes attribute declaration exporting Uint8Array and import a small binary asset with that attribute.

In an isolated beta workspace, add a bytes attribute declaration exporting Uint8Array and import a small binary asset with that attribute. Read byteLength into a number. Begin with the provided text example, then change only the declaration and import attribute.

Compile with the pinned beta and verify the actual asset in a supporting build separately. Optional extension: compare a source-phase WebAssembly import. That extension is not required and must not be advertised as universally supported runtime behavior.

Step 1: Compile the provided text attribute fixture in isolation. Step 2: Change the declaration and import together for bytes.

Practice starter: solve before reading the solution

typescript

// assets.d.ts: declare a bytes attribute module exporting Uint8Array.
// example.ts: import the binary asset with type: bytes and read byteLength.
Reasoning
Change one attribute and declaration at a time in an isolated workspace.
  • Hint 1: The companion declaration is a global .d.ts file.
  • Hint 2: The real host still needs asset-loading support.

Solution, tests and trade-offs

The solution uses an attribute-sensitive ambient declaration and a matching bytes import.

The solution uses an attribute-sensitive ambient declaration and a matching bytes import. The compiler checks byteLength through Uint8Array. The fixture intentionally stops at compile-time evidence because the lesson environment does not provide a universal binary-asset loader for every host.

A complete integration test should build and run under the specific chosen host with the real asset. Keep that test distinct from the compiler fixture, and record preview status in documentation. Do not infer runtime support merely because a new lib declaration exists.

Practice solution and acceptance checks

typescript

import bytes from "./sample.bin" with { type: "bytes" };
const length: number = bytes.byteLength;
Reasoning
Uint8Array typing establishes byteLength statically, but does not supply a binary runtime loader.

Interview reasoning and next step

In interviews, explain the boundary between syntax support, type declarations and executable host support.

In interviews, explain the boundary between syntax support, type declarations and executable host support. Discuss preview adoption as an experiment with a rollback path, not a mandatory migration for ordinary API or React work.

Return to the stable capstone after this optional exploration. The core track remains useful without beta features, and its independent tasks should remain approachable for readers who are still building confidence with everyday TypeScript.

Optional stretch — after the core task works: Explore source-phase WebAssembly imports in a supporting host.

Explore preview import-attribute declarations in an isolated compiler fixture.