Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Sharing Types Across Modules

In this video, you moved the league’s Team, Player and Fixture types out of the match helpers and into a module of their own, types.ts, so that every script can import them instead of keeping its own copy. You saw what Node does with an ordinary import of a type, why the checker reports it first, and how type in an import fixes it.

Key Points

  • Export a type with the same export keyword you’d use for a function. Put export in front of each type in types.ts, as in export type Fixture = { ... };, and other modules can import it.
  • The import paths end in .ts because Node runs these files as they are. Why they need that ending is the next lesson’s question.
  • An ordinary import of a type stops Node. Node strips the types from each file before it runs it. Every line of types.ts is a type, so the whole file turns into blank space and its exports are gone. The line import { Fixture } from "./types.ts"; isn’t a type, so it stays and asks for an export that no longer exists: SyntaxError: The requested module './types.ts' does not provide an export named 'Fixture'.
  • Watch out: types disappear, but that doesn’t make an ordinary import of a type harmless. The type goes, and the import that asks for it stays.
  • The checker sees it first. The Riverside project’s tsconfig.json turns on verbatimModuleSyntax. With it, the checker treats each import as Node will run it, exactly as written, so npm run check reports error TS1484: “‘Fixture’ is a type and must be imported using a type-only import when ‘verbatimModuleSyntax’ is enabled.”
  • import type brings in only types. Write import type { Fixture } from "./types.ts";, and stripping removes the whole line along with the types it imported.
  • type inside the braces marks one name. When a module exports both values and types, as results-table.ts exports ResultsTable and TeamRow, write import { type TeamRow, ResultsTable } from "./results-table.ts";. Stripping removes type TeamRow and keeps ResultsTable, which the script uses while it runs.
  • The rule: when you import a name only as a type, mark it with type. Use import type when the whole line is types, and type inside the braces when the line mixes types and values.

The Code

The league’s types, in a module of their own:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 A volunteer has typed the league’s match helpers, all in one long file. At the top sit the league’s Team, Player and Fixture types.

00:10 Other scripts need those types too. The league’s older scripts each copied them by hand, and the copies no longer agree. In one, the age group is a number.

00:21 In this video, you’ll give the types one home, and import them so that Node can still run every file.

00:29 Create types.ts in the Riverside project, and move the three types into it. Put export in front of each one, as you would for a function. Back in the match helpers, write the import as JavaScript taught you, with the name in braces.

00:46 The path ends in .ts because Node runs these files as they are. Why it needs that ending is the next lesson’s question.

00:55 Run the match helpers with Node, and it stops at line one.

01:00 Read the error from the top. It points at Fixture in the import. It’s called a syntax error, but the line is fine JavaScript. The message says types.ts doesn’t provide an export with that name.

01:14 But types.ts exports Fixture, right there. To see why, look at what Node runs.

01:22 Node strips the types from each file before it runs it. In types.ts, every line is a type, so the whole file turns into blank space. Its exports are gone. In the match helpers, the annotations go too.

01:37 But the import line isn’t a type, so it stays. Node strips one file at a time, and this line alone doesn’t say that Fixture is a type. So the line asks types.ts for an export called Fixture that no longer exists.

01:52 Watch out: types disappear, but that doesn’t make an ordinary import of a type harmless. You don’t have to wait for Node to find this. Run npm run check, and the checker reports an error on the same word.

02:09 The message says Fixture is a type, and must be imported using a type-only import when verbatimModuleSyntax is enabled.

02:18 The Riverside project’s tsconfig turns that option on. With it, the checker treats each import as Node will run it, exactly as written. So an import that brings in only types has to say so.

02:33 Add type after import. Now the whole line says it brings in only types. Strip it, and the line vanishes with the types it imported. Nothing is left asking for Fixture.

02:46 The check is clean. Run the file, and Node prints the subject line for Kestrel Park.

02:53 Some modules export both values and types. The results table file now exports its class, and its TeamRow type for the rows. A new league table script needs both.

03:05 Inside the braces, type sits in front of TeamRow only. The class stays a plain import, because the script creates a table from it while it runs. Your turn: pause the video and predict what’s left of this import line after Node strips the types.

03:27 The braces keep ResultsTable, and TeamRow is gone. Node runs the script and prints the Lions’ row.

03:35 Here’s the rule: when you import a name only as a type, mark it with type. Use import type when the whole line is types. Use type inside the braces when the line mixes types and values.

03:50 Now the league’s types live in one place, and the match helpers import them with import type. Next, in Writing Import Paths That Node Can Run, you’ll find out why those import paths need their .ts ending.

Become a Member to join the conversation.