In this video, you tried to run the league’s older fixture emails script with Node, straight from its TypeScript file, and found out why Node refuses some TypeScript. You saw which syntax makes the compiler write JavaScript of its own, turned on the option that makes the checker report it, and met what to write instead.
Key Points
- Node runs TypeScript in strip-only mode. It removes the types and runs what’s left, and it never adds code of its own. When a file has syntax that needs extra code, Node stops before it runs a single line, with
SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]. - A parameter property can’t be stripped. On a field,
privateis only a check that strips away. In front of a constructor parameter, as inconstructor(private teamName: string) {}, it makes a parameter property. That’s a shortcut that declares a field and sets it from that argument, so the compiler writes the assignment. - An enum, and a namespace with values, make the compiler write code too. An enum becomes a variable and a function that fills an object with its names. A namespace, an older way to group names, does the same when it holds values. You’ll mostly meet these in older code.
- Watch out: an enum is a value as well as a type. It’s an object that exists while the program runs. Stripping it would take that object away and leave a line such as
MatchStatus.Postponedwith nothing to read, so Node refuses the file. erasableSyntaxOnlymakes the checker report them. With"erasableSyntaxOnly": trueintsconfig.json,npm run checkreports each one as error TS1294: “This syntax is not allowed when ‘erasableSyntaxOnly’ is enabled.” The checker reports them all at once, while Node stops at the first. The Riverside project already has it on, sonpm run checkfinds them before you run a file.- A declared field replaces a parameter property. Declare the field in the class body with
privateand its type, and assign it in the constructor. Strip the types, and the annotations andprivateturn into blank space, but the assignment stays, because you wrote it. Syntax that strips away cleanly like this is called erasable. - A literal union does an enum’s job when the code only needs the type. In that case, write a literal union of the strings. If
subject()took the union, a call would pass the string itself, as inemail.subject("postponed"), and the checker would still catch a typo such as"postphoned". - An
as constobject gives you names to use while the program runs. Write a plain objectas const, and give the type of its values the same name, written by hand as the same union for now. Later in this course, you’ll derive that type from the object instead. - The test: if removing the types would leave the program missing code, Node can’t strip it.
The Code
The fixture emails script’s class with a declared field in place of the parameter property. The enum below it is still there:
fixture-emails.ts
class FixtureEmail {
private teamName: string;
constructor(teamName: string) {
this.teamName = teamName;
}
subject(status: MatchStatus) {
return `${this.teamName}: match ${status}`;
}
}
enum MatchStatus {
Scheduled = "scheduled",
Played = "played",
Postponed = "postponed",
}
const email = new FixtureEmail("Riverside Lions");
console.log(email.subject(MatchStatus.Postponed));
With erasableSyntaxOnly on, the checker reports the one piece of syntax that’s left (wrapped for display):
$ npm run check
> [email protected] check
> tsc --noEmit
fixture-emails.ts:13:6 - error TS1294: This syntax is not allowed when
'erasableSyntaxOnly' is enabled.
13 enum MatchStatus {
~~~~~~~~~~~
Found 1 error in fixture-emails.ts:13
Node stops at the same enum, before it runs a single line:
$ node fixture-emails.ts
node:internal/modules/run_main:111
triggerUncaughtException(
^
file:///home/user/fixture-emails/fixture-emails.ts:13
> enum MatchStatus {
Scheduled = "scheduled",
Played = "played",
Postponed = "postponed",
> }
SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is not supported in strip-only mode
...
An enum’s two erasable replacements, a plain object as const for names to use while the program runs and a literal union for the type of its values, with the same name:
status-object.ts
const MatchStatus = {
Scheduled: "scheduled",
Played: "played",
Postponed: "postponed",
} as const;
type MatchStatus = "scheduled" | "played" | "postponed";
console.log(MatchStatus.Postponed);
The check is clean, and Node runs the file:
$ node status-object.ts
postponed
Next, in Exercise: Replace the Match Status Enum, you’ll finish the fixture emails script, so Node runs it straight from its TypeScript file.