This project is read-only.

Void type as first class type

Topics: Language Specification
May 20, 2014 at 5:27 AM
It would be great if compiler could understand void types and put nothing in the generated JS.
Then it would be possible to write this code and change function signatures without changing the usage !

Working one:
function f() : number {}
function g(x : number) : void {}

var a = f();
Not working with void:
function f() : void {}
function g() : void {}

var a = f();
May 20, 2014 at 6:42 AM
Edited May 20, 2014 at 6:48 AM
Void is not a type - it means "returning nothing", and you are trying to return something from nothing, which is not allowed. I understand that this is a request to open a hole in the type enforcement system to delete the "= f();" part, but what if someone did this by mistake? I'm sure 99% of people would never expect this behaviour. What if I typed 'g' instead of 'f' (assuming no parameters), and it compiled fine, in a 5000 line TS file? The type system SHOULD capture this as an error, otherwise it could make bugs a nightmare for many more people than it would help. If this is to be proposed properly, there'd have to be a modifier associated, much like "partial methods" in C#, where the "partial" modifier is used.
May 20, 2014 at 7:07 AM
Another idea may be a directive similar to the "[Conditional("DEBUG")]" attribute in C# - see discussion here:
May 21, 2014 at 7:54 AM
Edited May 21, 2014 at 7:54 AM
You can use it in generics, so it is a type:
function f<T>(){}

class A<U,V>{

var x = new A<number, void>();
As long as you don't use a variable with void type it is fine.
May 21, 2014 at 3:17 PM
Your example actually works when all you do is change type 'number' to type 'void':
function f(): void { }
function g(x: void): void { }
var a = f();
However it doesn't work if you change 'g' to have no parameters because the absence of a parameter isn't the same as a parameter of type 'void'.
May 21, 2014 at 5:02 PM
Edited May 21, 2014 at 5:22 PM
@NN 'void' is not really a type. It's a type placeholder that represents 'no type' that happens to mean 'undefined' is expected (or null). It's only a type in that its a no-type type. ;) Apparently you can expect void values (undefined), so it appears I was off there, but you cannot assign anything but "undefined"/"null" to voided vars/properties.

As the specification says: "The Void type exists purely to indicate the absence of a value, such as in a function with no return value. ...... NOTE: We might consider disallowing declaring variables of type Void as they serve no useful purpose."

@ahejlsberg I think he wants the code removed from the output, which doesn't occur when void is used.

ps. Is black a color? I suppose it depends on how you look at it, or don't (pun intended). ;)
Jun 4, 2014 at 9:13 AM
Making void as type allows writing generic functions easily.
For example CLR type system doesn't consider void as a type, so it has duplication for methods returning types and methods returning void.

Also this is useful for creating very generic interfaces.
For instance:
interface DictionaryOrSet<K, V>
  add(key : K, value : V) : void;
  remove(key : K) : void;

class Dictionary<K,V> implements DictionaryOrSet<K,V> {
  add(key : K, value : V) : void { }

class Set<T> implements DictionaryOrSet<T,void> {
  add(value : T) : void { }
Jun 4, 2014 at 7:37 PM
Those types definitions there are valid in TypeScript, you just need to implement 'remove' in Dictionary and Set as per the normal interface implementation rules.
Jun 7, 2014 at 6:12 PM
danquirk wrote:
Those types definitions there are valid in TypeScript, you just need to implement 'remove' in Dictionary and Set as per the normal interface implementation rules.
Great. It really works.
I just didn't try it :)