Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Allow optional property syntax, similar to typescript with key?: value #322

Open
gkielyellation opened this issue Jan 14, 2021 · 5 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@gkielyellation
Copy link

It would be great for users coming from typescript to be able to write the following:

type ObjectType = {
  key?: string
}

const test: ObjectType = {};

Thanks.

@JSMonk
Copy link
Owner

JSMonk commented Jan 15, 2021

Hi @gkielyellation.
You will have the same behavior if you will move the question mark after : like this:

type ObjectType = {
  key: ?string
}

const test: ObjectType = {};

But in the next version (Hegel 2) we will use TypeScript syntax :)

@JSMonk JSMonk added enhancement New feature or request question Further information is requested labels Jan 15, 2021
@charlag
Copy link

charlag commented Jun 19, 2021

@JSMonk isn't it significant whether the key is present or not?

@JSMonk
Copy link
Owner

JSMonk commented Jun 22, 2021

I think that, If the developer defined the property as undefinedable, it means that it thinks that the property can be inside the object, so, in the case there is no difference, because in both case, at the static time, the result will be undefined.
The only difference will be when we use in operator.

@thecotne
Copy link
Contributor

Object.keys and Object.entries also give different results when prop is not there vs when prop is undefined

Screenshot 2021-06-22 at 18 52 56

@JSMonk
Copy link
Owner

JSMonk commented Jun 22, 2021

It's true. And also we infer the keys result from the object type.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants