sirisian / ecmascript-types

ECMAScript Optional Static Typing Proposal http://sirisian.github.io/ecmascript-types/
453 stars 4 forks source link

Nullable function examples #45

Closed sirisian closed 5 years ago

sirisian commented 5 years ago

The use of question mark tokens is now everywhere in the spec. It's probably a good idea to include nullable function syntax so people can help find any edge cases:

let foo:(uint32?)?:uint32? = x => null;

So pretty. Along with any example using the above single signature constraint the interfaces syntax should be included also:

let foo:{ (:uint32?):uint32; }? = null;
sirisian commented 5 years ago

Added here: https://github.com/sirisian/ecmascript-types#typed-arrow-functions

One last thought this brings up. Can an interface define its nullability. That is it always ideal to have the nullability of a type defined explicitly where it's used. For example:

interface IExample?
{
    (:uint32?):uint32;
}

Such syntax would mean that every usage of IExample is nullable. This hurts the readability I think of code. That said other languages allow one to define their own types which allow similar things.

function F(a:IExample):void {}
F(null);

I'm unsure, but it's something to think about. Could be added later also I believe with no problems.

sirisian commented 5 years ago

I'm gonna say no on nullable interface syntax. It's hard to read.