https://hegel.js.org/ * Docs * Try * GitHub Hegel Logo An advanced static type checker const assertNumber = arg => { if (typeof arg !== "number") { throw new TypeError("Given argument is not a number"); } } const assertExisting = (obj): $Throws => { if (typeof obj === "object" && obj !== null && "value" in obj) { // Current function throws "TypeError" type which is incompatible... assertNumber(obj.value); } }; Get StartedTry Online * No Runtime TypeErrors Hegel attempts to prevent runtime TypeErrors with a strong type system, great type inference and notifying you about corner cases. * Easily Integrated Hegel is only JavaScript with types, so you don't need to use specific file extensions or comments to start working with it. * Community-friendly Hegel is developed by community for community. So, your PRs and issues will not be ignored or skipped. * Soundness const numbers: Array = []; // Error: Type "Array" is incompatible with type "Array" const numbersOrStrings: Array = numbers; numbersOrStrings.push("Hello, TypeError!"); // Error: Property "toFixed" does not exist in "Number | undefined" numbers[1].toFixed(1); * Strong Type System function assertNumber(num: ?number) { // Error: Type "number | undefined" is incompatible with type "boolean" if (!num) { // Oops you lost "0" throw new TypeError("Number was not provided"); } return num; } assertNumber(0); * Type Inference // Hegel will infer "promisify" as "<_q, _c>((_c) => _q) => (_c) => Promise<_q>" const promisify = fn => arg => Promise.resolve(fn(arg)); // There, Hegel will infer "<_c>(_c) => Promise<_c>" const id = promisify(x => x); // And "upperStr" will be inferred as "Promise" const upperStr = id("It will be inferred").then(str => str.toUpperCase()); // Finally, "twiceNum" will be inferred as "Promise" const twicedNum = id(42).then(num => num ** 2); * Typed Errors function assert(age) { if (typeof age !== "number") { throw new TypeError("Age is not number."); } if (age <= 0) { throw new ReferenceError("Age can't be less or equals zero."); } } try { assert(0); } catch(error) { // So, as a result, "error" variable type will be "ReferenceError | TypeError | unknown" } * Distributed under MIT License (c) Artem Kobzar 2019 - 2021