Skip to content

Attempting to assign the result of a function typed as :void or :never should cause an Error #57773

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
SeoDeveloper01 opened this issue Mar 14, 2024 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@SeoDeveloper01
Copy link

SeoDeveloper01 commented Mar 14, 2024

🔍 Search Terms

"void function result assignment", "never function result assignment", "void functions assignment error", "disallow void and never assigning"

⭐ Suggestion

Functions that don't return any value are typed as :void or :never. However, currently, it's possible to assign the execution result of these functions without any warning or error. In the case of :void type, it will return undefined (the default return value in JavaScript), and in the case of :never, its result isn't reachable at all. So, how can we assign it? Obviously, it makes no sense.

📃 Motivating Example

function foo(): void {
    console.log('')
}

function bar(): never {
    while (true) { }
}

/**
 * Here we should get warning/error
 */
const baz = foo() // baz implicitly has type :void, but actually contain undefined
const qux = bar() // qux implicitly has type :never, but actually result of bar() function don't reachable

💻 Use Cases

For consistency with other strongly typed programming languages, where we cannot assign the result of executing :void and :never functions.

@fatcerberus
Copy link

For void specifically, see #42709. For never, the fact that it’s assignable to anything is the type-theoretically correct behavior as never is the bottom type and therefore a subtype of all other types.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 15, 2024
@RyanCavanaugh
Copy link
Member

In the case of :void type, it will return undefined

Common misconception; void isn't an alias for undefined. See https://stackoverflow.com/questions/58885485/why-does-typescript-have-both-void-and-undefined

Either of these situations can occur in non-problematic ways in generic code so it's not really clear what value these rules would provide. You'll get errors on use, and can get errors on unused variables, so this doesn't really seem to accomplish anything.

@SeoDeveloper01
Copy link
Author

SeoDeveloper01 commented Mar 16, 2024

Maybe I wasn't clear enough in my explanation. So, to put it simply, my main argument is that TypeScript should follow the behavior of other statically typed languages like C++, C#, Java, etc., where we can't assign the result of a non-value returning function (or a void function). It will make more semantic difference between void, never, and undefined types.

@RyanCavanaugh
Copy link
Member

That is what I understood the request to be, yes

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants