23
CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CS 5142 Cornell University 11/14/13

1

CSCI-GA.3033.003 Scripting Languages

11/14/2013 TypeScript

Page 2: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.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

CS 5142 Cornell University 11/14/13

2

Concepts

Page 3: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CS 5142 Cornell University 11/14/13

3

Weak/Strong, Static/Dynamic Typing Concepts

ML Java

JavaScript PHP

Scheme

Perl assembler

C

Static typing (compile-time checks)

Strong typing (explicit conversions)

Weak typing (implicit conversions)

Dynamic typing (runtime checks)

VBA

Page 4: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Unexpected Behavior

$ node > '5' + 2 '52' > '5' - 2 3

CS 5142 Cornell University 11/14/13

4

$ node > '' == '0' false > 0 == '' true > 0 == '0' true

JavaScript

Page 5: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Strong Typing •  Makes intention explicit •  Makes code easier to read/maintain •  Can catch certain types of errors •  Less likely to have “unexpected” behavior

CS 5142 Cornell University 11/14/13

5

Concepts

Page 6: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CS 5142 Cornell University 11/14/13

6

About TypeScript •  Superset of JavaScript

–  Static typing –  Class-based object-oriented programming

•  Developed by Microsoft •  Designed to make it easier to build large

JavaScript applications •  Related languages:

–  CoffeeScript –  Dart –  Mascara

TypeScript

Page 7: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CS 5142 Cornell University 10/07/13

7

Types TypeScript

primitive

number string boolean trivial

null undefined

object

array named anonymous …

enum

any

Page 8: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CS 5142 Cornell University 11/14/13

8

Install as a node.js package: $ npm install -g typescript Run the compiler: $ tsc hello.ts Output is JavaScript: $ ls hello.js hello.ts

TypeScript

How to Write + Run Code

Page 9: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Type Declarations

function printit(msg : string) { console.log(msg); } var msg = "Hello World!"; printit(msg);

CS 5142 Cornell University 11/14/13

9

function printit(msg) { console.log(msg); } var msg = "Hello World!"; printit(msg);

hello.ts hello.js

TypeScript

Page 10: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Type Declarations

function printit(msg : string) { console.log(msg); } var msg = "Hello World!"; printit(msg);

CS 5142 Cornell University 11/14/13

10

function printit(msg) { console.log(msg); } var msg = "Hello World!"; printit(msg);

hello.ts hello.js

TypeScript

Page 11: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Type Checking

function printit(msg : string) { console.log(msg); } var msg = 5; printit(msg);

CS 5142 Cornell University 11/14/13

11

$ node hello.js 5

hello.js output

TypeScript

Page 12: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Type Checking

function printit(msg : string) { console.log(msg); } var msg = 5; printit(msg);

CS 5142 Cornell University 11/14/13

12

$ node hello.js 5

hello.js output

TypeScript

Page 13: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Type Checking

function printit(msg : string) { console.log(msg); } var msg = 5; printit(msg);

CS 5142 Cornell University 11/14/13

13

$ tsc hello.ts hello.ts(6,1): error TS2081: Supplied parameters do not match any signature of call target.

hello.ts output

TypeScript

Page 14: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Object Types

interface Fruit { weight: number; color: string; seed?: boolean; } function pluck(f : Fruit) { console.log(f.color + " fruit"); } var x = {weight: 10, color: "red"}; pluck(x);

CS 5142 Cornell University 11/14/13

14

•  An interface gives a name to an object type

•  Purely compile-time construct

•  Checks structural equality

•  Fields with a ? Are optional.

fruit.ts

TypeScript

Page 15: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Object Types

interface Fruit { weight: number; color: string; seed?: boolean; } function pluck(f : Fruit) { console.log(f.color + " fruit"); } var x = {weight: 10, color: "red"}; pluck(x);

CS 5142 Cornell University 11/14/13

15

•  An interface gives a name to an object type

•  Purely compile-time construct

•  Checks structural equality

•  Fields with a ? Are optional.

fruit.ts

TypeScript

Page 16: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Classes

class Apple { constructor(public weight, public color) { } public pluck() { return this.color + " apple”; } }

CS 5142 Cornell University 11/14/13

16

•  Introduces a named type and a member

•  A declaration includes a type declaration and a constructor

TypeScript

Page 17: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Class Type

class Apple { constructor(public weight : number, public color : string) { } public pluck() { return this.color + " apple”; } }

CS 5142 Cornell University 11/14/13

17

Interface Apple { weight : number; color : string; pluck() : string; }

TypeScript

Page 18: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Class Member

var Apple: { new(weight : number, color : string) : Apple; }

CS 5142 Cornell University 11/14/13

18

•  This is the JavaScript constructor function

TypeScript

Page 19: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Class Inheritance

class A { a: number; public f() {console.log("a"); } public g() {console.log("a"); } } class B extends A { b: string; public g() {console.log("b"); } } var b = new B(); b.f(); // a

CS 5142 Cornell University 11/14/13

19

•  A derived class inherits all members from its base class if it does not override them

•  Members with the same name and type are overridden

TypeScript

Page 20: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

this

class A { a: number; public f() {console.log("a"); } public g() {console.log(a"); } } class B extends A { b: string; public g() {console.log("b"); } } var b = new B(); b.f(); // a

CS 5142 Cornell University 11/14/13

20

•  A derived class inherits all members from its base class if it does not override them

•  Members with the same name and type are overridden

TypeScript

Page 21: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

Modules

module M { export function f() { return "f"; } function g() { return "g"; } } M.f(); M.g(); // Error, g is not exported

CS 5142 Cornell University 11/14/13

21

•  Introduces namespaces •  Provide encapsulation •  Implemented using the

same “pattern” as node modules

TypeScript

Page 22: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

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

CS 5142 Cornell University 11/14/13

22

TypeScript

Page 23: CSCI-GA.3033.003 Scripting Languages · CS 5142 Cornell University 11/14/13 1 CSCI-GA.3033.003 Scripting Languages 11/14/2013 TypeScript

CS 5142 Cornell University 11/14/13

23

Last Slide •  Next week: Security!

Administrative