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
exportkeyword you’d use for a function. Putexportin front of each type intypes.ts, as inexport type Fixture = { ... };, and other modules can import it. - The import paths end in
.tsbecause 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.tsis a type, so the whole file turns into blank space and its exports are gone. The lineimport { 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.jsonturns onverbatimModuleSyntax. With it, the checker treats each import as Node will run it, exactly as written, sonpm run checkreports error TS1484: “‘Fixture’ is a type and must be imported using a type-only import when ‘verbatimModuleSyntax’ is enabled.” import typebrings in only types. Writeimport type { Fixture } from "./types.ts";, and stripping removes the whole line along with the types it imported.typeinside the braces marks one name. When a module exports both values and types, asresults-table.tsexportsResultsTableandTeamRow, writeimport { type TeamRow, ResultsTable } from "./results-table.ts";. Stripping removestype TeamRowand keepsResultsTable, which the script uses while it runs.- The rule: when you import a name only as a type, mark it with
type. Useimport typewhen the whole line is types, andtypeinside the braces when the line mixes types and values.
The Code
The league’s types, in a module of their own: