1.3 Object Types

TypeScript programmers use object types to declare their expectations of object behavior. The following code uses an object type literal to specify the return type of the ‘MakePoint’ function.

  1. var MakePoint: () => {
  2. x: number; y: number;
  3. };

Programmers can give names to object types; we call named object types interfaces. For example, in the following code, an interface declares one required field (name) and one optional field (favoriteColor).

  1. interface Friend {
  2. name: string;
  3. favoriteColor?: string;
  4. }
  5. function add(friend: Friend) {
  6. var name = friend.name;
  7. }
  8. add({ name: "Fred" }); // Ok
  9. add({ favoriteColor: "blue" }); // Error, name required
  10. add({ name: "Jill", favoriteColor: "green" }); // Ok

TypeScript object types model the diversity of behaviors that a JavaScript object can exhibit. For example, the jQuery library defines an object, ‘$’, that has methods, such as ‘get’ (which sends an Ajax message), and fields, such as ‘browser’ (which gives browser vendor information). However, jQuery clients can also call ‘$’ as a function. The behavior of this function depends on the type of parameters passed to the function.

The following code fragment captures a small subset of jQuery behavior, just enough to use jQuery in a simple way.

  1. interface JQuery {
  2. text(content: string);
  3. }
  4. interface JQueryStatic {
  5. get(url: string, callback: (data: string) => any);
  6. (query: string): JQuery;
  7. }
  8. declare var $: JQueryStatic;
  9. $.get("http://mysite.org/divContent",
  10. function (data: string) {
  11. $("div").text(data);
  12. }
  13. );

The ‘JQueryStatic’ interface references another interface: ‘JQuery’. This interface represents a collection of one or more DOM elements. The jQuery library can perform many operations on such a collection, but in this example the jQuery client only needs to know that it can set the text content of each jQuery element in a collection by passing a string to the ‘text’ method. The ‘JQueryStatic’ interface also contains a method, ‘get’, that performs an Ajax get operation on the provided URL and arranges to invoke the provided callback upon receipt of a response.

Finally, the ‘JQueryStatic’ interface contains a bare function signature

  1. (query: string): JQuery;

The bare signature indicates that instances of the interface are callable. This example illustrates that TypeScript function types are just special cases of TypeScript object types. Specifically, function types are object types that contain one or more call signatures. For this reason we can write any function type as an object type literal. The following example uses both forms to describe the same type.

  1. var f: { (): string; };
  2. var sameType: () => string = f; // Ok
  3. var nope: () => number = sameType; // Error: type mismatch

We mentioned above that the ‘$’ function behaves differently depending on the type of its parameter. So far, our jQuery typing only captures one of these behaviors: return an object of type ‘JQuery’ when passed a string. To specify multiple behaviors, TypeScript supports overloading of function signatures in object types. For example, we can add an additional call signature to the ‘JQueryStatic’ interface.

  1. (ready: () => any): any;

This signature denotes that a function may be passed as the parameter of the ‘$’ function. When a function is passed to ‘$’, the jQuery library will invoke that function when a DOM document is ready. Because TypeScript supports overloading, tools can use TypeScript to show all available function signatures with their documentation tips and to give the correct documentation once a function has been called with a particular signature.

A typical client would not need to add any additional typing but could just use a community-supplied typing to discover (through statement completion with documentation tips) and verify (through static checking) correct use of the library, as in the following screenshot.

  1.3 Object Types - 图1

Section 3.3 provides additional information about object types.