Because TypeScript is a supertset of JavaScript, it supports all built-in JavaScript functions, types, objects, keywords and so on. The one you are looking for is the throw
keyword, that will raise the desired exception.
Your code so far was good, so the following will do the job.
export class TriStateCheckboxManager {
constructor(public checkBox: HTMLInputElement) {
if (checkBox.type !== "checkbox") {
// checkBox doesn't meet the reqruirements,
// so raise an error. Optionally, you could
// log a message, warning or whatever you want
// to the console.
console.warn("checkBox.type doesn't match.", checkBox, this);
throw "checkBox.type doesn't match." // Throw anything you want here
}
}
}
BTW: It's strongly recommended to use !==
and ===
for comparisons in JavaScript (thus TypeScript) instead of !=
and ==
. See here for more.
EDIT:
As MiMo said below, you can throw whatever type you want, so an object would be a suitable canditate too.
I found that this article looks promising if you're into JavaScript / TypeScript error handling. Here's the MDN page for throwing errors in JavaScript.