Idea: Deno compile-time functions CTFE #7359
Labels
cli
related to cli/ dir
declined
thank you, but respectfully declined
suggestion
suggestions for new features (yet to be agreed)
CTFE = Compile time function execution
https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe
In Rust, they are called Constant Expressions:
https://doc.rust-lang.org/reference/const_eval.html
I think what I'm fishing for here is a way for Deno to provide a [pre] TS compile-time hook (or on hot reload): i.e. a special app entry-point will get called prior to the application starting or reloading. For example, our app may look like:
data.json
index.ts:
I think this might be valuable to the Deno ecosystem as it reduces the need for a basic build system for many common cases, which are mainly frontrunning data loading or data generation. I'd imagine this would would by calling the TS compiler twice to first run the prebuild/CTFE hook, stash the resulting data somehow, and reuse it for the regular TS project compile.
Now what would be REALLY interesting is if Deno could statically inject that data into the second phase of the regular TypeScript compilation. If TypeScript can be aware of the raw values used in the first compile, you can use the upcoming TS 4.1 template literal types (https://twitter.com/danvdk/status/1301707026507198464) to build types based on the static data generated in the first prebuild step!
The end result is you could write a TS project fetch a json file, generate a string to represent it's structure, and then use 4.1's template literal to have strict type generation of that json file available for regular type validation use, but that type is being generated via a previous compile-time/CTFE hook. Deno could allow TypeScript to do something that it doesn't wish to do for itself as a tool (microsoft/TypeScript#26534 and microsoft/TypeScript#15761)... this may fill in a missing piece of the toolchain that would be a treasured expansion of language's overall capabilities.
If you're still not sold, see what SQLx does for Rust using Const Evalutions (CTFE). It's able to both query strings at compile time -and- generate strict types for query results! https://github.com/launchbadge/sqlx
The text was updated successfully, but these errors were encountered: