What & Why
Setup
Type Annotations
Type AnnotationsPrimitive TypesUnknown TypeOptional TypesUnion TypesFunction TypesObject TypesStrict and Soft object typesOptional object type propertiesObjectClass TypesArray TypesTuple TypesType AliasesGeneric TypesMagic TypesModules
Type System
ConfigurationLibraries
For Potential Contributors
Index

Object Types

Edit

Object is another fundamental part of JavaScript language. With object you can represent any composed data types as sequence of properties and values. You can create object type in Hegel by using curly braces {} and name-value pairs using a colon : split by commas , (the same as in JavaScript).

Playground
const user: { email: string; password: string } = {
email: "any.user@gmail.com",
password: "qwerty",
};

Strict and Soft object types

Defined object type is strict by default, it means that you cannot define additional property in object literal if it was not defined inside object type.

Playground
/* Error: Type "{
email: 'any.user@gmail.com',
password: 'qwerty',
status: 'offline'
}" is incompatible with type "{ email: string, password: string }" */
const strictUser: { email: string; password: string } = {
email: "any.user@gmail.com",
password: "qwerty",
status: "offline",
};

To make defined object type as soft you can add special statement ... (three dots). This statement will give Hegel information that you want to include additional properties inside object type.

Playground
const softUser: { email: string, password: string, ... } = { // 👌!
email: "any.user@gmail.com",
password: "qwerty",
status: "offline"
};

If you are familiar with Flow.js, in Flow.js this two examples (strict and soft object types) will look like this:

Playground
const strictUser: {| email: string, password: string |} = { // 👌!
name: "any.user@gmail.com",
password: "qwerty"
};
Playground
const softUser: { email: string; password: string } = {
// 👌!
name: "any.user@gmail.com",
password: "qwerty",
status: "offline",
};

Optional object type properties

As was mentioned in Optional Types, you can define object properties as optional by adding ?(question mark) before the type name.

Playground
const user: { email: string; password: ?string } = {
email: "any.user@gmail.com",
}; // 👌!

The logic is next - if you try to access to property which does not exist in object then value of this property will be undefined. Optional Types annotate that type of argument/variable/property can have annotated type value or undefined, so, undefined will be a valid value for this property.

If you are familiar with Flow.js or TypeScript, you may know about similar optional property syntax inside this two "analyzers", which look like this:

Playground
const user: { email: string; password?: string } = {
email: "any.user@gmail.com",
}; // 👌!

But Hegel doesn't have this syntax and define the same syntax for two situations (when your property is optional and when your property can contain undefined.

Object

Also, Hegel supports Object type which represents any possible object in JavaScript:

Playground
function json(obj: Object) {
return JSON.stringify(obj);
}
let result = json({ baz: 3.14, bar: "hello" }); // 👌!
result = json({}); // 👌!
result = json(new Object()); // 👌!
// Because Array is an object in JavaScript
result = json([]); // 👌!
// Because Function is an object in JavaScript
result = json(() => 2); // 👌!
// Error: Type "2" is incompatible with type "Object"
result = json(2);