Flag to disallow assignment of void
to any
#45750
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
I think it would be useful to have an option to forbid the assignment of
void
values to variables/parameters of typeany
.π Search Terms
flag assign why is void assignable to any
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
My suggestion is to add a flag to disallow assignment of
void
values to variables/parameters of typeany
.As I understand it, writing
void
as a return value means that the value will not be observed. Assigning the value to a variable of typeany
thus seems like an undesirable action, and I wonder why this is even allowed at all.π Motivating Example
Simple example
Current behavior:
Desired behavior if suggestion is implemented and flag is active:
More involved example
Notice that when you click the support link, the donation page does not actually open. This is because
showThanksMessage
, which has avoid
return type, secretly returnsfalse
. This is then assigned to theonclick
callback, which is a function returningany
. This is then interpreted by the browser as 'prevent the default action of opening the link' (nowadays most people would useEvent#preventDefault()
to do this).With the proposed flag enabled, the assignment to
onclick
would be an error, as we would not be able to assign() => void
to(ev: MouseEvent) => any
because of the return types.π» Use Cases
This prevents people from accidentally using the values of
void
-returning functions, as demonstrated above.The text was updated successfully, but these errors were encountered: