Repository navigation
Description
Note 1: the original title of this issue was "Remove type-checking from the critical path of Angular builds (TypeScript and Template compilation)", but through community discussion we found "Out-of-band type-checking for TypeScript and Template compilation" more fitting).
Note 2: This proposal has its origin in RFC: Exploration of use-cases for Angular JIT compilation mode and aims to address the concerns around AOT builds being too slow compared to the JIT alternative.
Note 3: 📺 Check out the video recording of a community discussion about this proposal @ angularnation.net
The problem
Currently, precious hundreds of milliseconds, but often more commonly many seconds, are being spent on type-checking code of Angular applications as developers edit the code using interactive workflows (like ng serve, ng test --watch, etc). Not only does this increase the latency, and decrease the number of iterations per minute, it also makes it impossible to see results of a change in the browser if the change doesn't pass the type-checking step. This makes it hard to experiment, and explore, which impacts creativity, and forces developers to either iterate less, or take the time to make the compiler happy, just to find out that they are heading down the wrong direction.
But it doesn't have to be this way... we can have both a faster iteration cycle, as well as all the benefits of the type system and type checking.
Background info
Developer productivity, velocity, and happiness (no, really!), as well as higher quality of code, and better UX of the final product are directly correlated with how quickly developers can iterate on the code — make a code change and see the result locally.
The lower the edit & refresh latency (the faster the feedback loop) the better — this is especially relevant for interactive workflows (local dev server, unit tests, etc).
Lower latency means that developers can afford more iterations on solving a problem in a given period of time, thus enabling them to try various approaches, be more creative, and to pick the best solution. If the iteration latency is low, developers often end up shipping code that (barely) meets the functional criteria, and due to a lack of time can't focus on UX or code base quality.
side note: Latency of committing code and deploying it to production also plays a big role in overall success, but this proposal is specifically focused on the interactive feedback loop.
Angular has always tried to strike the balance between flexibility that enables creativity and providing guide rails for developers to support them on the path of creating correct, performant, maintainable, accessible, localizable code. One of the means to provide these guide rails has been the use of TypeScript and type-checking to ensure that the code is sound.
Type-checking and the use of type systems are a great way to increase code quality, prevent bugs, and enable automated refactoring of the code base at scale. However, type-checking also comes at a cost.
Proposed solution
The current (simplified) processing model of a typical Angular build system is as follows:
parse code -> analyze Angular specific metadata (e.g. decorators) -> generate JS code ->
generate TS code needed for type checking (type check blocks) -> ask TypeScript perform type-checking ->
TypeScript to downlevel `.ts` files to `.js` files -> bundle code ->
finally do something with the code (run tests, update dev server, etc)
Note that these tasks are performed in a sequence, and type-checking is in the middle of this critical path.
side note: The Angular CLI currently uses TypeScript’s incremental compilation to minimize the amount of type checking required per rebuild, which however still blocks the final steps of the bundling process, resulting in DX similar to (but slightly faster than) fully sequential build pipeline.
side note (2): We can, and do utilize parallelization in order to improve build times, especially when using build systems like Bazel and friends. With this approach we achieve parallelization by breaking down bigger apps into smaller compilation units and build those in parallel as much as possible, while still honoring the dependency graph of these compilation units. This improves edit & refresh latency in some cases but with limitations and increased complexity.
If we removed the type-checking from this critical path we unlock two benefits:
faster builds and rebuilds — type-checking is an expensive operation
enable prototypization and exploration — we'd allow unit tests and dev servers to refresh even if the code doesn't pass all of the type-checks
The mental model shift proposed by this change is to turn type-checking into a very sophisticated lint check.
Implementation details
Much is TBD still and there are many options, ranging from:
- simple but inefficient: disable type-checking in the critical path, and use a separate process to run a duplicate TS program that would type-check the code (but not block the dev server refresh / unit test run).
- We could also support a mode in which the type-checking is disabled during builds, but would still work when running
ng lintor using Angular Language Service plugin in IDEs.
- We could also support a mode in which the type-checking is disabled during builds, but would still work when running
- more evolved, but more efficient: one possible option would be to embrace a highly parallelizable transpiler/build engine like esbuild or swc with
ngc(the Angular compiler) as a plugin. We could even consider using Vite (which uses esbuild). In this setup,ngcwould need to be improved to support a "type-check-less" mode for efficiently converting Angular metadata and templates into JS code without bothering to analyze and generate code that supports type-checking.- Type-checking would still happen out of band, in a way similar to the "simple" solution.
- extreme: Transition Angular from the program-based compilation to localized compilation (where each
.tsfile can be converted to.jsfile without global program analysis or global optimization). Then use this localized compiler as a highly parallelizable plugin to a build engine like esbuild or swc. This approach would also enable Vite-style, on-demand transpilation of parts of the code base, something that is very difficult with the current Angular compiler architecture.- Type-checking would still happen out of band, in a way similar to the "simple" solution.
Trade offs
- developers could write code with bugs that could be easily caught by type-checking, and waste time debugging the issue in their dev server / unit test. This could be mitigated with:
- IDEs already offer excellent support for low latency type-checking for TypeScript, and with the recent release of Ivy-based Angular Language Service (in v12), we now have the same capability for Angular templates and decorators as well.
- We can and should still perform type-checking in the background, and embrace the "eventual consistency" mindset to provide developers with type-checking feedback in the terminal, and dev server UI (via injected scripts eventually displaying warnings about type issues in the browser)
- increased complexity of the build pipeline in order to support parallelization and eventual delivery of the type-checking results to the terminal (for
ng buildandng testor browser (forng serve). - duplication of some work — depending on the implementation we might end up performing a limited amount of work twice, for example source code parsing.
- increased memory usage of the build pipeline — similarly to the trade off above, depending on how this is implemented, the build pipeline might end up consuming more memory.
Web ecosystem compatibility & Prior art
With the rise of popularity of TypeScript in the web ecosystem, we also observed that a part of the community has embraced the approach of using TypeScript as a glorified linter already. Some examples:
- babel - the TypeScript team @ Microsoft have worked closely to enable syntax-level support (type-stripping) in babel (blog post)
- deno - supports TypeScript as the input language without performing type-checking (which is the responsibility of the TypeScript compiler that the developers might choose to run as a "linter").
- swc - TypeScript / JavaScript transpiler & bundler (used by deno).
- esbuild - TypeScript / JavaScript transpiler & bundler (used by Angular CLI (in a limited way) and Vite)
- Vite - JavaScript dev server (and build pipeline) with TypeScript support
- bazel - limited number of recently created projects have started to embrace the use of TypeScript type-checking via
testonlytargets (according to aspect.dev) - ts-node - TypeScript execution engine for Node.js with optional support for type-checking.
The trend in the community seems to be only accelerating in the direction of using type-checking as an aid, rather than as a hard block in the developer workflows.
What's the goal of this issue?
The goal is to write down some of the ideas that have been made possible thanks to Ivy and start collecting early feedback that could be used as an input for future RFC.
Alternatives considered
Keep on improving the type-checking speed — something we've been focused on for years, and we have reached the point of diminishing returns.
Activity
Airblader commentedon Aug 12, 2021
Thanks for the detailed explanation. I'm really happy to see the Angular team making these things a bit more public / transparent.
My 2c: I completely understand and welcome the motivation to shorten feedback cycles by separating the type checking process.
However, often if your types don't check out, your application won't work correctly either. So I would hope that type checking doesn't become an unnoticeable background process that would only be seen in the stdout, but remains (prominently) visible in the browser itself.
IgorMinar commentedon Aug 12, 2021
@Airblader absolutely! the goal is not to make type-checking insignificant - the errors would be very prominent.
The goal is to give developers an option to ignore the type errors temporarily during the interactive development.
We could even provide an option to make the type-checking errors blocking if developer or team prefers that (controlled via a command line flag, or angular.json config).
AwesomeObserver commentedon Aug 12, 2021
I think esbuild would be good tooling for compilation/translation code pipeline in Angular.
And moving type check to nonmain process is a chouse.
Also a piece of news, the react framework next.js working on integrating SWC for optimization pipeline
IgorMinar commentedon Aug 12, 2021
@AwesomeObserver thanks for the note. It made me realize that I was a bit too prescriptive in the implementation section. We can and should consider using any highly-parallelizable transpiler/bundler, including swc. I updated that section to be less prescriptive.
AwesomeObserver commentedon Aug 13, 2021
@IgorMinar Oh, yes is a good point
SanderElias commentedon Aug 13, 2021
I'm happy with the direction in the current RFC's
Working on large projects, where a 'save & review` cycle can take over 20 seconds, I do feel the pain.
Long term, I would say the 'extreme' option will win out in all scenarios. This would simplify the whole build pipeline, and make scaling so much easier.
It also perfectly suits the 'atomic' model from ivy, Fits the current es-2020 module model, and would mean you can process all files in parallel when you want.
Also, this would make Angular less dependent on tooling, and it would become much easier to tailor a build system that would fit the demands of the app.
It will give the developer more freedom/power on how the end result is reached.
For some apps. putting everything in 1 giant bundle is the optimal solution, while for other apps, going to the extreme of doing any bundling would benefit their use-case. While I don't think either of those extremes will suit any app, I know for sure the place of the line is different for all apps.
Angular devs will be able to make their own trade-offs, that is the thing that counts.
I'm less concerned about memory demands. To a degree, if less memory means slower builds, that is an acceptable trade-off. When less memory means a failing build, that would be a no-go.
If there is a reasonable lower bar, that is ok, as long as that is clearly documented. And properly logged errors do a lot here.
ng build `ERROR: this computer doesnt have <amount> of free memory`Back to the other part of this RFC.
Out of band type errors.
I have no problem here.
some of my random related thoughts:
ng build, but notng serveng typecheckfor CI/CD application?We should recognize there are 2 modes of operating.
in the development phase we need:
And don't care about the memory of memory use. or CPU cycles. Most devs have beefy enough machines
in the build phase we need
And we do care a bit more about memory and CPU cycles. As this will be in CI/CD and most of those systems don't have the same resources as dev machines. (or at least not without a significant cost!)
IgorMinar commentedon Aug 13, 2021
@SanderElias I completely agree with your comment except for one thing - we should remain cognisant of the memory usage for three reasons:
So yes, memory usage is a problem to be mindful of, and we can't discount it.
flash-me commentedon Aug 13, 2021
Even though I totally get the mentioned points here, I think, this also should be viewed from the opposing side.
What about applications that have been build piece by piece and are composed at runtime and therefore don't run into such troubles with long builds and/or
save & review?What if people do want type-checking?
Also the build times are hugely affected by external aspects, e.g.
So there are several options to improve build times. E.g. running tasks in container with a *nix environment is still much faster than running the same task on the windows host.
I would appreciate it, if opting out from type-checking can be turned on/off through a flag and not being hardcoded.
cheers
flash ⚡
5 remaining items
IgorMinar commentedon Aug 15, 2021
note: the original title of this issue was "Remove type-checking from the critical path of Angular builds (TypeScript and Template compilation)", but through community discussion we found "Out-of-band type-checking for TypeScript and Template compilation" more fitting, so I updated the title.
IgorMinar commentedon Aug 15, 2021
@DmitryEfimenko the extreme option requires changes to several subsystems and more importantly, it represents a mindset shift. It is definitely not cheap. Estimating the effort is hard, especially because we'd need to ensure compatibility with 2500+ apps at Google which we'd be responsible for transitioning to the new pipeline. The good news is that we could break down the project into incrementally deliverable, and on its own valuable, subprojects. A great example of this is the "optional ngmodules" a.k.a "standalone components" project that will present in an RFC in the upcoming days — this project is already heading in the direction of the "extreme" option, and is independently deliverable and very valuable.
If we prioritized this, and not changed our current level of funding and/or support obligations 1.5–3 years for end-to-end execution seems like a good guesstimate for the extreme option.
IgorMinar commentedon Aug 15, 2021
I created a writeup on the "extreme" option as a separate proposal: Transition Angular from the program-based compilation to localized compilation — please take a look if that option has piqued your interest.
bcarson commentedon Aug 19, 2021
We had a great discussion about this RFC on Angular Nation yesterday, thanks to @IgorMinar, @alxhub @SanderElias, @chaosmonster, @kroeder, @bampakoa and everyone else who joined us! Here's the link for anyone who would like to watch: https://www.youtube.com/watch?v=FbJLC7GxAAs
LanderBeeuwsaert commentedon Aug 19, 2021
As came up also in the Angular Nation discussion, sometimes you just want to quickly try out things and not be bothering with type checking.
In those moments, being able to turn type checking off somehow, would be an interesting (but admittedly potentially 'dangerous') option.
I would imagine that this would be a much less involved change than moving type checking out-of-band.
As such this could be a first quick win and could be used to experiment to what degree removing type-checking delays would help DX.
IgorMinar commentedon Aug 24, 2021
@LanderBeeuwsaert thanks for the note.
Turning off type checking today is not very easy. For starters
tscnorngcdo not support turning type checking off.There is an issue open for
tscto support--transpileOnlyoption (microsoft/TypeScript#29651) though. The discussion on that issue is actually similar in many ways to this proposal — for example check out this comment about how this could speed up CI/CD pipelines.One thing to keep in mind is that even if
tscwere to implement this feature, we might still consider using a different tool for the transpilation becauseswcoresbuildare usually orders of magnitude faster thantsc.Lastly, I'd much prefer to make type-checking non-blocking rather than push developers towards turning it off, as I do believe that type-checking is really really useful most of the time, I just don't want it to get in the way of fast iteration and exploration.
e-oz commentedon Sep 7, 2021
For me type-checking easily outweighs some milliseconds benefit. For “10000+ components” apps it might save more time, but in such apps, types are even more important.
All the options of the partial type-checking are even worse than no type-checking, imho.
Please at least consider implementing it as a feature that can be turned off.
IgorMinar commentedon Sep 7, 2021
@e-oz thanks for the feedback.
The proposal doesn't include partial type-checking of any kind. The type checking would still happen at the full fidelity and strictness just as it does today. It just proposes to move the type checking to happen out-of-band with respect to the critical edit&refresh path.
Also the cost of type-checking is measured in dozens of seconds for large projects.
Lastly, as discussed above, we should implement this so that the teams/developers can decide if the type-checking should be blocking, non-blocking, or even off.
e-oz commentedon Sep 7, 2021
thanks, @IgorMinar for clarification.
I was just a little bit worried by this
and by the plans of using esbuild (because esbuild doesn't check types).
But if it will be totally configurable - it’s amazing.
yharaskrik commentedon Sep 7, 2021
Love the effort the Angular team is putting into these RFCs, I have been very impressed as of late! (not that I wasn't impressed before that is!).
I agree that the latency really stymies the effectiveness and creativity of developers, especially when making "small" changes (this is a relative term based on your specific scenario obviously). HMR can be a way around this but I have yet to see an implementation that works really well. I have always wanted Angular to embrace a more parallelizable compiler (although the compiler up till now has been fantastic), as large apps start to get a little iffy (other solutions to this obviously, such as incremental compilation from Nx, micro-frontends using module federation etc, but these are all more involved than optimizing the compiler, for the dev obviously, not for the angular term persay). I actually wrote a blog post on the bundle size/speed for our companies code base (https://tech.trellis.org/Angular-v11-vs-v12-Build-Performance/) as I have been interested in this for a while.
I like the idea of being able to allow for type checking to be more of a lint check but would either need to be well thought out since as @e-oz mentioned type checking gets more important the larger the codebase or it would need to be opt in.
We use Nest and Nx at Trellis and the way
nx serve <nest app>works is it will actually restart the server even if there are type errors and not hold up the refresh cycle. This can be handy, except when it's not, ie. you didn't notice you forgot to pass in a new function parameter and thats why your endpoint doesn't work, for example.In my opinion extreme is the long term solution here, other tools are starting to integrate and support non TS based tooling (like esbuild and swc) as they are written in Rust or other more performant languages. I love Angular and don't want it to fall behind in terms of compilation performance or DX because it is not embracing new tools. Of course I realize this is a major lift and will take a lot of time and effort so I don't expect the Angular team to just go and implement it overnight but do strongly believe we need to keep up to date with tooling. The Angular CLI has always been one of the best selling points of Angular and one of the best built in CLI tools out there, let's not let it fall behind.
All in all, I love this RFC and support optimizing the pipeline wholeheartedly, the better we make it for the developers using it the more people will use it and love it.
fetis commentedon Sep 8, 2021
I double on this. It's especially tedious with unit testing, when a single commented line might bring you a lot of "unused variable, unused import messages". IMO during the development, unless code is compiled (ie syntax is correct) to the JS it should be shipped to the browser. I'm ok to see non-critical errors more as warnings and fix them later when I already have something working.
My current turnover on a project is between 30 and 60 secs, you loose focus and context on a such long time span.
matthewjh commentedon Sep 25, 2021
I would love to see ngc as a plugin that can be used with esbuild and other transpilation tools. If not for our dependency on ngc, we would probably have already switched to "use tsc as a linter and keep it out of the fast dev loop".
You can turn off
noUnusedLocals,noUnusedParametersetc. in your tsconfig:https://www.typescriptlang.org/tsconfig#noUnusedLocals
jpike88 commentedon Oct 5, 2021
Angular's core tools have always been greatly lacking in development cycle speed, helping give rise to a whole ecosystem of bundlers that do it multiple times faster. I jumped ship a long time ago and have contributed to several bundlers, which has allowed me to retain my sanity, and have migrated back to angular CLI due to the breaking changes related to 13. It feels like I've permanently moved out of the fast lane, to put it nicely.
The demand for performant development has always been there, it shaves precious hours off everyone's time and lets people focus on quality, not quantity. The last few major versions of Angular have done little to help with this. Throwing more stuff on the pile needs to stop and performance needs to take the front seat. Hopefully the upper management will reconsider what the priorities should be, I don't understand how such a slow cycle (10, 20, even 30 seconds a cycle) was ever acceptable. Tools like esbuild represent the pinnacle of this performance goal and all efforts should be made to heavily integrate with these kinds of tools. The only thing in the way is making that the focus.
I think the 'extreme' pathway is ideal, as it gives the most room for a clean slate and matches what bundlers out there already do with their HMR functionality. Of course, what would be truly ideal is porting the whole Angular build system to Go itself, treating esbuild as a first-party bundler and shifting away from Webpack would bring maximum performance gains, cutting a 2 minute build down to even just a few seconds. If the current alternatives out there can solve for that, surely so can the Angular team.
jpike88 commentedon Jan 26, 2022
Relevant:
https://news.ycombinator.com/item?id=30074414
https://kdy1.dev/posts/2022/1/tsc-go