I have a problem with TypeScript and Immutable. My code is as follows:
class Repo {
private repository {
items: Map({ "1": { "a": "b" })
}
public get(id: string) {
if (!id) {
throw new Error("ID must be passed. Use `getAll` or `getAllAsMap` to get all items.");
}
const found: Object | undefined = this.repository.items.get(id);
if (typeof found !== "undefined") {
return this.repository.items.get(id);
}
throw new Error(`Entry with ID ${id} cannot be found.`);
}
}
In here I am getting an error saying that this.repository.items.get(id)
may be undefined. But the line before I am checking whether it is not. What can I do, apart from using !
?