Loading video player…

Keeping to Erasable Syntax

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, private is only a check that strips away. In front of a constructor parameter, as in constructor(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.Postponed with nothing to read, so Node refuses the file.
  • erasableSyntaxOnly makes the checker report them. With "erasableSyntaxOnly": true in tsconfig.json, npm run check reports 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, so npm run check finds them before you run a file.
  • A declared field replaces a parameter property. Declare the field in the class body with private and its type, and assign it in the constructor. Strip the types, and the annotations and private turn 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 in email.subject("postponed"), and the checker would still catch a typo such as "postphoned".
  • An as const object gives you names to use while the program runs. Write a plain object as 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:

Language: TypeScript Filename: 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):

Language: Shell
$ 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:

Language: Shell
$ 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:

Language: TypeScript Filename: 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:

Language: Shell
$ 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.

00:00 You’ve typed the team roster, and the roster exercise left you with a question about an older script. The league’s fixture emails script sends each team its next match, with a subject line like this.

00:12 Volunteers ran tsc, the TypeScript compiler, which wrote JavaScript into a dist folder. Then Node ran that. A volunteer wants to run it the Riverside project’s way, by pointing node straight at the TypeScript file.

00:27 You’ll see why Node refuses some TypeScript, and what to write instead.

00:34 A FixtureEmail class builds the subject line. Below it, an enum called MatchStatus names each state a match can be in. MatchStatus.Postponed stands for the string postponed.

00:47 Run the file, and Node stops before running a line. The error points at the constructor. On a field, private is only a check that strips away. In front of a parameter, it makes a parameter property.

01:01 It’s a shortcut that declares a field and sets it from that argument. Node doesn’t support that in strip-only mode. Strip-only means Node removes the types and runs what’s left.

01:13 It never adds code of its own.

01:17 Node stops at the first problem, but the checker finds them all. In the script’s tsconfig, set erasableSyntaxOnly to true. Run npm run check, and the checker reports two errors with one message: this syntax isn’t allowed with that option on.

01:36 The first is the parameter property. The second is the enum.

01:42 The Riverside project already has it on, so npm run check finds these before you run a file. To see why Node can’t strip them, look at what tsc wrote into dist.

01:55 The compiler wrote this assignment itself. The enum became a variable, and a function that fills an object with the three names. Watch out: an enum looks like a type, but it’s also a value, an object that exists at run time.

02:12 Stripping it would take that object away, leaving the last line nothing to read, so Node refuses the file.

02:20 A namespace, an older way to group names, makes the compiler write code too when it holds values.

02:28 For the class, declare the field in its body, with private and its type. Then assign it in the constructor, as the results table does. Strip the types, and the annotations and private turn into blank space.

02:42 The assignment stays, because you wrote it. Syntax that strips away cleanly like this is called erasable.

02:49 Run the check again, and one error is left, on the enum. Node stops there too.

02:57 Two erasable pieces do an enum’s job. Try them in a new Riverside file, so the old script keeps its enum. When the code only needs the type, write a literal union of the three strings.

03:09 It’s a type, so it erases to nothing.

03:13 If subject took this union, a call would pass the string itself, and the checker would still catch a typo.

03:21 When you also want names to use while the program runs, write a plain object, as const. As const keeps each value as its literal string. Give it a type with the same name, that union, written by hand for now.

03:36 Later in this course, you’ll derive the type from the object instead. That’s all you need about it for now. Strip it, and as const and the type line vanish.

03:45 Log MatchStatus.Postponed, run the file, and Node prints postponed.

03:53 Here’s the test: if removing the types would leave code missing, Node can’t strip it. An enum fails that test. So does a parameter property, and a namespace with values.

04:06 A literal union, an object as const and a declared field all pass.

04:12 Next, in Exercise: Replace the Match Status Enum, you’ll finish the fixture emails script, so Node runs it straight from its TypeScript file.

Become a Member to join the conversation.