microsoft / TypeScript Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: allowing standalone .d.ts emit through explicit type annotations (--isolatedDeclarations, --noTypeInferenceOnExports?)
#47947
Comments
|
Even if you could force explicit type annotations on everything, I don’t think that would be enough to enable parallel compilation, since the structural typing means the compiler has to know what the imported types contain in order to check them. |
|
Bruce: the assumption would be that you generate `.d.ts` files in one step,
which is purely syntactical, i.e. no type checking happens here. And then
you type check all sources (`.ts` and `.d.ts`) in a second step. In the
second step, you have all `.d.ts` files that you generated in the first
step available - so you can perform all checks that TS does as it would
today.
|
|
Thanks for raising this issue. This is a feature/area I had been considering prototyping for a while. It sounds like we're thinking of the same thing but I'll elaborate here so you can verify. Feature DescriptionThe key goal is to allow a declaration This kind of standalone declaration generation isn't possible for the full set of TypeScript source files today. An example problematic case is when an exported type is computed based on types originated in dependencies. Declaration files can't express transitive concepts such as import { a, b } from "./dependency";
export const sum = a + b; // declaration emit will resolve this to a singular string or numberPotentially new declaration syntax could be added to mitigate this. But a general solution is to constrain the set of TypeScript that can be authored. Think of it as introducing stronger linting rules. When this need arose previously to permit standalone per-file TS->JS compilation, the Pros & ConsAs you say, this feature will permit parallelisation of declaration emit. It will also reduce the blast radius of declaration regeneration needed when editing a single file. Another less obvious benefit is that it will improve type-checking performance for consumers of otherwise bloated declaration files - by eliminating the (increasingly rare) edge cases where excessive inlining super-sizes the declaration files. And when declaration emit is decoupled from type-checking, it opens the door for high-performance declaration emit tools in other languages, e.g. Rust/Go/Zig. A downside of forcing isolation of declaration files is that it may increase the count of file accesses for anyone consuming that declaration file set during type-checking. Per-file overheads are noticeable particularly on Windows and particularly when malware scanners intercept the file access. This can be mitigated by declaration bundling. |
|
@robpalme yes, what you're writing here matches my understanding. And indeed, One thing I don't follow on: why do you think this will increase the number of file accesses needed during type checking? Shouldn't the compiler just read the exact same set of |
.d.ts emit through explicit type annotations (--noTypeInferenceOnExports?).d.ts emit through explicit type annotations (--isolatedDeclarations, --noTypeInferenceOnExports?)
My fault for not being clear. This proposal is really two things:
It is possible to deliver (1) without (2). But delivering (2) requires (1). (2) may entail more file accesses during checking, as it guarantees no inlining of types. Last I checked, the checker is lazy and only loads imports that are truly needed to check something. Inlining, as opposed to referencing, will lead to more cases where that laziness pays off and means a dependency file does not need to be loaded. This is a minor/rare case that shouldn't really sway anything. |
|
I would frame it this way:
The potential perf gains here are indeed extremely large if we think about non-error-checking scenarios. We'll have to consider what the edge cases are where the syntactic rules might be sufficient to capture this invariant, but it's a very interesting proposal. |
|
This looks like type-first mode of Flow. They obtained a great perf boost. |
|
I started doing some experimentation with |
Suggestion
TypeScript supports relying on type inference to produce (parts of) the API of a module. E.g. users can write (sorry, slightly contrived):
Note how you need to specify the type of
x(otherwise it degenerates toany), but can leave out the return type oflengthOfif you like. TypeScript will infer the type, potentially using type information from the file's (transitive) dependencies.This causes two problems.
Readability. It is difficult to understand what the return type of
countPartswill be. This is purely a stylistic issue that could be fixed with a lint check (though there is some complexity e.g. due totypeof).Compilation performance/parallelism.
Imagine you're using project references, and you have a dependency structure:
To compile, we need to first compile
textutils/splitter, wait for that to complete, e.g. 5s, then compilecounter, wait e.g. 3s, then compileapp(6s). Total compilation wall time is|app| + |counter| + |textutils/splitter|, in our example5s + 3s + 6s = 16 seconds.Now assume we could produce
.d.tsfiles without requiring transitive inputs. That'd mean we could, in parallel, produce the.d.tsfiles fortextutils/splitter,counter(andapp, though we don't need that). After that, we could, in parallel, type check and compiletextutils/splitter,counter, andapp. Assuming sufficient available parallelism (which seems reasonable, given how common multicore CPUs are), total compilation wall time is the maximum time to extract.d.tsfiles, plus the time for the slowest compile. Assuming.d.tsextraction is purely syntactical, i.e. does not need type checking nor symbol resolution, it shouldn't add more overhead than a few hundred ms. Under these assumptions, the wall time to wait for the project to compile would be500 ms + 6s = 6.5 seconds, i.e. more than a x2 speedup.The problem with that is that we cannot produce
.d.tsfiles without running full type checking, I believe purely due to type inference.RFC thus: I wonder if this would sufficiently motivate the ability to restrict using type inference in exported API?
E.g. we could have a
noTypeInferenceOnExportscompiler flag, that would allow TypeScript to parallelise emitting.d.tsacross project references, and then parallelize type checking.The counter point is that projects that experience slow builds in edit refresh situations might instead want to turn off type checking entirely for their emit, at least on critical paths. However that means users do not see compilation results, and produces additional complexity (e.g. when and how to report type checking failures).
Impact
We've run some statistics internally at Google on build operations.
As one would expect, this change has little impact on most incremental "hot inner loop" builds, as those typically just re-type check a single file and produce no
.d.tschange at all, so caching saves us from long build chains. We're seeing ~20% wall time improvements in the 90th percentile across all builds involving TypeScript.However the impact on slower builds is more substantial. For a sample "large" project that sees both slow individual compiles and a long dependency chain, we see ~50% improvement in the 90th percentile, and 75% in the 99th percentile (which is representative of CI style "cold" builds with little caching).
performance compilation parallelism inference declarations
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: