-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[3.9] Complex types truncated in declaration files #38557
Comments
How big was your |
Almost 2.8 million chars, generated from about 50 lines of code in the corresponding .ts file... Many of these characters are of the form import("ts-json-validator/dist/json-schema")., so assigning that to some intermediate value would probably save a lot of characters. I don't know if .d.ts supports that. That's what I get for ridiculous recursive types. The file at issue defines three schemas from https://github.com/ostrowr/ts-json-validator; I'll try splitting them into three files. But 3.9 makes that library pretty useless if there's a cap on type size. Is this value configurable? |
We don't do it in the emitter ourselves, however if you write |
Thanks! That workaround fixes the issue in my codebase (with some ts-expect-error and eslint-ignores around unused variables) even though the .d.ts is still more than a million characters. Naturally, this isn't a super satisfying fix. Can you point me to the PR or discussion around introducing this limit? I don't understand why it was introduced, but I'm sure there are plenty of reasons. In any case, compilation should probably fail with a nice error message when the limit is reached, not emit invalid code. Philosophically, this is similar to my comment here: #35533 (comment) – it's OK for typescript not to be able to do something, but when it can't do something (and is able to detect that it's in that situation) it should yell at the user whenever possible! |
#37461 - you are correct in that we should consider issuing an error when this occurs, rather than relying on people noticing that the output itself contains a truncation. |
Ah, makes sense why your workaround fixed it even though the doc is over a million characters. Looks like the real maxLength is 2 * 1 million. I'll consider diving in to try to issue an error in this situation, but can't promise I'll get to it soon! It looks a little less trivial than I expected since it seems that this code implements shared functionality for when truncation really is needed. (Also makes sense why --noErrorTruncation didn't fix the issue for me.) |
Node truncation starts at 1 million estimated length (and thus stop building synthetic AST nodes), the final string is just also hardcapped at 2 million, in case the estimate and the real final size are very different. |
Re the new labels – I totally understand that a truncated .d.ts file is easier to debug than an OOM – but there are plenty of projects that do generate large (but still very useful) .d.ts files. I think tsc should never generate invalid code – in fact, I'd rather it crashed. $0.02 from me: #37461 should be reverted at least until truncation comes with a nice error message (but maybe forever.) Why limit the power of tsc on machines that can handle large-ish files? If you can't revert, this should added to the list of "breaking changes" in the release notes :) |
Can you point me to the file where this happens? I would like to experiment with some stuff. I am very certain that I've run into this before reaching 1 million characters, even with noErrorTruncation true. |
I recently ran in to this issue thanks to a PartialDeep<I*Override> where IOverrides was using the CSSProperties type PartialDeep is from typefest
which generated
This causes the truncation described in the issue. I have temporarily reverted that package to TS3.8 and the file generated is ~53MB. Any ideas for a workaround? |
Add an explicit type annotation with the type you want, rather than letting the complier print it out in full, for one. Two, the latest version of TS should be issuing an error any time this truncation would occur, so this should be fixed in 4.1 (in the sense that you'll no longer quietly get a broken million plus character declaration file). |
thanks for the suggestions @weswigham. Could you elaborate on the "adding an explicit type declaration" suggestion? This project is still on TS 4.0.3, so that explains why I didn't get any errors. |
We only emit types like this when we can't use a type annotation you've written. Wherever the generated type appears in the output, go to the corresponding place in your input, and add a type annotation. |
The type annotations are all there. Its when passing the IAvatarOverride through PartialDeep that these types get expanded. For now I have loosened the properties that were typed as |
Somewhere you need to actually write the |
TypeScript Version: 3.9.0+ (works in 3.8.x, broken in 3.9.x, 4.0.x)
Search Terms:
truncated, "...", declaration
Code
I'm still working on getting a scoped repro here, and will update this ticket when I have one. But – in TS 3.8 (and earlier), I have a complex type that produces the following code in a .d.ts file:
In 3.9+, this gets truncated as though we're in an editor environment:
Expected behavior:
Types aren't truncated in .d.ts files
Actual behavior:
Types are truncated, producing syntactically invalid
.d.ts
filesRelated Issues:
The text was updated successfully, but these errors were encountered: