You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/Classes.md
+49-7Lines changed: 49 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ var greeter = new Greeter("world");
23
23
```
24
24
25
25
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`.
27
27
28
28
You'll notice that in the class when we refer to one of the members of the class we prepend `this.`.
29
29
This denotes that it's a member access.
@@ -111,7 +111,7 @@ new Animal("Cat").name; // Error: 'name' is private;
111
111
```
112
112
113
113
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.
115
115
116
116
However, when comparing types that have `private` and `protected` members, we treat these types differently.
117
117
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.
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