-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtype-compatibility.ts
86 lines (64 loc) · 1.82 KB
/
type-compatibility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// -- Structural typing --
// x is compatible with y if y has at least the same members as x.
{
interface Named {
name: string;
}
class Person {
name: string;
}
let p: Named;
// OK, because of structural typing - We have the same number of properties, of the same name andof the same type.
p = new Person();
//In nominally-typed languages like C# or Java, the equivalent code would be an error
//because the Person class does not explicitly describe itself as being an implementer of the Named interface.
}
// -- Comparing two functions --
// Parameter types
{
let x = (a: number) => 0;
let y = (b: number, s: string) => 0;
y = x; // OK
x = y; // Error
}
// Return types
{
let x = () => ({name: "Alice"});
let y = () => ({name: "Alice", location: "Seattle"});
x = y; // OK
y = x; // Error, because x() lacks a location property
}
// Optional parameters and rest parameters
{
function invokeLater(args: any[], callback: (...args: any[]) => void) {
/* ... Invoke callback with 'args' ... */
}
// Unsound - invokeLater "might" provide any number of arguments
invokeLater([1, 2], (x, y) => console.log(x + ", " + y));
// Confusing (x and y are actually required) and undiscoverable
invokeLater([1, 2], (x?, y?) => console.log(x + ", " + y));
}
// -- Enums --
{
enum Status { Ready, Waiting };
enum Color { Red, Blue, Green };
let status = Status.Ready;
status = Color.Green; //error
let x1: number = Status.Ready;
let x2: number = Color.Green;
}
// -- Generics --
{
interface Empty<T> {
}
let x1: Empty<number>;
let y1: Empty<string>;
x1 = y1; // okay, y matches structure of x1
// with specified Generic arguments
interface NotEmpty<T> {
data: T;
}
let x2: NotEmpty<number>;
let y2: NotEmpty<string>;
x2 = y2; // error, x and y are not compatible
}