Skip to content

Flag to disallow assignment of void to any #45750

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

Closed
5 tasks done
stevenwdv opened this issue Sep 6, 2021 · 2 comments
Closed
5 tasks done

Flag to disallow assignment of void to any #45750

stevenwdv opened this issue Sep 6, 2021 · 2 comments
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

Comments

@stevenwdv
Copy link

Suggestion

I think it would be useful to have an option to forbid the assignment of void values to variables/parameters of type any.

πŸ” Search Terms

flag assign why is void assignable to any

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

My suggestion is to add a flag to disallow assignment of void values to variables/parameters of type any.
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 type any thus seems like an undesirable action, and I wonder why this is even allowed at all.

πŸ“ƒ Motivating Example

Simple example

Current behavior:

const x: () => void = () => 42; // So far so good
console.log(x()); // Prints 42; this shouldn't be allowed because `x` returns void

Desired behavior if suggestion is implemented and flag is active:

const x: () => void = () => 42;
console.log(x()); // Error: void is not assignable to any

More involved example

const thanks = document.createElement("span");
thanks.textContent = "Thanks for supporting us!";
thanks.hidden = true;
const showThanksMessage: () => void = () => thanks.hidden = false;

const support = document.createElement("a");
support.textContent = "Support us!";
support.href = "/donate";
support.target = "_blank";
support.onclick = showThanksMessage;

document.body.append(support, " ", thanks);

Notice that when you click the support link, the donation page does not actually open. This is because showThanksMessage, which has a void return type, secretly returns false. This is then assigned to the onclick callback, which is a function returning any. This is then interpreted by the browser as 'prevent the default action of opening the link' (nowadays most people would use Event#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.

@MartinJohns
Copy link
Contributor

Related: #42709

@andrewbranch andrewbranch added 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 labels Sep 7, 2021
@stevenwdv
Copy link
Author

I'm closing this as it has no chance of being implemented if even #50752 is not feasible.

@stevenwdv stevenwdv closed this as not planned Won't fix, can't repro, duplicate, stale Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants