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. name: string;
    2. favoriteColor?: string;
    3. }
    4. function add(friend: Friend) {
    5. var name = friend.name;
    6. }
    7. add({ favoriteColor: "blue" }); // Error, name required
    8. 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.

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

    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.

    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;

    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.

      

    Section provides additional information about object types.