DanielXMoore / Civet

A TypeScript superset that favors more types and less typing
https://civet.dev
MIT License
1.56k stars 33 forks source link

`autoLet` with typed declaration #845

Open 747 opened 11 months ago

747 commented 11 months ago

Without autoLet:

sos .= 0
soso: number .= 0

generates

let sos = 0;
let soso: number = 0;

but under autoLet:

"civet autoLet"
sos = 0
sos_: number .= 0 // this works
soso: number = 0

to be

let sos = 0;
let sos_: number = 0; // this works
let number; // this works
({ soso: (number = 0) });

(the comment behaves weirdly as well, but it is not in the scope of this discussion :P)

edemaine commented 11 months ago

soso: number = 0 has a non-declaration meaning (object literal with an assignment in the value). I hesitate to change its behavior in autoLet mode. Rather, the intent is that manual lets should use .= syntax as usual. autoLet affects assignments, and soso: number = 0 isn't a valid assignment of soso (rather, it's an assignment to number).

747 commented 11 months ago

I would suggest allowing syntax like below if it wouldn't cause other semantic conflicts. It feels more mentally consistent (at least inside me) when someone is using autoLet (or autoVar).

x(: number) = 0
bbrk24 commented 2 months ago

Would it be possible to do something here with the new :: operator?

edemaine commented 2 months ago

Yes, interesting idea: we could define sos:: number = 5 to mean sos = 5 and give the type annotation number to autoLet/autoVar. (This feels similar to my old CoffeeScript fork that used sos ~ number = 5.)

I guess sos:: number = 5 is slightly easier to type than sos: number .= 5, and probably nicer in an autoLet/autoConst (e.g. CoffeeScript) codebase that otherwise only uses = for declaration. But it is also very close...

I will point out that it's a different meaning than we currently have for ::, which can only occur in declarations and only within items of array and object patterns. Currently the declaration sos:: number .= 0 isn't valid (though it could be), and even less so with assignment (=). But we can decide what :: means in a broader context.