4.5 Object Literals
Object literals are extended to support type annotations in methods and get and set accessors.
PropertyDefinition: ( Modified ) IdentifierReference CoverInitializedName PropertyName :
AssignmentExpression PropertyName CallSignature {
FunctionBody }
GetAccessor SetAccessor
GetAccessor: get
PropertyName (
)
TypeAnnotationopt {
FunctionBody }
SetAccessor: set
PropertyName (
BindingIdentifierOrPattern TypeAnnotationopt )
{
FunctionBody }
The type of an object literal is an object type with the set of properties specified by the property assignments in the object literal. A get and set accessor may specify the same property name, but otherwise it is an error to specify multiple property assignments for the same property.
A shorthand property assignment of the form
prop
is equivalent to
prop : prop
Likewise, a property assignment of the form
f ( ... ) { ... }
is equivalent to
f : function ( ... ) { ... }
Each property assignment in an object literal is processed as follows:
- If the object literal is contextually typed and the contextual type contains a property with a matching name, the property assignment is contextually typed by the type of that property.
- Otherwise, if the object literal is contextually typed, if the contextual type contains a numeric index signature, and if the property assignment specifies a numeric property name, the property assignment is contextually typed by the type of the numeric index signature.
- Otherwise, if the object literal is contextually typed and the contextual type contains a string index signature, the property assignment is contextually typed by the type of the string index signature.
- Otherwise, the property assignment is processed without a contextual type.
The type of a property introduced by a property assignment of the form Name :
Expr is the type of Expr.
A get accessor declaration is processed in the same manner as an ordinary function declaration (section 6.1) with no parameters. A set accessor declaration is processed in the same manner as an ordinary function declaration with a single parameter and a Void return type. When both a get and set accessor is declared for a property:
- If both accessors include type annotations, the specified types must be identical.
- If only one accessor includes a type annotation, the other behaves as if it had the same type annotation.
- If neither accessor includes a type annotation, the inferred return type of the get accessor becomes the parameter type of the set accessor.
If a get accessor is declared for a property, the return type of the get accessor becomes the type of the property. If only a set accessor is declared for a property, the parameter type (which may be type Any if no type annotation is present) of the set accessor becomes the type of the property.
When an object literal is contextually typed by a type that includes a string index signature, the resulting type of the object literal includes a string index signature with the union type of the types of the properties declared in the object literal, or the Undefined type if the object literal is empty. Likewise, when an object literal is contextually typed by a type that includes a numeric index signature, the resulting type of the object literal includes a numeric index signature with the union type of the types of the numerically named properties (section 3.9.4) declared in the object literal, or the Undefined type if the object literal declares no numerically named properties.
If the PropertyName of a property assignment is a computed property name that doesn’t denote a well-known symbol (2.2.3), the construct is considered a dynamic property assignment. The following rules apply to dynamic property assignments:
- A dynamic property assignment does not introduce a property in the type of the object literal.
- The property name expression of a dynamic property assignment must be of type Any or the String, Number, or Symbol primitive type.
- The name associated with a dynamic property assignment is considered to be a numeric property name if the property name expression is of type Any or the Number primitive type.