TypeScript. Types. CS144: Web Applications

Similar documents
Mobile App:IT. Methods & Classes

LEARN WITH INTRODUCTION TO TYPESCRIPT

Nick Senger & Jesse van den Kieboom

Introduction to TypeScript

IN4MATX 133: User Interface Software

An Introduction to TypeScript. Personal Info

One language to rule them all: TypeScript. Gil Fink CEO and Senior Consultant, sparxys

Object Oriented Programming

Introduction to Programming Using Java (98-388)

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Inheritance and Polymorphism

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

TYPESCRIPT. Presented by Clarke Bowers

INF5750. Introduction to JavaScript and Node.js

Java: Comment Text. Introduction. Concepts

CS 2340 Objects and Design - Scala

Introduction to the SharePoint Framework Bob German Principal Architect - BlueMetal. An Insight company

Node.js. Node.js Overview. CS144: Web Applications

JavaScript: Sort of a Big Deal,

Sri Vidya College of Engineering & Technology

Lab 1 - Introduction to Angular

TypeScript coding JavaScript without the pain

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Lecture 02, Fall 2018 Friday September 7

C++ Important Questions with Answers

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

COMP 430 Intro. to Database Systems. SQL from application code

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

What is Inheritance?

Fundamental Concepts and Definitions

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Pace University. Fundamental Concepts of CS121 1

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Javascript Arrays, Object & Functions

Chapter 5. Names, Bindings, and Scopes

Fundamentos de programação

Class, Variable, Constructor, Object, Method Questions

Java Object Oriented Design. CSC207 Fall 2014

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

JAVA: A Primer. By: Amrita Rajagopal

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

One Framework. Angular

Object Model Comparisons

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Java Professional Certificate Day 1- Bridge Session

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Programming overview

Introduction to Programming

Computer Science is...

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Background Constructors (1A) Young Won Lim 1/22/18

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Introduction to Java

Background Constructors (1A) Young Won Lim 6/30/18

Building Your own Widget with ArcGIS API for JavaScript

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Implementing an ADT with a Class

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Object Oriented Design

CSC324 Principles of Programming Languages

Chapter 10 Introduction to Classes

Example: Fibonacci Numbers

Background Constructors (1A) Young Won Lim 7/5/18

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Section 2.2 Your First Program in Java: Printing a Line of Text

OBJECT ORIENTED PROGRAMMING

Selected Questions from by Nageshwara Rao

Packages and Information Hiding

Typestate and Session Types for Java

Protection Levels and Constructors The 'const' Keyword

Java 1.5 in a Nutshell

Chapter 4 Defining Classes I

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Lecture 18 Tao Wang 1

1.00 Lecture 8. Using An Existing Class, cont.

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Computer Components. Software{ User Programs. Operating System. Hardware

Objectives. Introduce static keyword examine syntax describe common uses

Programming - 2. Common Errors

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

IAT 355 : Lab 01. Web Basics

Chapter 1: A First Program Using C#

Object Oriented Programming

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Array. Prepared By - Rifat Shahriyar

Working with JavaScript

Chapter 5 Names, Binding, Type Checking and Scopes

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 2

Transcription:

TypeScript Superset of JavaScript (a.k.a. JavaScript++) to make it easier to program for largescale JavaScript projects New features: types, interfaces, decorators,... All additional TypeScript features are strictly optional and are not required Any JavaScript code is also a TypeScript code! Transpilation: TypeScript code is compiled to a JavaScript code using TypeScript compiler // --- hello. ts --- function hello ( name : string ): string return " Hello " + name ; console. log ( hello (" world!")); $ tsc hello. ts The above command runs the TypeScript compiler tsc on hello.ts and produces the hello.js file, which contains a standard JavaScript code. $ node hello. js Hello world! Types Types can be added to functions and variables as an intended contract function hello ( name : string ): string return " Hello " + name ; let user = [0, 1, 2]; hello ( user ); Junghoo "John" Cho (cho@cs.ucla.edu) 1

Compiler produces an error for the above code due to type mismatch $ tsc hello. ts hello. ts (6,33) : error TS2345 : Argument of type ' number [] ' is not assignable to parameter of type ' string '. Use any type to specifically indicate that any type is possible Use void as the return type of a function with no return value Q: Why would anyone want this? Compile-time error vs run-time error Rigidity vs flexibility Classes TypeScript allows explicit declaration of class properties, including public, private, protected access levels JavaScript syntax // JavaScript -- point. js constructor (x, y) TypeScript syntax x: number ; private y: number ; constructor (x, y) Junghoo "John" Cho (cho@cs.ucla.edu) 2

let p = new Point (10, 20) ; console. log (p.x); * Property with no access-level keyword becomes public Adding access-level keyword to constructor automatically adds the property constructor ( public x: number, private y: number ) let p = new Point (10, 20) ; console. log (p.x); This code is equivalent to the previous code Interfaces Like Java, TypeScript supports interfaces Two types are compatible if their internal structure is compatible We can implement an interface simply by having the needed structure of the interface, without an explicit implements clause interface Person firstname : string ; lastname : string ; function hello ( person : Person ) return " Hello, " + person. firstname + " " + person. lastname ; let user = firstname : " Jane ", lastname : " User " ; hello ( user ); No error in the above example because user is compatible with Person Junghoo "John" Cho (cho@cs.ucla.edu) 3

Generics Like Java generics, TypeScript allows creating generic functions/classes using parameterized types Example class Pair <T > x: T; y: T; constructor (x: T, y: T) let p = new Pair < number >(1, 2) ; function log <T >( arg : T) : void console. log ( arg ); log < number >(1) ; Decorators We can decorate classes, methods, properties, and parameters using a decorator Syntax: @decorator Example: @sealed // <- decorator class Greeter greeting : string ; constructor ( greeting : string ) this. greeting = greeting ; greet () return " Hello, " + this. greeting ; Junghoo "John" Cho (cho@cs.ucla.edu) 4

* Interpretation: objects of this class are sealed! * In JavaScript, sealing means no new property and method can be added and their attributes (such as enumerable, writable) cannot be changed Technically, decorators are functions that modify JavaScript classes, properties, methods, and parameters General syntax for decorator: @expression expression must be (or evaluate to) a function, and it will be called at runtime with the decorated entity as its parameter(s) * Class decorators get the constructor of the class as its parameter Example: possible implementation of the above @sealed decorator: function sealed ( constructor : Function ) Object. seal ( constructor ); // seal the constructor Object. seal ( constructor. prototype ) // seal its prototype * This example effectively seals any object of the class Junghoo "John" Cho (cho@cs.ucla.edu) 5