Skip to content

Commit 4279ece

Browse files
committed
no message
1 parent 966efbd commit 4279ece

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

GLOSSARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Duck Typing
2+
If it walks like a duck and quacks like a duck, its a duck. For TypeScript if it has all the members structurally then it okay for other things (irrespecitive of name) that accept that structure.

code/testing.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var point2D = {
2+
x: 0,
3+
y: 10,
4+
};
5+
var point3D = {
6+
x: 0,
7+
y: 10,
8+
z: 20
9+
};
10+
function iTakePoint2D(point) {
11+
}
12+
iTakePoint2D(point2D);
13+
iTakePoint2D(point3D);
14+
iTakePoint2D({
15+
x: 0
16+
});

code/testing.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface Point2D {
2+
x: number;
3+
y: number;
4+
}
5+
interface Point3D {
6+
x: number;
7+
y: number;
8+
z: number;
9+
}
10+
var point2D: Point2D = { x: 0, y: 10, }
11+
var point3D: Point3D = { x: 0, y: 10, z: 20 }
12+
function iTakePoint2D(point: Point2D) { /* do something */ }
13+
14+
iTakePoint2D(point2D); // exact match okay
15+
iTakePoint2D(point3D); // extra information okay
16+
iTakePoint2D({ x: 0 }); // Error: missing information `y`

code/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "1.4.1",
3+
"compilerOptions": {
4+
"target": "es5",
5+
"module": "commonjs",
6+
"declaration": false,
7+
"noImplicitAny": false,
8+
"removeComments": true,
9+
"noLib": false
10+
},
11+
"filesGlob": [
12+
"./**/*.ts",
13+
"!./node_modules/**/*.ts"
14+
],
15+
"files": [
16+
"./testing.ts"
17+
]
18+
}

docs/why-typescript.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,22 @@ We will discuss all the details of all the annotation methods supported by TypeS
4747
In some languages (specifically nominally typed ones) static typing results in unnecessary ceremony because even though *you know* that the code will work fine the language semantics force you to copy stuff around. This is why stuff like [automapper for C#](http://automapper.org/) exists. In TypeScript because we really want it to be easy for JavaScript developers, and be minimum cognitive overload types are *structural*. This means that *duck typing* is a first class language construct. An example is in order:
4848

4949
```ts
50-
interface Foo{
51-
name: string;
50+
interface Point2D {
51+
x: number;
52+
y: number;
5253
}
53-
interfaace Bar{
54-
name: string;
55-
class: number;
54+
interface Point3D {
55+
x: number;
56+
y: number;
57+
z: number;
5658
}
59+
var point2D: Point2D = { x: 0, y: 10, }
60+
var point3D: Point3D = { x: 0, y: 10, z: 20 }
61+
function iTakePoint2D(point: Point2D) { /* do something */ }
62+
63+
iTakePoint2D(point2D); // exact match okay
64+
iTakePoint2D(point3D); // extra information okay
65+
iTakePoint2D({ x: 0 }); // Error: missing information `y`
5766
```
5867

5968

0 commit comments

Comments
 (0)