sirisian / ecmascript-types

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

Allow to invoke a special mode to use implicit type assignation #37

Closed michaeljota closed 6 years ago

michaeljota commented 6 years ago

Per your proposal, when using types, you need to the navigator both, the type and the value. Although, I have to say I like the sintaxis, this may add a lot of boilerplate at the end. Using a special string in the beginning of the file, of the function, could tell the browser to implicitly assign the same type of the first value.

'use strict'
'use typing'

let num = 0; // value: 0 | type: number
num = 'hello' // Error

class Foo {
  bar = 'hello'; // value: 'hello' | type: string
  constructor() {
    this.bar = 0 // Error
  }
}
sirisian commented 6 years ago

My original spec had something like this with "use stricter" with the goal of changing the meaning of the language. Then I stumbled upon: https://esdiscuss.org/topic/removal-of-language-features#content-51

It made me reevaluate trying to change semantics and instead just stick to making additions to the language.

As for applying types the spec has the ":=" typed assignment operator.

let num := 0;
// num = `hello`; // would result in a type error

Yes, it's verbose, but it's staying with the idea of not adding new modes. I can almost guarantee you I could not find a champion that would support introducing such a large mode to the language.