-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Function Statements - interface assignment, errorless property assignment #46941
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 think what you need is Declaration Merging. function func(a, b) {
func.counter = 1
}
declare namespace func {
export var counter: number
} |
@whzx5byb Your solution seems elegant. The function remains untouched. Very good. But elegant here is relative. I haven't seen namespaces used in a hacky way like that - just to make an error go away. I'd rather not have that error in the first place. You're still dealing with an outside declaration that is strongly coupled with the function. You want that function to be atomic and easy to move. What I need is a typescript that is opt in. One that doesn't insert itself where it isn't needed and shout error where there is none. |
Typescript has a lot of tools to deal with gradual typing. The scenario you've given has a strongly typed answer, the declaration merge; as well as weakly typed answers through |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Update: |
Suggestion
π Search Terms
Function Statement , static variables
β Suggestion
Problem case:
My 3 suggestions related to this problem.
π Motivating Example
π» Use Cases
If you need static variables in func then using the func object as a bag is the only good solution in javascript.
I am not sure if this typescript error is a bug or a feature since assigning properties to func in the outer scope does not cause errors
I found the best way to get rid of the error is to add
// @ts-ignore
in front of the property assignment. However doing this everywhere just to deal with a typescript shortcoming isn't ideal.My failed attempts to work around this error by working with typescript:
The text was updated successfully, but these errors were encountered: