-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Function adding property type error #55303
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
What is |
|
https://github.com/vuejs/core/blob/main/packages/reactivity/src/effect.ts |
|
|
|
I'd suggest using - const runner = _effect.run.bind(_effect) as ReactiveEffectRunner
- runner.effect = _effect
- return runner
+ return Object.assign(_effect.run.bind(_effect), { effect: _effect }) |
First of all, thank you for your reply above, so that I can solve the problem in this way. Although this solves the problem at hand, after assigning a value to the runner and before assigning an effect, so that you can't use Object. assign for the merge operation, the function return run function will result in the unit test not being passed.I don't seem to find a way to solve this problem directly in typescript, but instead do other parts of the code to solve the types in the ts part. But under certain conditions I have to assign the two separately, so this way the merge operation doesn't work, ts doesn't provide a direct solution for this, which confuses me, I appreciate your response, thanks a lot! |
I also endorse zclsx's statement |
In that case the error message is correct: the type says that the property always exists, but there’s an observable period of time where it’s missing. That’s a real type violation, and it makes sense that you have to write a type assertion to acknowledge it. |
So why is it that objects are guaranteed to be of the correct type when they are subsequently supplemented with relevant attributes, but functions are not guaranteed to be of the correct type when they are subsequently supplemented with attributes? I'm curious. thanks |
I don’t know where you got that idea. Leaving properties off of an object to be assigned later is an error too. |
I'm sorry, it's just me, but I need to do some logical processing between two assignment operations, how do I get there in this case without using assertions?
|
You can always use Instead, you could write a custom function as a workaround. function assign<T extends {}, U>(target: T, source: U): asserts target is T & U {
Object.assign(target, source);
}
const runner = run
//Logic handling between run and effect
assign(runner, { effect });
runner;
// ^? const runner: (() => void) & { effect: () => void; } |
SuperCool |
Thank you for your guidance! This is crazy! pretty cool |
ts No plans to implement this feature? |
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
Function addition attribute
🕗 Version & Regression Information
No solution is provided in typescript
⏯ Playground Link
No response
💻 Code
🙁 Actual behavior
const runner:ReactiveEffectRunner = _effect.run.bind(_effect);
When we use type annotations this way, we get an error: type "() => Missing attribute "effect" in any" but type "ReactiveEffectRunner< any>" This property is required in.
🙂 Expected behavior
How do we assign properties to a function if we don't use as to make assertions?
I needed typescript to provide a solution for us to solve the problem of incorrect function assignment types, instead of using crude assertions to solve it. Because typescript can put forward solutions to solve problems, it will make the code more elegant. Looking forward to your reply, thank you! 🙏
The text was updated successfully, but these errors were encountered: