kaleidawave / ezno

A JavaScript compiler and TypeScript checker written in Rust with a focus on static analysis and runtime performance
https://kaleidawave.github.io/posts/introducing-ezno/
MIT License
2.3k stars 42 forks source link

Simple example of typechecking from code? #162

Closed jloganolson closed 3 weeks ago

jloganolson commented 3 weeks ago

Do you have an example of how I could run type checking on a string or file of typescript from within code?

I tried

import {check} from 'ezno/initialised'
const fs_handler = (_path) => "const x = !t ? 4 : 5;";
console.dir(check("input.js", fs_handler), { depth: 5 })

It ran, but didn't seem to produce the desired output. I've been looking for awhile so figure i'd just ask.

kaleidawave commented 3 weeks ago

Yep you should be able to use the JS (via WASM) version of the checker with

import { check } from 'ezno/initialised';
const fs_handler = (_path) => "const x: string = t ? 4 : 5;";
const output = check("input.ts", fs_handler);
console.dir(output.diagnostics, { depth: 5 });

Since the addition of the playground #115, check also returns a lookup table for getting the type name of expressions at a position, thus .diagnostics is needed.

kaleidawave commented 3 weeks ago

Ah yes I see there is outdated documentation (and check is missing) of this. Will update

https://github.com/kaleidawave/ezno/blob/aafc9a4bfefe72f43de89976ff56542711759081/src/js-cli-and-library/README.md?plain=1#L15-L37

kaleidawave commented 3 weeks ago

I also wonder if there a way to make the console.log more informative...?

image

jloganolson commented 3 weeks ago

Yeah, is there something i can do to that WASMCheckOutput to actually see/log typecheck errors (or that it passes)? Or is that the '.diagnostics' in the example code above?

kaleidawave commented 2 weeks ago

Yep .diagnostics contains type checker errors with reasons and there byte origin position in the source.

image

It also contains information and warnings. So you might want to filter on .kind.

Have updated in the documentation in #155