2013 1114 Typescript
2013 1114 Typescript
003
Scripting Languages
11/14/2013
TypeScript
Typing
Strong typing = no implicit type conversion
Weak typing = implicit type conversion
Static typing = check for type errors at compile time
Dynamic typing = check for type errors at run time
Unexpected Behavior
$ node $ node
> '5' + 2 > '' == '0'
'52' false
> '5' - 2 > 0 == ''
3 true
> 0 == '0'
true
Strong Typing
Makes intention explicit
Makes code easier to read/maintain
Can catch certain types of errors
Less likely to have unexpected behavior
primitive object
Output is JavaScript:
$ ls
hello.js hello.ts
Type Declarations
hello.ts hello.js
Type Declarations
hello.ts hello.js
Type Checking
hello.js output
var msg = 5;
printit(msg);
Type Checking
hello.js output
var msg = 5;
printit(msg);
Type Checking
hello.ts output
Object Types
fruit.ts
interface Fruit { An interface gives a
weight: number; name to an object type
color: string; Purely compile-time
seed?: boolean; construct
} Checks structural
equality
function pluck(f : Fruit) { Fields with a ? Are
console.log(f.color + " fruit"); optional.
}
Object Types
fruit.ts
interface Fruit { An interface gives a
weight: number; name to an object type
color: string; Purely compile-time
seed?: boolean; construct
} Checks structural
equality
function pluck(f : Fruit) { Fields with a ? Are
console.log(f.color + " fruit"); optional.
}
Classes
Class Type
Class Member
Class Inheritance
this
Modules
this
In a constructor, member function:
this is the class instance
In a static function:
this is constructor function
In a function declaration: this is Any
In the global module: this Any
Last Slide
Next week: Security!