flems
flems copied to clipboard
Show Typescript compiler errors in console
Would make the Typescript language support a lot nicer.
Could you maybe post an example of some typescript code that should throw an error / warning?
This for example should not pass type checking :-)
interface Foo {
bar: string
}
const n: number = false
const foo:Foo = {bar: n}
console.log(foo)
The simplest error might be to contradict an annotation, which should always be an error regardless of compiler settings:
const i: number = '1'
Here are some other examples that show errors with different compiler settings. (You can flip them on and off with the Options menu.)
I've been looking closer at getting error output, but unfortunately it's not as straight forward as I had hoped. I currently use ts.transpileModule
which bypasses any kind of diagnosis. To get the diagnosis a lot more complex API is required to be used (even though their docs state it is simple 😛).
This speaks for having an API in Flems to do type checking or linting etc, instead of trying to plug it in to the compilation phase.
The example from the typescript playground uses the monaco editor, and I tried to dig into the source of that, but it's heavily tied up inside and felt like finding a needle in a haystack.
I've found this documentation and this to be the best place to start for now.
It requires creating a program
and then supplying that with a CompilerHost
to simulate the file system.
Either digging in to that or participating in this issue to get the option in ts.transpileModule
might be the way to go..
Ah, it's too bad it's not a trivial undertaking. If I had the time I'd love to look into it but I really don't right now. There may be other TS users who would though, if you advertise it a bit. Just being able to run TS code is a pretty nice feature already.
Hmm.. couldn't help myself.. I managed to get a bit further.. This definitely needs to be put in a worker, cause it seems quite heavy, but I think that might be possible to tweak by removing checks for errors not needed..
That's awesome!
The Cannot find name 'console'
error should be fixed by adding compilerOptions.lib: ["dom"]
. Doesn't look like that's an option for createProgram
... hopefully those can be set somewhere.
The Cannot redeclare block-scoped variable 'n'.
(and 'foo'
) don't make sense to me. They are only defined once...
Edit:
That's awesome!
Yeah, doubling down on that :-)
Thanks for the pointers :) I'll play around with it a bit more next week.. Also looked a bit into fetching type declarations with @spacejack on gitter, so I'll just leave this flems here because someone didn't implement saving flemses yet...
I think this might be solved better by using the monaco editor and use the typescript checker it includes. I haven't found anything for CodeMirror unfortunately, there's only the js linter, but nothing typescript specific. What do you think @spacejack & @pygy ?
I'm afraid I don't have any useful feedback...