In the upcoming lecture, we will be adding a catch block to our src/routes/cells.ts file. Similar to a lecture earlier in this section, we will need to add a type guard and type predicate.
To resolve, make the following changes:
1) Add an additional interface:
interface LocalApiError {
code: string;
}2) Add a function to return the predicate:
...
router.get("/cells", async (req, res) => {
const isLocalApiError = (err: any): err is LocalApiError => {
return typeof err.code === "string";
};
...3) Update the catch block to use the type guard:
...
} catch (err) {
if (isLocalApiError(err)) {
if (err.code === "ENOENT") {
await fs.writeFile(fullPath, "[]", "utf-8");
res.send([]);
}
} else {
throw err;
}
}
});
...