Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript & runtime type-checking, simple solution in 2020 [closed]

As we all know, TypeScript type-checks only at compile-time.

There are couple of existing approaches to add runtime checks, such as io-ts, but I'm left to wonder if there is a simpler way.

For example a Babel plugin that would transpile this:

type Todo = {
  userId: number;
  id: number;
  title: string;
  completed: boolean;
}

const resp = await fetch("https://jsonplaceholder.typicode.com/todos/1");

const data = await resp.json();

assert(data typeof Todo);

to that:

const __TodoType = {
  userId: Number;
  id: Number;
  title: String;
  completed: Boolean;
};
const __isTodoType = obj => (
  obj &&
  obj.constructor === Object &&
  Object.keys(obj).length === Object.keys(__TodoType).length &&
  Object.entries(obj)
    .every(([prop, val]) =>
      __TodoType[prop] && val &&
      __TodoType[prop] === val.constructor)
);

const resp = await fetch("https://jsonplaceholder.typicode.com/todos/1");

const data = await resp.json();

assert(__isTodoType(data));

This would be a super simple solution and would cover many (if not most) uses cases. AFAICT, this would be enough for asserting serialization / data fetching.

Has someone managed to build such Babel plugin?

Edit - I know the existing libraries such as io-ts but I'm looking for something much simpler. The Babel Plugin I'm showcasing is vastly simpler (from the perspective of the plugin user) than anything else I've seen so far. I'm not sure why this hasn't been done before.

like image 325
brillout Avatar asked May 03 '20 11:05

brillout


People also ask

Is TypeScript better than JavaScript?

TypeScript vs JavaScript: HighlightsJavaScript is better suited for small-scale applications, while TypeScript is better for larger applications. TypeScript supports static typing but JavaScript does not. TypeScript supports interfaces but JavaScript does not. TypeScript features prototyping but JavaScript does not.

Is TypeScript better than Python?

In terms of raw performance, Typescript is much faster than Python. When coding memory-intensive tasks in Python, e.g games, that utilize high-end 3D graphics, the CPU begins to take a hit and there is a significant drop in performance. Unlike Typescript, Python is not asynchronous at its core.

Should I learn JavaScript or TypeScript?

We frequently see the question “Should I learn JavaScript or TypeScript? “. The answer is that you can't learn TypeScript without learning JavaScript! TypeScript shares syntax and runtime behavior with JavaScript, so anything you learn about JavaScript is helping you learn TypeScript at the same time.

Is TypeScript frontend or backend?

Is TypeScript Frontend or Backend? TypeScript is neither a frontend or backend language, but rather a superset of the already established and well-known software language, JavaScript.


1 Answers

I was frustrated by this as well and ended up writing my own type guard generator called ‛ts-type-checked‛ available as an NPM module. It is a TypeScript transform that generates type guards based on your types.

like image 196
Ján Jakub Naništa Avatar answered Nov 08 '22 08:11

Ján Jakub Naništa