GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing maker.makecode.com — Interfaces InterfacesThe easiest way to see how interfaces work is to start with a simple example: The type-checker checks the call to We can write the same example again, this time using an interface to describe the requirement of having the The interface It's worth pointing out that the type-checker does not require that these properties come in any sort of order, only that the properties the interface requires are present and have the required type. Optional PropertiesNot all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional properties are popular when creating patterns like "option bags" where you pass an object to a function that only has a couple of properties filled in. Here's an example of this pattern: Interfaces with optional properties are written similar to other interfaces, with each optional property denoted by a The advantage of optional properties is that you can describe these possibly available properties while still also preventing use of properties that are not part of the interface.
For example, had we mistyped the name of the Excess Property ChecksIn our first example using interfaces, TypeScript lets us pass However, combining the two naively would let you to shoot yourself in the foot the same way you might in JavaScript.
For example, taking our last example using Notice the given argument to You could argue that this program is correctly typed, since the However, TypeScript takes the stance that there's probably a bug in this code. Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments. If an object literal has any properties that the "target type" doesn't have, you'll get an error. Function TypesInterfaces are capable of describing the wide range of shapes that JavaScript objects can take. In addition to describing an object with properties, interfaces are also capable of describing function types. To describe a function type with an interface, we give the interface a call signature. This is like a function declaration with only the parameter list and return type given. Each parameter in the parameter list requires both name and type. Once defined, we can use this function type interface like we would other interfaces. Here, we show how you can create a variable of a function type and assign it a function value of the same type. For function types to correctly type-check, the names of the parameters do not need to match. We could have, for example, written the above example like this: Function parameters are checked one at a time, with the type in each corresponding parameter position checked against each other.
If you do not want to specify types at all, TypeScript's contextual typing can infer the argument types since the function value is assigned directly to a variable of type Note that you can also give a function type (without using an interface) equivalently as follows: Class TypesImplementing an interfaceOne of the most common uses of interfaces in languages like C# and Java, that of explicitly enforcing that a class meets a particular contract, is also possible in TypeScript. You can also describe methods in an interface that are implemented in the class, as we do with Interfaces describe the public side of the class, rather than both the public and private side. This prohibits you from using them to check that a class also has particular types for the private side of the class instance. Extending InterfacesLike classes, interfaces can extend each other. This allows you to copy the members of one interface into another, which gives you more flexibility in how you separate your interfaces into reusable components. An interface can extend multiple interfaces, creating a combination of all of the interfaces. Hybrid TypesStatic TypeScript does not currently permit hybrid types, as discussed below. As we mentioned earlier, interfaces can describe the rich types present in real world JavaScript. Because of JavaScript's dynamic and flexible nature, you may occasionally encounter an object that works as a combination of some of the types described above. One such example is an object that acts as both a function and an object, with additional properties: When interacting with 3rd-party JavaScript, you may need to use patterns like the above to fully describe the shape of the type. Interfaces Extending ClassesStatic TypeScript does not currently permit interfaces extending classes, as discussed below. When an interface type extends a class type it inherits the members of the class but not their implementations. It is as if the interface had declared all of the members of the class without providing an implementation. Interfaces inherit even the private and protected members of a base class. This means that when you create an interface that extends a class with private or protected members, that interface type can only be implemented by that class or a subclass of it. This is useful when you have a large inheritance hierarchy, but want to specify that your code works with only subclasses that have certain properties. The subclasses don't have to be related besides inheriting from the base class. For example: In the above example, Within the |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |