Guarding With Assertion Functions

In the last lesson, isSighting() told the checker what a true result means, so an entry from the export became a Sighting inside the branch taken when it returned true. When an entry failed, the import printed Rejected: not a sighting and moved on to the next one. That’s the right call for one bad sighting among many.

Some data leaves the import nothing to move on to. If the club’s settings are wrong, there’s no export file to read and no rule for which counts to keep. In this lesson, you’ll write assertSettings(), a function that stops the import with a clear error for invalid settings and tells the checker what they are when they’re right. Start with a function that only throws, and see what the checker makes of it.

Throw When the Settings Are Wrong

You need a check that stops the import, because the script would import every sighting incorrectly under broken settings. In the lesson on satisfies, you wrote the settings in the code, where the checker could see them. The club keeps them in a settings.json file instead, so a member can change the export’s name without touching the script. The import reads that file before anything else, and its contents come from outside the program.

You could call a predicate and throw when it returns false. That single false doesn’t say which check failed, though, so an error thrown from it couldn’t name the property to fix. A function that checks each property in turn can throw as soon as one fails, with a message that names it.

Create read-settings.ts in your millbrook-birders folder with that function. The ImportSettings type includes two of the settings’ three properties, because checking every item of an array, which sites would need, comes in Checking Data Once at the Boundary. To keep this lesson to one file, you store the settings file’s text in a string, as you did with the export entries in the last lesson:

Language: TypeScript Filename: read-settings.ts
 1type ImportSettings = {
 2  exportFile: string;
 3  minCount: number;
 4};
 5
 6function checkSettings(value: unknown) {
 7  if (typeof value !== "object" || value === null) {
 8    throw new Error("settings.json must hold an object");
 9  }
10  if (!("exportFile" in value) || typeof value.exportFile !== "string") {
11    throw new Error("settings.json: exportFile must be a string");
12  }
13  if (!("minCount" in value) || typeof value.minCount !== "number") {
14    throw new Error("settings.json: minCount must be a number");
15  }
16}
17
18const text = '{"exportFile":"sightings-2026.json","minCount":"2"}';
19const settings: unknown = JSON.parse(text);
20checkSettings(settings);
21console.log(`Importing ${settings.exportFile}`);
22console.log(`Skipping counts below ${settings.minCount}`);

Each if in checkSettings() reverses one of the checks you met in isSighting(). When a check fails, the function throws an Error that explains the problem. The text on line 18 has a slip that someone editing the file by hand could make. The quotes around "2" make minCount a string. Check the project:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

read-settings.ts:21:26 - error TS18046: 'settings' is of type 'unknown'.

21 console.log(`Importing ${settings.exportFile}`);
                            ~~~~~~~~

read-settings.ts:22:38 - error TS18046: 'settings' is of type 'unknown'.

22 console.log(`Skipping counts below ${settings.minCount}`);
                                        ~~~~~~~~

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

Become a Member to join the conversation.