IssueRaiser

#AvoidTheVoid Allow users to avoid the `void` type.

# Suggestion Allow developers to avoid the `void` type. ![AVOIDTHEVOID](https://user-images.githubusercontent.com/2996754/107414463-0724c180-6ae0-11eb-98a9-e1c9f26f1e34.png) I’d like to propose a couple changes to TypeScript to make `void` an optional type, and infer `undefined` in more places where `void` is currently used. I’ve argued in multiple places that TypeScript’s `void` type is confusing and detrimental to development, and I was asked by a maintainer to create the following document to outline the problems with `void` and propose potential solutions. ## The Problem First, what is `void`? According to [the TypeScript handbook](https://www.typescriptlang.org/docs/handbook/basic-types.html#void): > void is a little like the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value. In practice, the `void` type is more or less a black hole kind of type. Nothing is assignable to `void` except itself, `undefined` and `any`, and `void` is not assignable to anything except itself, `unknown` and `any`. Additionally, typical type narrowings like `typeof x === "string"` do not narrow `void`, and `void` infects type unions like `void | string` by making the resulting type behave like `void`. The handbook hints at the original use-case for void, which is that it was used to model functions which don’t return, i.e. functions without a `return` statement. Although the runtime behavior of thes

Posted by brainkim on Feb 9, 2021, 6:22 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Software)

#AvoidTheVoid Allow users to avoid the `void` type.

brainkimFeb 9, 2021, 6:22 PM

# Suggestion Allow developers to avoid the `void` type. ![AVOIDTHEVOID](https://user-images.githubusercontent.com/2996754/107414463-0724c180-6ae0-11eb-98a9-e1c9f26f1e34.png) I’d like to propose a couple changes to TypeScript to make `void` an optional type, and infer `undefined` in more places where `void` is currently used. I’ve argued in multiple places that TypeScript’s `void` type is confusing and detrimental to development, and I was asked by a maintainer to create the following document to outline the problems with `void` and propose potential solutions. ## The Problem First, what is `void`? According to [the TypeScript handbook](https://www.typescriptlang.org/docs/handbook/basic-types.html#void): > void is a little like the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value. In practice, the `void` type is more or less a black hole kind of type. Nothing is assignable to `void` except itself, `undefined` and `any`, and `void` is not assignable to anything except itself, `unknown` and `any`. Additionally, typical type narrowings like `typeof x === "string"` do not narrow `void`, and `void` infects type unions like `void | string` by making the resulting type behave like `void`. The handbook hints at the original use-case for void, which is that it was used to model functions which don’t return, i.e. functions without a `return` statement. Although the runtime behavior of thes

Comments (0)

No comments yet. Start the discussion.

Comment on this issue