Checking a Value With satisfies

In the last lesson, an assertion told the checker that the species API’s reply was a SpeciesResponse, and the checker took its word for it. Not every value in the import script comes from outside, though. You write the club’s import settings, which name the export file to read and the sites to accept, by hand in the code, so the checker can see every value in them.

In this lesson, you’ll put a type on those settings in three ways and compare what tsc says about each. You’ll come away able to use satisfies to check a value against a type while keeping the value’s own, more precise type. Start with an annotation, the way you’d probably write it first.

Annotate the Settings

The settings need a type so that a slip in them shows up in the check instead of on the year list. If overlap.ts from the last lesson is still in your millbrook-birders folder, delete it first, because npm run check checks every .ts file and would report its error throughout this lesson.

Then create settings.ts with an ImportSettings annotation:

Language: TypeScript Filename: settings.ts
 1type Site = "Millbrook Reservoir" | "Gull Point";
 2
 3type ImportSettings = {
 4  exportFile: string;
 5  sites: Site[] | "all";
 6  minCount: number;
 7};
 8
 9const settings: ImportSettings = {
10  exportFile: "sightings-2026.json",
11  sites: ["Millbrook Reservoir", "Gull Pont"],
12  minCount: 1,
13};
14
15const siteList = settings.sites.join(" and ");
16console.log(`Importing ${settings.exportFile} from ${siteList}`);

Site is a literal union of the club’s two sites. The sites property holds either a list of sites or "all", for a year when the club accepts sightings from anywhere. On line 11, "Gull Pont" is a slip anyone typing in a hurry could make. Check the project:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

settings.ts:11:3 - error TS2322: Type '("Gull Pont" | "Millbrook Reservoir")[]'
is not assignable to type '"all" | Site[]'.
  Type '("Gull Pont" | "Millbrook Reservoir")[]' is not assignable to type
  'Site[]'.
    Type '"Gull Pont" | "Millbrook Reservoir"' is not assignable to type 'Site'.
      Type '"Gull Pont"' is not assignable to type 'Site'.
      Did you mean '"Gull Point"'?

11   sites: ["Millbrook Reservoir", "Gull Pont"],
     ~~~~~

  settings.ts:5:3 - The expected type comes from property 'sites' which is
  declared here on type 'ImportSettings'
    5   sites: Site[] | "all";
        ~~~~~

settings.ts:15:33 - error TS2339: Property 'join' does not exist on type
'"all" | Site[]'.
  Property 'join' does not exist on type '"all"'.

15 const siteList = settings.sites.join(" and ");
                                   ~~~~

Read the first error from the top. The array on line 11 contains "Gull Pont", which isn’t a Site, and tsc even suggests "Gull Point". That’s the annotation doing its job.

The second error is the surprise. You can see that sites is a list, so why doesn’t it have .join()? An annotation gives the variable the declared type, so from then on settings.sites is "all" | Site[], whatever you wrote. To call .join(), you’d have to narrow it with a check that you already know passes.

Assert the Settings With as

You’ll also find settings typed with as, the way the old import script typed its data. Move the type to the end of the object as an assertion, on lines 9 and 13:

Language: TypeScript Filename: settings.ts
 1type Site = "Millbrook Reservoir" | "Gull Point";
 2
 3type ImportSettings = {
 4  exportFile: string;
 5  sites: Site[] | "all";
 6  minCount: number;
 7};
 8
 9const settings = {
10  exportFile: "sightings-2026.json",
11  sites: ["Millbrook Reservoir", "Gull Pont"],
12  minCount: 1,
13} as ImportSettings;
14
15const siteList = settings.sites.join(" and ");
16console.log(`Importing ${settings.exportFile} from ${siteList}`);

Run npm run check again, and only one error is left:

Language: Shell
settings.ts:15:33 - error TS2339: Property 'join' does not exist on type
'"all" | Site[]'.
  Property 'join' does not exist on type '"all"'.

15 const siteList = settings.sites.join(" and ");
                                   ~~~~

The error for the typo is gone, and the .join() error is the same as before. The assertion hid one problem and fixed nothing. The last lesson’s overlap rule has one more part, for unions. The checker counts two types as overlapping when they share a member, even if neither fits the other. Neither array type fits the other here, but both arrays can hold "Millbrook Reservoir", so the assertion passes.

Node.js removes the types before it runs the file, so the remaining type error doesn’t stop the run either:

Language: Shell
$ node settings.ts
Importing sightings-2026.json from Millbrook Reservoir and Gull Pont

The typo remains in the settings, which would make the import reject every sighting from Gull Point.

Check the Settings With satisfies

You want to catch the typo, as the annotation did, while keeping sites as the list you wrote. The satisfies operator does both: value satisfies Type checks the value against the type, as an annotation does, but the variable keeps the type the checker infers for the value.

Change as to satisfies on line 13 and check again. The error for the typo comes back, word for word as with the annotation, and the .join() error doesn’t:

Language: Shell
settings.ts:11:3 - error TS2322: Type '("Gull Pont" | "Millbrook Reservoir")[]'
is not assignable to type '"all" | Site[]'.
...
      Type '"Gull Pont"' is not assignable to type 'Site'.
      Did you mean '"Gull Point"'?

Now fix the typo on line 11:

Language: TypeScript Filename: settings.ts
 1type Site = "Millbrook Reservoir" | "Gull Point";
 2
 3type ImportSettings = {
 4  exportFile: string;
 5  sites: Site[] | "all";
 6  minCount: number;
 7};
 8
 9const settings = {
10  exportFile: "sightings-2026.json",
11  sites: ["Millbrook Reservoir", "Gull Point"],
12  minCount: 1,
13} satisfies ImportSettings;
14
15const siteList = settings.sites.join(" and ");
16console.log(`Importing ${settings.exportFile} from ${siteList}`);

This time, npm run check passes with no errors, and the run prints the right sites:

Language: Shell
$ node settings.ts
Importing sightings-2026.json from Millbrook Reservoir and Gull Point

The checker matched the object against ImportSettings, then kept the type it inferred: settings.sites is ("Millbrook Reservoir" | "Gull Point")[], with no "all" in it, so .join() passes. ImportSettings also guided that inference. On its own, the array would be string[], but with Site to aim at, the checker kept the literal types. Like as, satisfies disappears when Node.js strips the types, so it has no effect on the values while the script runs.

So you have three ways to put a type on a value. An annotation checks the value and gives the variable the declared type. An as assertion checks only for overlap. satisfies checks the value and keeps its precise type, which suits values you write yourself, such as settings. If you’re unsure which one fits a value in your own code, ask the mentor:

Check Your Understanding

Try these before you open the answers. The first two use settings.ts from this lesson.

  1. With as ImportSettings, why did npm run check miss "Gull Pont"?
  2. With the annotation and no typo, why does settings.sites.join(" and ") still fail the check?
  3. A teammate writes const reply = (await response.json()) satisfies SpeciesResponse; to check the species API’s reply. Would that check the reply when the script runs?
  1. An assertion only asks whether the types overlap. An array that can hold "Millbrook Reservoir" overlaps an array of sites, so the checker accepts the claim and never reports "Gull Pont".
  2. The annotation gives settings the declared type, so settings.sites is "all" | Site[], and a string such as "all" has no .join() method.
  3. No. satisfies is removed with the other types before the script runs, so it checks only what the checker can see in the code. It doesn’t help at check time either, because .json() returns any, which passes as every type, and reply stays any. The reply needs a check in your own code, as in the last lesson’s lookup.ts.

In the next lesson, Exercise: Replace the Hopeful Assertions, you’ll make pieces of the club’s import trustworthy, so that a changed export can’t put bad data on the year list.

It’s the course’s first coding exercise, and it runs in your browser instead of your millbrook-birders folder. You edit the code there and click Run Tests, and automated tests tell you what passed and what they expected. Full screen gives you more room, and the exercise’s Contents tab explains the rest.

Become a Member to join the conversation.