ts-validate-type icon indicating copy to clipboard operation
ts-validate-type copied to clipboard

Simple Desgin

Open brillout opened this issue 5 years ago • 5 comments

Hi @edbentley !

I'm curious; what do you think of 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));

brillout avatar May 03 '20 11:05 brillout

Hi @brillout, thanks for the suggestion.

I’m curious, what does this do that this package is lacking? Is it that you’re able to reference an existing type?

edbentley avatar May 04 '20 08:05 edbentley

Okay seems like I wrongly understood what validateType does. My mistake, sorry.

With validateType my example would look like this:

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

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

const data: any = await resp.json();

let todo: Todo;
try {
  todo = validateType<Todo>(data);
} catch(err) {
   assert(false, "wrong JSON data");
}

Correct?

brillout avatar May 21 '20 11:05 brillout

@brillout Right your example looks exactly as I would like to have it too, very nice! The problem I've had is filling in the type of Todo requires some sort of lookup in the TypeScript compiler.

Converting it to a runtime representation like const __TodoType you have above could work but there's lots of cases it won't - for example if you import a type?

edbentley avatar May 26 '20 14:05 edbentley

Lovely :).

Converting it to a runtime representation like const __TodoType you have above could work but there's lots of cases it won't - for example if you import a type?

I'm new to TypeScript and I know virtually nothing about how Babel transpiles. I was assuming that a Babel transformer would be able to know where a TypeScript type is being used? So that types that are used in runtime can be translated to runtime code together with import/exports to use this "runtime type check code" across files.

Would be so nice to be able to re-use TypeScript types to do runtime type checking in such an easy way!

brillout avatar May 26 '20 18:05 brillout

As far as I’m aware (please correct me if I’m wrong), the Babel transformer knows the name of the type used, but it can’t look up its value. I’d love this feature too but not sure how it could be done at the library level!

edbentley avatar Jun 02 '20 17:06 edbentley