type-czech v1.7.4
TypeCzech
Ditch TypeScript and use TypeCzech runtime type checking instead. Live editable JSFiddles with visible type checking in the list below.
Specify JavaScript parameter signatures and function return types
Type check React components
Does not touch nor affect your code
Verify async/await arguments and resolved values
Toggle TypeCzech on or off programatically
Console.log type errors or throw exceptions
Catch empty parameters like null, NaN, or an empty array
Object interfaces for expected methods and properties
Type check monads
npm i type-czech package with require for Node.js and import for React
No compiling, a single dependency free file
Shortest Possible Live Example
The shortest easiest possible example on a live editable JSFiddle.
type_czech = TypeCzech('LOG-ERRORS') // type checking code
function BEFORE_addXY(x, y) {
return type_czech.checkParam_type([x, y], ['number', 'number'])
}
addXY = type_czech.linkUp(addXY, BEFORE_addXY)
function addXY(x, y) { return x + y } // your code
console.log(" 5 + 5 = ", addXY(5, 5))
linesConsole(2)
console.log(" 6 + 'six' = ", addXY(6, 'six')) // type error
linesConsole(1)
console.log(" 7 + 7 = ", addXY(7, 7))
Base Example
The motivation is to verify function parameters before execution, and function results after completion with PRE_yourRoutine() and POST_yourRoutine() functions. The example below will not log any type errors in Node.js nor the browser as there are no type errors.
Run this live on an editable JSFiddle
React Node.js SSR Barebones Example
This example is live on Render.com as a slow to start, free hosted site that takes 30-180 seconds to start. View the browser console for type errors as below. The barebones source needs three commands to start.
- npm install
- npm run dev
- http://localhost:3000/
Program started with "npm run dev"
Program started with "npm run prod"
When the program is launched in production mode, all type checking stops. The incorrect string type is no longer caught.
A live editable JSFiddle 707 - React JSX example shows how to type check the parameters of a React list component, as well as the properties of the structure returned.
Node.js Async/Await Example
The live Run Time Type Checking Square Root Finder, 150 seconds to start on free hosting, verifies that live aysnc server calls have correct number types. The types checked for are 'Roman Numerals', 'Word Numbers', 'Floats', and 'Integers'. Run locally with the Square Root Finder source.
The server responds with the square root of XVI is in fact IV.
Here we see an error when we try to calculate the square root of 16 as a roman number on the server. Locally we get the "'16' is not a valid roman number", and since the server decides that this is a "400 Bad Request", TypeCzech warns us that the server never returns a valid answer with the "POST_serverGetSqrt E did not return within 3007 milliseconds" error message.
The idea is to simulate a non-returning server crash.
The server responds with the square root of 16 is 4.
A live editable JSFiddle 703 - Async Await shows how to check return types inside an await function.
Live Editable Type-Czech JSFiddle Examples
Contents of 74 live editable examples. Best 'Editor layout' is 'Right results'.
To Install the Package
NPM JavaScript library for Node.js projects.
npm i type-czech
Require for Node.js is Supported, as Well as Import for React
const { TypeCzech_obj } = require("./node_modules/type-czech/type-czech-require.js");
import TypeCzech_obj from "./node_modules/type-czech/type-czech-import.js";
Online Examples
Why Use
Function Expression Error - Uncaught ReferenceError: myFunction is not defined
Lottery Example
Assert Pollution
The Idea
The Recipe
Type Signatures
Empty Strings, Arrays, and Objects
JSFiddle - 74 Page Live Online Editable Tutorial
Local Filesystem - 74 Page Local Editable Tutorial
16 Simple How To Snippets
TypeCzech API
Node.js Only Examples
Browser and Node.js Tests
Recommended Techniques
Production TypeCzech
FAQ
Dependencies
Run Tests
Compile New Versions
Created by
© 2024 Steen Hansen