Proposal: Static Typing
waldemar at google.com
Thu Apr 4 00:49:58 UTC 2019
On 3/23/19 1:34 PM, IdkGoodName Vilius wrote:
> This is a proposal for static typing. Here is the github repository link: https://github.com/CreatorVilius/Proposal-Static-Typing
> I think it would be great thing in JS.
We intentionally reserved syntax so that something like that would be possible in the future.
I've also spent a lot of time working on past proposals to do such things. A few interesting issues would invariably arise that make both static and runtime typing unsound:
- If you allow user-defined types, objects can spontaneously change their type by mutating their prototype. Thus, you can declare a variable x to have type Foo and check that you're assigning an instance of Foo to it, but the value x can become a Bar (different from a Foo) spontaneously without any operations on x.
- Something similar happens with trying to type arrays. You wrote:
let c: auto = ['a', 'b'] // c is now string
but that begs the question of what is the type of just ['a', 'b'].
- Is it string? No, it can't be that because you can replace its second element with a number.
- Is it any? Well, in that case c should have type any, not string
- Is it object? In that case c should have type Object.
and so on.
If you follow this to its logical conclusion and think about what the types of various methods that work on arrays should be, you end up with an enormous and confusing variety of array and object types, which is something we explored years ago. In some cases you'd want structural types, in some cases you'd want 'like' types (an array of anything which just happens to hold numbers at the moment), and so on.
More information about the es-discuss