DanielXMoore / Civet

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

Nested type annotation fails to parse #1322

Closed STRd6 closed 3 months ago

STRd6 commented 4 months ago

When adding a nested type annotation the parser fails to pick it up:

function f(v: T):
  O

This is useful in cases where there are more complex types and having the additional newline makes it easier to see what's going on.

function O<T>(v: T):
  T extends boolean ? ObservableBoolean<T> :
  T extends number ? ObservableNumber<T> :
  ObservableValue<T>
edemaine commented 4 months ago

How do you imagine distinguishing the indented type from the indented body of the function? I believe this is ambiguous in general...

As a workaround, you can use parentheses around the type for now.

Here's another fun way to write it (but still requires parens):

function O<T>(v: T): (
  if T extends boolean
    ObservableBoolean<T>
  else if T extends number
    ObservableNumber<T>
  else
    ObservableValue<T>
)
STRd6 commented 4 months ago

The colon makes this mostly unambiguous. Here's something that works already:

function O<T>(v: T): T extends boolean ? ObservableBoolean<T> :
  T extends number ? ObservableNumber<T> :
  ObservableValue<T>

  v

https://civet.dev/playground?code=ZnVuY3Rpb24gTzxUPih2OiBUKTogVCBleHRlbmRzIGJvb2xlYW4gPyBPYnNlcnZhYmxlQm9vbGVhbjxUPiA6CiAgVCBleHRlbmRzIG51bWJlciA%2FIE9ic2VydmFibGVOdW1iZXI8VD4gOgogIE9ic2VydmFibGVWYWx1ZTxUPgoKICB2Cgo%3D