Execute typescript directly

I'd be very surprised if this has never been floated before, so I'm slightly trying to invoke Cunningham's law by presenting this as an "idea":

What if ecmascript could run typescript directly?

This would simplify a huge number of build setups. Type checking could still be done by tsc/the typescript library, but for runtimes that supported it, there'd be no need for: source maps or call stack modification, complex dependency tracking in monorepos just to be able to build things, declaration maps, worrying about destination directories differing from source at runtime, etc. etc.

Similarly to how babel is now able to parse typescript without validating the types, could the js engine be taught to parse the type annotations and just throw them out, then run the code without them? tsc would effectively become a lint task.

I'm not really expecting anyone to say "cool, good idea, we'll do that next week" but I'd be interested to find out if it's already been considered. It seems like it's not that much of a stretch beyond all the es modules syntax, as long as the actual type checking is out of scope. The annotations could be thought of in the same way as comments. The syntax is valid/interpretable, but has no effect at runtime.

ecmascript doesn't run anything, it's just the specification for the language. It'd be up to individual implementations to do this - such as deno.

if you're suggesting standardizing typescript - at all, let alone in ecma-262 - I think that's a much less likely and much more difficult ask.

2 Likes

Yeah, I guess I phrased it wrong. I'm curious if there's any interest on this side. If, hypothetically, typescript had a full formal spec, would this team be open to incorporating a subset of it? Specifically, to make things easier, the subset of just the syntax - type annotations, generics, type/infer/extends/keyof/as/is/asserts keywords etc. without any of the type checking requirements.

I've just looked at the latest typescript spec and it's apparently archived. The hypothetical still stands though - and maybe it could be a motivator for the ts team to unarchive and start maintaining the spec, since that'd be a prerequisite.

An existing, officially supported, way for projects to benefit from TypeScript without requiring a build step is to write JavaScript and add the type-information using a mix of '.d.ts' files and the JSDoc syntax that TS supports

I've used this on a few (small) projects and really liked the experience.

1 Like

This stage 0 proposal might be of interest. It is pretty close to what you're describing: an approved syntax for type annotations, treated like comments by the engine.

3 Likes

I see this introduces interface as a declaration syntax, enabling parsing and ignoring it. How would this work for structures with a more complicated transpilation story like enum, which have runtime representations and lead to the creation of values? What about other "pluggable type systems" that require more of these, e.g. a trait {}?

Might be better to just file an issue against that repo asking that.

1 Like

@iain perfect - I hadn't seen that proposal, I think that covers this request well enough. Thanks!

You may want to know about deno or ts-node, but integrate that into the language will be very hard