Skip to content

Commit 6e511e7

Browse files
committed
Add documentation for implements in the class chapter
1 parent f6958b6 commit 6e511e7

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

pages/Classes.md

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var greeter = new Greeter("world");
2323
```
2424

2525
The syntax should look very familiar if you've used C# or Java before.
26-
We declare a new class `Greeter`. This class has three members, a property called `greeting`, a constructor, and a method `greet`.
26+
We declare a new class `Greeter`. This class has three members, a property called `greeting`, a constructor, and a method `greet`.
2727

2828
You'll notice that in the class when we refer to one of the members of the class we prepend `this.`.
2929
This denotes that it's a member access.
@@ -111,7 +111,7 @@ new Animal("Cat").name; // Error: 'name' is private;
111111
```
112112

113113
TypeScript is a structural type system.
114-
When we compare two different types, regardless of where they came from, if the types of each member are compatible, then we say the types themselves are compatible.
114+
When we compare two different types, regardless of where they came from, if the types of each member are compatible, then we say the types themselves are compatible.
115115

116116
However, when comparing types that have `private` and `protected` members, we treat these types differently.
117117
For two types to be considered compatible, if one of them has a `private` member, then the other must have a `private` member that originated in the same declaration.
@@ -131,7 +131,7 @@ class Rhino extends Animal {
131131

132132
class Employee {
133133
private name: string;
134-
constructor(theName: string) { this.name = theName; }
134+
constructor(theName: string) { this.name = theName; }
135135
}
136136

137137
var animal = new Animal("Goat");
@@ -220,7 +220,7 @@ if (employee.fullName) {
220220
}
221221
```
222222

223-
While allowing people to randomly set fullName directly is pretty handy, this might get us in trouble if we people can change names on a whim.
223+
While allowing people to randomly set fullName directly is pretty handy, this might get us in trouble if we people can change names on a whim.
224224

225225
In this version, we check to make sure the user has a secret passcode available before we allow them to modify the employee.
226226
We do this by replacing the direct access to fullName with a `set` that will check the passcode.
@@ -235,7 +235,7 @@ class Employee {
235235
get fullName(): string {
236236
return this._fullName;
237237
}
238-
238+
239239
set fullName(newName: string) {
240240
if (passcode && passcode == "secret passcode") {
241241
this._fullName = newName;
@@ -307,8 +307,8 @@ alert(greeter.greet());
307307
```
308308

309309
Here, when we say `var greeter: Greeter`, we're using `Greeter` as the type of instances of the class `Greeter`.
310-
This is almost second nature to programmers from other object-oriented languages.
311-
310+
This is almost second nature to programmers from other object-oriented languages.
311+
312312
We're also creating another value that we call the *constructor function*.
313313
This is the function that is called when we `new` up instances of the class.
314314
To see what this looks like in practice, let's take a look at the JavaScript created by the above example:
@@ -390,3 +390,45 @@ interface Point3d extends Point {
390390
var point3d: Point3d = {x: 1, y: 2, z: 3};
391391
```
392392

393+
## Implementing interfaces
394+
A class declaration can also implement interfaces in the `implements` clause.
395+
The class needs to provide an implementation for the properties of every interface in the `implements` clause.
396+
397+
```ts
398+
interface Kid {
399+
name: string;
400+
age: number;
401+
learn: () => void;
402+
}
403+
404+
interface Programmer {
405+
favoriteLanguage: string;
406+
}
407+
408+
class CoolKid implements Kid, Programmer {
409+
constructor(public name: string, public age: number) { }
410+
411+
public favoriteLanguage = "TypeScript";
412+
public learn() { console.log("Just learned more about TypeScript class!"); }
413+
}
414+
```
415+
416+
There are several tips regaruding using `implements` for interfaces and classes declarations:
417+
418+
1. The `implements` can only be used in class declarations, and it can be followed by both classes and interfaces.
419+
In comparison, `extends` can be used in both class and interface declarations.
420+
However when used in an interface declaration, `extends` can be followed by interfaces or classes; when in class declaration, `extends` can only be followed by classes.
421+
422+
2. The derived class type must be assignable to the interface or classes it implements.
423+
For example, a class cannot implement an interface with a call signature, because the class won't be assignable to the interface type.
424+
```ts
425+
interface foo {
426+
(): void;
427+
}
428+
429+
class Bar implements foo {
430+
// Error: Class 'Bar' incorrectly implements interface foo.
431+
}
432+
```
433+
434+
More details about class heritage can be found in the [specification](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#811-class-heritage-specification).

0 commit comments

Comments
 (0)