Open SheepTester opened 1 year ago
One step closer to backwards compatible syntax. I like it.
For the love of all things holy, please, no...
I'd prefer just putting types in a comment personally.
//: (number, number, number) -> number
function do_something(a, b, c) {
}
//: <T>(T, number, number) -> T
function do_something(a, b, c) {
}
Constructs like Interfaces could be just normal JavaScript objects attached with decorators. They could be inspected at runtime then and if unused be removed at deployment time using tree shaking.
const Foo = new Interface({
...
})
@implements(Foo)
class FooBar {
}
I agree, ideally we shouldn't need an ECMAScript proposal for type annotations. I'm personally not a fan of this proposal in general.
Anyone could just make a modern convention using existing syntax. However, without an ECMAScript proposal backing it, we'd just end up with multiple type annotation comment syntaxes, which Python currently also struggles with.
But if the TypeScript team is really intent on adding special syntax for type annotations to the language, then I'd like them to be as unintrusive to the existing grammar as possible.
Adding fuel to the fire that is the other syntax bikeshedding proposals, I propose that type annotations be instead limited to string literals. Something like this, though for the reasons listed below, the string contents ultimately don't matter.
To be clear: you can put anything in these strings, and it wouldn't be a syntax error. The way I formatted types in the examples above isn't the focus of my proposal.
This doesn't cover all the syntax that TypeScript offers, such as
a!.b
or generic invocations, but I don't think that was the aim of this proposal. Besides, this offers enough flexibility that type checkers can invent their own syntax, like(a as '!null').b
or(add as '<number>')(4, 5)
.Here are the benefits to string literal type annotations:
<...>
). Bare string statements are already allowed and ignored in JavaScript, so no special syntax is needed fortype
andinterface
statements.var arr: Array<number>;
, directly in JavaScript may give the impression that the language gives more support for type annotations than just allowing the syntax.function future(x: instanceof Date)
that actually enforces types could still be introduced in the future.def clone(self) -> 'MyClass':
), though in their case it's because they evaluate type expressions at runtime, while this proposal ignores them.Array<number>
as a JavaScript object at runtime, but'Array<number>'
is just a string. This way, perhaps decorators could access these type annotations at runtime, like Python__doc__
. This could be useful for libraries that enforce types at runtime, for example.And downsides:
let mode: "'on' | 'off'";
) or multiline type annotations would be somewhat annoying to write. One convention is to always use template literals; this also allows types to extend across multiple lines. That might introduce extra complexity, though, since I think template literals are parsed differently, and we'd want to disallow expressions likelet x: `${a}`;
.