JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.09k stars 59 forks source link

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

Open gkielyellation opened 3 years ago

gkielyellation commented 3 years ago

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 commented 3 years ago

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 :)

charlag commented 3 years ago

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

JSMonk commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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