PR #998 provides a REPL for Deno, but only supports plain JavaScript. It needs to support TypeScript as well.
I was thinking about the approach and am writing up a couple ideas. ts-node provides a REPL and in looking at how they accomplish it seems like a reasonably logical approach to attempt with Deno. In order to support that, I think we need to do the following:
ts
incrementalCompile(sourceCode: SourceCode, previousOutput?: OutputCode): { diagnostics: ts.Diagnostic[], outputCode: OutputCode, additionalCode: OutputCode);
compiler.configure() in #1136 will likely be need as well to allow the REPL to change the configuration, which it will need to do. We might have to modify it so it actually allows some changes that we wouldn't normally allow.The reason it needs to work this way, is that in some cases, the TypeScript will output something higher up (like when it emits helpers) and those need to be evaled.
Things like imports and async are going to be complex, I would recommend we aim for an MVP and then figure out how to go from there.
cc: @bartlomieju
Thanks for opening the issue @kitsonk. Compiler extensions were my questions to you. I'll take a look at ts-node and start hacking around.
EDIT: typo
Just a couple of points on this based on some recent conversations:
No idea about the practicality of this suggestion, but could you have the best of both worlds by having the typescript support enabled through a flag? In other words, one REPL (e.g. loaded via deno) for javascript and another for Typescript (e.g. loaded via deno -ts).
Yeah, feels like we will need some sort of flag, because it is a bit heavyweight to have TS always on in the REPL. Technically, it shouldn't be too hard.
Striping types should be easy enough - i kinda think v8 should be able to do it... I wonder if they would accept a patch to add an API for that...
So, Babel does that. The TS team worked with Babel to ensure the lexer could "ignore" TS. So the logic is there and I suspect documented in some fashion. It is more than just types though, as there are some TypeScript syntax which has a functional emit and isn't erasable (which they avoid like the plague now), specifically enum and const enum come to mind. (There are also some legacy things like import foo = require("foo"); too, which is valid TS but not anywhere close to valid JS).
It would be useful to be able to experiment in the repl and see type errors, supporting that would need more than just stripping types though.
I just left this comment https://github.com/denoland/deno/pull/3760#issuecomment-590472390
There are many things we can do to improve the REPL without introducing the TS compiler. We should do those things first (e.g. improve inspect, tab completion). Once we're on par with Node's REPL, we can start looking into how to go beyond that by using typescript.
Now that we have fairly frictionless access to swc and there are other reasons we might want to use swc in REPL to parse JavaScript, we could support TypeScript without type checking in the REPL.
Thoughts @caspervonb?
Preferably we would also add better completion if we're integrating TypeScript support but we can just do stripping as a first pass, I'll get back to this once I'm done with code coverage.
Most helpful comment
It would be useful to be able to experiment in the repl and see type errors, supporting that would need more than just stripping types though.