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<ReferenceError> => {
if (typeof obj === "object" && obj !== null && "value" in obj) {
// Current function throws "TypeError" type which is incompatible...
assertNumber(obj.value);
}
};
  • 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<number> = [];
    // Error: Type "Array<number>" is incompatible with type "Array<number | string>"
    const numbersOrStrings: Array<string | number> = 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<string>"
    const upperStr = id("It will be inferred").then(str => str.toUpperCase());
    // Finally, "twiceNum" will be inferred as "Promise<number>"
    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"
    }