Hegel: aspire to be the best JavaScript static type checker

Yunqi information:[ Click to see more industry information]
Here you can find the first-hand cloud information of different industries. What are you waiting for? Come on!

As a rookie in JavaScript type checker, Hegel aspires to be the best JavaScript static type checker. It claims to provide a reliable type system with strong type inference. At present, Hegel is still in the alpha test stage, and you can have a functional experience in its dedicated online practice field.

Hegel is an optional JavaScript type checker for type annotation. At the same time, like TypeScript, users do not need to relearn a new language structure, only need to master the annotation syntax. Hegel hopes that through a strong and stable type system, it can avoid exceptions caused by type errors when the program is running. The following code shows its powerful type checking capability:

// Define numbers as array < number >
const numbers: Array<number> = [];
// Assign the numbers variable to numbersOrStrings, but of type array < string| number >
// Hegel checks for type errors:
// HegelError: incompatible type "array < number >" and type "array < number| string >"
const numbersOrStrings: Array<string | number> = numbers;
// Assign a value to the second element of numbersOrStrings
numbersOrStrings[1] = "Hello, TypeError!";
// The following code, Hegel, checks for type exceptions:
// HegelError: property 'toFixed' does not exist in 'Number | undefined'
numbers[1].toFixed(1);

The above code uses TypeScript (v3.8.3) to compile without any errors, but it does throw an exception at actual run time.

Translator's note:
When JS is running, numbers and numbersOrStrings are both reference types. When they assign values to each other, they belong to reference assignment. Therefore, when numbersOrStrings modifies the element content, it has the same effect on numbers. So when numbers[1].toFixed(1) is executed, an error will be reported, because there is no toFixed function in the string. The above code shows Hegel's reliable type system.

In addition to a reliable type system, robust type inference is also the main design goal of Hegel. The sample code is as follows:

// Hegel would infer that "promise" is "promise"<_ q, _ c>((_ c) => _ q) => (_ c) => Promise<_ Q> "
const  promisify = fn => arg => Promise.resolve(fn(arg));
// Here, Hegel would deduce that<_ c>(_ c) => Promise<_ C> " 
const id =  promisify(x  => x)
// Similarly, "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);  

When running the same code in TypeScript (test version: 3.7.5), TS will recognize three exceptions and infer the variable result as Promise type. Therefore, robust type inference allows developers to write as few code comments as possible, which is more conducive to code readability.
Hegel also included the exception in the type check. The example code is as follows:

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)  {
  // The "error" variable here is inferred as "ReferenceError ︱ TypeError ︱ unknown"
}

The disadvantage of Hegel is that casts and any types are not supported, for example:

// Error: no any type in Hegel
const something:  any  =  null;
// Error: type conversion is not supported in Hegel
(null:  any).call();

Its comparison with the mainstream type checker (TypeScript and Flow) is illustrated in the Hegel documentation. In addition to supporting standard types (base types, functions, objects, classes, arrays), Hegel's type system also supports unknown types (such as JSON.parse() returned type), optional type, union type, primitive type, type alias, generic type, and mutable type. This is also one of the features of Hegel.
Mutable types can help us extract or create new types from existing types. As a result, mutable types can be understood as generating functions of one type from another. In Hegel, 17 variable types are defined.
Here is an example code for the variable type $Exclude (semantically similar to the Exclude type in TypeScript):

// Define type
type  Status  =  "Ok"  |  "Failed"  |  "Pending"  |  "Canceled";
// After variable type
// IntermediateStatuses = "Canceled" | "Panding"
type  IntermediateStatuses  = $Exclude<Status,  "Ok"  |  "Failed">;
// assignment
const intermediateStatuses:  Array<$Exclude<Status,  "Ok"  |  "Failed">>  =  ["Pending",  "Canceled"];
// Hegel type check exception:
// Error: incompatible type '['Failed'] 'and type'... Array <'cancelled '|'pending' ''
intermediateStatuses.push("Failed");

Devon Govett, the author of parceljs, said on Twitter:
Hegel looks interesting, it's closer to Flow than TS, and it's implemented using JS. Focus on type inference and robust types, only JS and types (no extra functionality). Support. d.ts, vscode integration, etc.
Published on npm, Hegel provides a command-line tool and an interactive online experience area. At the same time, it provides corresponding installation commands in GitHub warehouse node.js The minimum version is 12.
Hegel is based on MIT protocol. Welcome to feedback and contribution on GitHub of this project. In addition, the author of Hegel states:
Hegel comes from the community and contributes to the community; therefore, any PRs and issues you have will not be ignored and forgotten.

[yunqi online class] product technology experts share every day!
Course address: https://yqh.aliyun.com/zhibo

Join the community immediately, face to face with experts, and keep abreast of the latest news of the course!
[yunqi online classroom community] https://c.tb.cn/F3.Z8gvnK

Original release time: May 29, 2020
Author: Bruno Couriol
This article comes from:“ InfoQ ”, you can pay attention to“ InfoQ"

Keywords: Javascript TypeScript github less

Added by drakal30 on Fri, 29 May 2020 09:31:34 +0300