DanielXMoore/Civet

`::=` type declaration shorthand

STRd6 opened this issue Β· 10 comments

STRd6 commented
Id ::= string | number
// πŸ‘‡ ts
type Id = string | number

User ::=
  id: number
  name?: string

// πŸ‘‡ ts
type User = {
  id: number
  name?: string
}

Suggested by @sultan99 with some modifications.

STRd6 commented

Id :: string | name is also a possibility. It won't conflict with Id::x because the .prototype. shorthand can't have spaces.

Consolidating with #126 so we can see the consistency:

// Type
User ::
  name: string

// Object
user :=
  name: 'John'

// Function
createUser := ({
  id: userId: string
  name:: string
  ...rest: SomeOtherProps
}) =>
  // ...

I like this.

Edit: Not sure what the syntax for typing the object properties would be though. Probably not this, I guess:

user :=
  name: string: 'John'
STRd6 commented

Edit: Not sure what the syntax for typing the object properties would be though. Probably not this, I guess:

user :=
  name: string: 'John'

TypeScript already supports:

const user = {
  name: "John", // string is inferred
  id: 7 as ID // cast number to ID
}

There's the difference between declaration and assertion.

const x: 'foo' | 'bar' = 'lalala' // Error
const y = 'lalala' as 'foo' | 'bar' // No error

But we don't need to worry about this now.

Id :: string | name is also a possibility.

I'd prefer ::= for defining a type. :: looks too much like "has the type" (as in :), e.g. in Haskell. Especially if:

Edit: Not sure what the syntax for typing the object properties would be though. Probably not this, I guess:

user :=
  name: string: 'John'

In #126 we've discussed using :: for type specification in objects:

user :=
  name:: string: 'John'

Besides type we have interface. Should we be consistent? Any idea?

STRd6 commented

::= for type declaration :: for interface? Just throwing it out there.

Makes sense because type declarations have an equal sign and interface declarations don’t.

Just pointing out that using :: for interface declaration would prevent us from using it as let shorthand: name:: type β†’ let name: type. So at least we should try to think up a good let shorthand while also thinking about interface, so that we can hopefully make one consistent design.

I just noticed, this is still marked as "TODO" in the README...