-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Limit of 25 members when using union of mapped type #40803
Comments
When the source object doesn't have |
I don't know what you mean by your first sentence. Was there a better way to achieve this same effect? I can work around this error by casting the test parameter to "any" first, and that's no major loss, but if I'm running into a design limitation, I sure would like a way to do this correctly. |
Here's another example of this limit of 25 union entries causing a false type error: #42518 / Playground. |
Here another example: Gist - Playground. Thanks |
This is a bit annoying and confusing. Is there any way to increase or remove this limit? |
instead of having the typescript error "malfunction", can we at least have it indicate that there is some sort of a limit? We had only found out about this limit by commenting out 1 mapped type at a time until the error message went away. |
@RyanCavanaugh what was the reason this issue got closed in 2021? The project I'm working on it's using TypeScript v5.0.4 (released 2023-04-07) and this error it's still happening. If there's a technical limitation that makes this issue difficult to resolve, at least there should be a clearer error than the current behaviour is showing. TypeScript Error
I write here because it's the oldest issue posted with this behavior. But I'm aware of this response. |
There's no upper limit on how much work could be incurred here; you could have an object with 16 properties each with 16 possible values; do you expect TS to do the full expansion of 18,446,744,073,709,551,616 different possible values? Certainly not. So there has to be an arbitrary limit somewhere; the limit we chose is 24 because that's always going to be reasonably fast. |
And what about improving the error message showed when this happens? Is it possible or even reasonable to expect that a better error message should be shown? I imagine something along the lines of:
|
It's extremely common for this limiter to hit and for nothing undesired to happen as a result. If we knew that the non-assignability only occurred because we hadn't expanded the union, we would have simply not reported that it was non-assignable in the first place. What you're describing would imply that you would see that message in the majority of cases where the source type was a nontrivial union, even though only a tiny fraction of the time would a full expansion have meant a successful assignment |
Maybe I'm understanding this incorrectly, but I think most commenters in this thread would feel that something like a warning message shown when the union exceeds 25 would be extremely helpful instead of the current error being shown. Perhaps "Warning: exceeding 24 members can not be accurately compared"? it would be more honest than what's being shown - is this possible, desirable? could this possibly be achieved via a configurable option? |
I don't think you're understanding it correctly, or have a very different perspective on whether or not TypeScript's error messages have enough noise in them. The only way we could do this would be to issue the error every time the source union was reasonably big, which is an incredibly common occurrence. In effect, nearly every time you saw an assignability error, it would say "We couldn't expand it because it was too big", and ~99% of the time, expanding the union wouldn't have caused the assignment to succeed anyway. |
is a flag for increasing the limit a little above 25 a possibility? eg, to 30, or 100 this flag would be really helpful for people who
Thinking for example of large teams who have critical code blocked by this limitation, and who need to either buy themselves some time to re-architecture or just swallow a small performance hit (costs of having a huge codebase etc) |
Well, in my case I change my code to compare in batches of unions to avoid having one union of more than 25 items. Maybe this is better than increasing the limit performance-wise? |
After spending hours on detective work on my complex code base, I also ended up here. I just wonder how many dev-hours has been wasted on this My case is as follows: type Kind = 1 | 2 | 3 ... 26;
type Option<T extends Kind> = {kind:T;}
type Options = Option<1> | Option<2> | Option<3> ... Option<26>; // Option<Kind> is not suitable in my case
const kind:Kind = getKind(...);
const result:Options = {kind};
Type '{ kind: Kind; }' is not assignable to type 'Options'.
Type '{ kind: Kind; }' is not assignable to type 'Option<26>'.
Types of property 'kind' are incompatible.
Type 'Kind' is not assignable to type '26'.
Type '1' is not assignable to type '26'. I was looking around and try to figure out any workaround whatsoever, just to figure out that such "design is wrong", but no examples of good design, and no workarounds. The workaround I eventually came up with is something as silly as: const result:Options = kind === 1 || kind === 2 ? {kind} : {kind}; I hope some more people can share their ideas on good design or workaround |
…lements having a length greater than 24. I've taken one deprecated element out so that we come under the limit. We should prune this list anyway, having a set of elements that demonstrate features of the library (e.g. field types), rather than specific elements used in Composer. Related issue: microsoft/TypeScript#40803 Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk>
…all text fields (#424) * Add plugin which adds widget decorations around the word 'widget' anywhere it appears, so that we can test widget decorations in isolation within the demo * Add identifier to test widget decoration * Remove existing decoration fix related to repeater depth, because it didn't solve the decoration bug in all cases. Add new fix, which explicitly handles DecorationSet and DecorationGroup, either of which we could receive. Make sure the decorations are offset correctly, in a manner that supports Widget and Node decorations in addtiion to inline decorations * Improve comment explaining the class declaration for DecorationGroup * Tidy widget decoration code in demo helpers * Apply linter * Restore some code that didn't need to be changed * Add tests and supporting code from Jon's branch Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk> * Solve TypeScript build error. This was related to the union type of elements having a length greater than 24. I've taken one deprecated element out so that we come under the limit. We should prune this list anyway, having a set of elements that demonstrate features of the library (e.g. field types), rather than specific elements used in Composer. Related issue: microsoft/TypeScript#40803 Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk> * Add tests for widget decorations specifically * Add test for case where decorations are dropped if they exist in an element following existing document content * Remove unnecessary code Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk> * Remove unnecessary code Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk> * Remove unnecessary code Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk> * Recreate decorations in context of inner editor's node, to avoid losing inline decorations * Move DecorationGroup to declaration file * Move decoration handling functions to own file as pure functions * Remove this.setDecorationsForEditor, which is now redundant * Add changeset --------- Co-authored-by: Jonathon Herbert <jonathon.herbert@guardian.co.uk>
TypeScript Version: 4.0.2
Search Terms: 25, limit, mappings, mapped, label:"Domain: Big Unions", keyof
Expected behavior: Unions of mapped types (or keyof said mapped type) should work through assignment
Actual behavior: Compiler raises error when one keyof parameter is assigned to another keyof parameter. Removing any of the 26 entries in the mapped type solves the issue.
Related Issues: #35493 is similar; this question on StackOverflow appears to deal with the same behavior
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: