Ideas on type hinting and named parameters

Luke Scott luke at webconnex.com
Tue Jun 9 15:05:52 UTC 2015


> On Jun 9, 2015, at 1:21 AM, Thaddee Tyl <thaddee.tyl at gmail.com> wrote:
> 
> I like that syntax.
> 
> I wonder about the runtime semantics of it, though. (I know TypeHint
> is a TODO, but…)
> Is there any implication at runtime of a function that returns a String?
> Is this:
> 
> ```js
> var point = {
>  x Number,
>  y Number
> };
> ```
> 
> … the same as this?
> 
> ```js
> var point = {
>  x: Number(),
>  y: Number()
> };
> ```
> 
> In which case we would need rules for constructors to always have
> default parameters, right?

This brings up a good point. I’ll open an issue for it.

Likely built-in types such as String, Number, and Boolean would have special meaning, assuming they are not shadowed. Their default values would be “”, 0, and false. Other types would be undefined by default.

Alternatively it could always be undefined, but that can be problematic, especially with Number.

> If those annotations are all only for a non-runtime type check, I feel
> strange now about having type analysis only for newly defined symbols.
> Since this is meant as some kind of pre-runtime `assert(symbol
> instanceof Type)`, we might want to check:
> - field types (such as `foo {bar Number}`)
> - parameter types (such as `Array[Number]` or `Map[String, Array[String]]`)
> - function types (such as `function(String) Number`)
> - union types (such as `RegExp | null`)
> - generics, possibly with extends / superclass (such as
> `function(items Array[T extends Entity], unmap function(Number) T)`)?

Types should probably be checked at runtime. However, having the flexibility to catch errors with a linter before running the code would be extremely valuable. So I’m not sure I would actually take it as far as Array[Number] because you can only enforce that at runtime given that an Array can contain anything.

Adding typed arrays in the language may be more beneficial. For example if you had NumberArray you could enforce the types in the array before they are added:

```js
push(...values Number) {...}
```

Luke



More information about the es-discuss mailing list