3.3 Object Types

Object types are composed from properties, call signatures, construct signatures, and index signatures, collectively called members.

Class and interface type references, array types, tuple types, function types, and constructor types are all classified as object types. Multiple constructs in the TypeScript language create object types, including:

  • Object type literals (section 3.8.3).
  • Array type literals (section 3.8.4).
  • Tuple type literals (section 3.8.5).
  • Function type literals (section 3.8.8).
  • Constructor type literals (section 3.8.9).
  • Object literals (section 4.5).
  • Array literals (section 4.6).
  • Function expressions (section 4.10) and function declarations (6.1).
  • Constructor function types created by class declarations (section 8.2.5).
  • Namespace instance types created by namespace declarations (section 10.3).

3.3.1 Named Type References

Type references (section 3.8.2) to class and interface types are classified as object types. Type references to generic class and interface types include type arguments that are substituted for the type parameters of the class or interface to produce an actual object type.

3.3.2 Array Types

Array types represent JavaScript arrays with a common element type. Array types are named type references created from the generic interface type ‘Array’ in the global namespace with the array element type as a type argument. Array type literals (section 3.8.4) provide a shorthand notation for creating such references.

The declaration of the ‘Array’ interface includes a property ‘length’ and a numeric index signature for the element type, along with other members:

  1. interface Array<T> {
  2. length: number;
  3. [x: number]: T;
  4. // Other members
  5. }

Array literals (section 4.6) may be used to create values of array types. For example

  1. var a: string[] = ["hello", "world"];

A type is said to be an array-like type if it is assignable (section 3.11.4) to the type any[].

3.3.3 Tuple Types

Tuple types represent JavaScript arrays with individually tracked element types. Tuple types are written using tuple type literals (section 3.8.5). A tuple type combines a set of numerically named properties with the members of an array type. Specifically, a tuple type

  1. [ T0, T1, ..., Tn ]

combines the set of properties

  1. {
  2. 0: T0;
  3. 1: T1;
  4. ...
  5. n: Tn;
  6. }

with the members of an array type whose element type is the union type (section 3.4) of the tuple element types.

Array literals (section 4.6) may be used to create values of tuple types. For example:

  1. var t: [number, string] = [3, "three"];
  2. var n = t[0]; // Type of n is number
  3. var s = t[1]; // Type of s is string
  4. var i: number;
  5. var x = t[i]; // Type of x is number | string

Named tuple types can be created by declaring interfaces that derive from Array<T> and introduce numerically named properties. For example:

  1. interface KeyValuePair<K, V> extends Array<K | V> { 0: K; 1: V; }
  2. var x: KeyValuePair<number, string> = [10, "ten"];

A type is said to be a tuple-like type if it has a property with the numeric name ‘0’.

3.3.4 Function Types

An object type containing one or more call signatures is said to be a function type. Function types may be written using function type literals (section 3.8.8) or by including call signatures in object type literals.

3.3.5 Constructor Types

An object type containing one or more construct signatures is said to be a constructor type. Constructor types may be written using constructor type literals (section 3.8.9) or by including construct signatures in object type literals.

3.3.6 Members

Every object type is composed from zero or more of the following kinds of members:

  • Properties, which define the names and types of the properties of objects of the given type. Property names are unique within their type.
  • Call signatures, which define the possible parameter lists and return types associated with applying call operations to objects of the given type.
  • Construct signatures, which define the possible parameter lists and return types associated with applying the new operator to objects of the given type.
  • Index signatures, which define type constraints for properties in the given type. An object type can have at most one string index signature and one numeric index signature.

Properties are either public, private, or protected and are either required or optional:

  • Properties in a class declaration may be designated public, private, or protected, while properties declared in other contexts are always considered public. Private members are only accessible within their declaring class, as described in section 8.2.2, and private properties match only themselves in subtype and assignment compatibility checks, as described in section 3.11. Protected members are only accessible within their declaring class and classes derived from it, as described in section 8.2.2, and protected properties match only themselves and overrides in subtype and assignment compatibility checks, as described in section 3.11.
  • Properties in an object type literal or interface declaration may be designated required or optional, while properties declared in other contexts are always considered required. Properties that are optional in the target type of an assignment may be omitted from source objects, as described in section 3.11.4.

Call and construct signatures may be specialized (section 3.9.2.4) by including parameters with string literal types. Specialized signatures are used to express patterns where specific string values for some parameters cause the types of other parameters or the function result to become further specialized.