The GitHub home page is displayed
Using document


HelloType is an API design simple JS runtime data type check tool, it can help your JS program in the use of a (group) variable before the variable data type check, to prevent the use of variable operations due to different data type error.

Although we already have typescript, it can only do static data type checking at compile time, not dynamic runtime data checking. HelloType is meant to fill this void. It is very simple to use and the API design is very easy to understand and semantic.

Remove API data format annoyance

The front end renders the interface by reading the data returned by the back-end interface, but the problem we often encounter is that the front end trusts the back-end data so much that it reads the data in accordance with the agreed format. However, one night, the monitoring system suddenly alarms, and the big boss in South Africa keeps making mistakes after entering the page. As a self-educated programmer, CMD +opt+I open the developer tool, a look, a pile of red error, like heart bleeding. After an overnight struggle, it turned out that the back end had released a new version that consistently changed numeric values to string numbers in a key interface’s return data. After a bit of MMP in your mind, you now finally have HelloType.

Create a data structure with a certain format using the method provided by hello-type

When you create a specific data structure container using HelloType, compare the container with the received API data to see whether the data format returned by the API conforms to the specified format and whether the data type on the data node is the specified data type. Once the API returns a data format or type that doesn’t meet the requirements, you can trace the error and send it to your monitoring system, and tell the backend students “You changed the data type without notifying the downstream team, do you want pills?”

Check the input and output of the function

TypeScript is an upgrade to the front-end language, but it only checks the data types in a project at compile time, not at runtime. In addition to the FACT that the API data is untestable, the user input data is also untestable, and if you’re using a third-party library, or if you’re writing a third-party library yourself, any code that crosses projects will have untestable data types. You change your project code to learn about TypeScript interfaces and paradigms, and when you’re online, look at the monitoring system, your inner MMP…

Use the ES7 decorator to quickly restrict the data types of the input and output of a method

If you’re super lazy like me and don’t want to write a bunch of TS code for a bunch of interfaces, stereotypes, and types that have nothing to do with the business itself, HelloType is intuitive and completely ES syntax without any local dialects.

Silently track data type errors

The most annoying intrusion into business code is that, for my application, it is always ready to report an error, that is, to tell the user the system is wrong! In this case, HelloType can help you collect data type errors and let your code do a quiet piece of code.

Using a global function, you grab the error information and upload it to the server via an Ajax request

The HelloType to trace method is completely asynchronous, completely asynchronous, does not affect the rendering of the interface, after tracing the data type error, asynchronously executes the function in the with method, this function is custom, so you can do anything in this function, but it’s asynchronous, you can collect logs, And then upload it to the surveillance system. Nice work.

Compatibility mode and strict mode

HelloType checks data in compatibility mode by default. Compatibility mode is strictly for arrays and objects. In compatibility mode, if an object has more attributes than specified, no error will be reported, as long as the specified part of the attribute data type check is correct. In strict mode, the number of elements in an array must be the same as the specified number. IfExists is ignored and must be observed in strict mode. In the case of an object, the main attribute must be the same as specified, neither more nor less, and IfExists is also ignored.

An error is reported because of the height attribute. The strict attribute gets the strict schema state of a type container, which is useful for structurally strict situations

In addition, there is another way to turn on strict mode: toBeStrict(). It directly changes the current container to a strict mode container. Using it when necessary makes the code look lean. Such as:

const SomeType = Dict({ body: Dict({ name: String}).tobestrict (), // When using Dict as a rule, it's better to call toBeStrict() because it's only used once})Copy the code

summary

JS data types themselves can be a nightmare for developers, but HelloType lets you sleep at night. With very reasonable code layout, you can very good interception of bugs due to data type problems. Moreover, because it is based on Error, it is very convenient for debugging and very beneficial for monitoring. By using it, specific Error data can be collected and reported to the server, and the server can correct the format and type of its output data.


Begin to use


If you think the idea is good, give it a star on Github 😊