-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Function augmented types are enforced inconsistently #61554
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
Comments
I guess #26368 didn't anticipate that conflicting stuff might be merged into |
This feels like the wrong solution to the problem. With your augmentation you're claiming that every function of any sort observed anywhere in your application might have a |
Thanks for the input! If there's a solution that wouldn't involve me changing 100K+ files of React components I'd certainly be interested, but besides that this is a fairly safe assumption (since the field we've chosen is specifically unused and will not be used by anything else). In any case, whether or not this is the best approach for what I'm trying to do is less relevant here than why TS is blocking it inconsistently, which is why I opened the issue in the first place |
Agreed, my feedback wasn't on-point here. I apologize. This isn't an augmentation problem; #26368 allows override of any function prototype property with an incompatible one, although it tracks that specific function as having the new type: Playground function foo() {
}
foo.toString = 14;
var x = foo.toString // number |
This follows from the intended semantics from #26368:
This is the first report of anyone ever not liking this combo, and I'm not sure which one you're proposing we change. A different possible workaround you can do is (TestFunc1).test = 1; since that skips the middle rule. |
I see, thanks for the workaround! Is there any documentation on the handbook for this behaviour with functions? I could see this tripping up other people since it's not the behaviour I would've expected, and since there's no documentation I also didn't end up thinking of the Regardless, thanks for everyone's time! |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
function, interface, augment, parentheses, inconsistent
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEBzCB7ARlC8DeAoe8AlgHYAuIMAZlGAgGICuxYphKxO+B85AzqQH4AXPH4wSiANxcAvrjm4w7fvAAqIfo2YBGeAF54lJizYcAFAEocM6UuIr1m4wCZ98M0eat27q9hkW0p4mPo6kWmAAzJbWtsqkahqkAIIwMCgA7hG6BjF6AHyxivGJ-KnpWS5uZnmF-oG4uGHZAHR8CQba0s0ubUluXU1JEZF9Kp3dSeWZre0Dk2VpM71zE40gAB4ADigwCf7SQA
💻 Code
🙁 Actual behavior
Only TestFunc2 and TestArrowFunc2 (the ones with
()
) are flagged as errors. All other cases are perfectly fine according to TypeScript.🙂 Expected behavior
All five of the
.test = 1
lines should be flagged as errors, since1
is not compatible withstring | undefined
.Additional information about the issue
I've only found this issue with properties on functions. Others (String, Number) work correctly in all cases, and even Function works correctly when the value is wrapped with
()
- but wrapping it in parentheses doesn't seem to have any semantic differences here.My use case is trying to augment functions (specifically React components) in a massive codebase while enforcing types on the metadata being passed.
Because of this bug, I have to use this instead:
which is poor DX on three fronts (frustrating to read/write, appears redundant but is needed, and introduces a required import on every use).
Looked at:
The text was updated successfully, but these errors were encountered: