arthurfiorette / proposal-safe-assignment-operator

Draft for ECMAScript Error Safe Assignment Operator
https://arthur.run/proposal-safe-assignment-operator/
MIT License
1.15k stars 14 forks source link

Example try expression in Typescript #48

Open Arlen22 opened 5 hours ago

Arlen22 commented 5 hours ago

I've updated the current Typescript to include this operator as a passthrough (as though it's implemented in Javascript) so we can play around with examples a bit.

https://github.com/Arlen22/TypeScript - main branch

I basically just implemented it as an expression operator, based off void and await.

To try it, clone the repo, run the following...

npm install
npm run pack
npm pack

then npm install the output file in an example project. If you're using vscode you can change the tsserver location to get intellisense as well.

Arlen22 commented 5 hours ago

The details are basically that when it encounters the try keyword, it first tries to parse it as a statement, then as an expression, unless it's already in an expression context, in which case it parses it as an expression (curly braces become objects).

I'm not sure I like that last part. I think curly braces should be an error unless they are surrounded by parentheses. But this sounds like all the other edge cases we're debating here, and a simple predictable language is better than one with random guarded edges. That's more what linters are for. But it could be a source of unending confusion in this case, so it might be a good idea.

I haven't tested any unusual parts of Javascript yet, like using or for loops.

Arlen22 commented 5 hours ago

For actual implementation in TypeScript, I think this should be converted to a self-executing function that returns the appropriate values. It's pretty standard for various syntax types to be converted into helper functions until they are implemented in Javascript.

I'll probably do that sometime in this example.