Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

::= type declaration shorthand #176

Closed
STRd6 opened this issue Jan 9, 2023 · 10 comments · Fixed by #1018
Closed

::= type declaration shorthand #176

STRd6 opened this issue Jan 9, 2023 · 10 comments · Fixed by #1018
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@STRd6
Copy link
Contributor

STRd6 commented Jan 9, 2023

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 STRd6 added enhancement New feature or request good first issue Good for newcomers labels Jan 9, 2023
@STRd6
Copy link
Contributor Author

STRd6 commented Jan 9, 2023

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

@gustavopch
Copy link

gustavopch commented Jan 9, 2023

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
Copy link
Contributor Author

STRd6 commented Jan 9, 2023

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
}

@gustavopch
Copy link

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.

@edemaine
Copy link
Collaborator

edemaine commented Jan 9, 2023

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'

@sultan99
Copy link

sultan99 commented Jan 9, 2023

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

@STRd6
Copy link
Contributor Author

STRd6 commented Jan 9, 2023

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

@gustavopch
Copy link

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

@edemaine
Copy link
Collaborator

Just pointing out that using :: for interface declaration would prevent us from using it as let shorthand: name:: typelet 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.

IamIpanda pushed a commit to IamIpanda/Civet that referenced this issue Jan 13, 2023
@bbrk24
Copy link
Contributor

bbrk24 commented Feb 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants