TypeScript. Jonathan Kula 12/3/2018

Similar documents
IN4MATX 133: User Interface Software

YEAH Hours Assignment 6: Adventure. 11/27/2018 Jonathan Kula

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Unit 5: More on Classes/Objects Notes

Java Programming. Atul Prakash

Variables and Typing

Object Oriented Programming

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Introduction to Java Unit 1. Using BlueJ to Write Programs

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Basic Keywords Practice Session

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC ELEMENTS OF A COMPUTER PROGRAM

YEAH Hours: Enigma. 11/6/18 Ryan Eberhardt

Tools. SWE 432, Fall Design and Implementation of Software for the Web

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CompSci 125 Lecture 02

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

JavaScript Basics. The Big Picture

Program Fundamentals

Using EnScript to Make Your Life Easier Session 1. Suzanne Widup, James Habben, Bill Taroli

Interfaces. Readings and References. Recall the discussion of ArrayLists. How can we manage lists of objects? Reading

Interfaces. CSE 142, Summer 2002 Computer Programming 1.

CS 1331 Exam 1 ANSWER KEY

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Intro. Scheme Basics. scm> 5 5. scm>

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Text Input and Conditionals

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS106AJ Midterm Review Session. October 28, 2017 Kat Gregory and Ryan Eberhardt

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

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

Week 3. This is CS50. Harvard University. Fall Cheng Gong

CISC 1600 Lecture 3.1 Introduction to Processing

Using the Zoo Workstations

LEARN WITH INTRODUCTION TO TYPESCRIPT

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Fundamentals of Programming CS-110. Lecture 2

CS2500 Exam 2 Fall 2011

To practice overall problem-solving skills, as well as general design of a program

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

15-122: Principles of Imperative Computation

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Static Methods. Why use methods?

Important From Last Time

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

TypeScript. Types. CS144: Web Applications

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Anjuli Kannan. Google Earth Driving Simulators (3:00-7:00)

Assignment #5 The Enigma Machine

Chapter 5 Lab Methods

Getting Started. 1 by Conner Irwin

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

An Introduction to TypeScript. Personal Info

CSC209. Software Tools and Systems Programming.

Shorthand for values: variables

Database Use & Design

Programming. Syntax and Semantics

do fifty two: Language Reference Manual

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

Chapter 5 Lab Methods

Introduction to Object-Oriented Programming

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

CSC209. Software Tools and Systems Programming.

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

C++ Arrays and Vectors

Chapter 11-B Homework ScalaFX & Eclipse Individual Assignment 25 Points

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Pointers in C/C++ 1 Memory Addresses 2

Teaching guide: Data structures (Twodimensional

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Setting Up the Fotosizer Software

Adding content to your Blackboard 9.1 class

Variables and Constants

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Preprocessor Directives

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

SCHEME AND CALCULATOR 5b

Transcription:

TypeScript Jonathan Kula 12/3/2018

Introducing TypeScript A superset of JavaScript. This means that all JavaScript code is valid TypeScript code! TypeScript just adds some new features that will make your life easier. TypeScript adds a step between code and browser that checks your code for consistency. (This is called the compiler.) TypeScript also adds additional syntax so you can tell the compiler what you re trying to do; then, it ll try to help you do that. It s all about making your code more consistent.

function makecircle(radius, x, y, gw) { let oval = GOval(x, y, radius * 2, radius * 2); gw.add(oval); } let gw = GWindow(400, 400); makecircle(50, 200, 200);

function makecircle(radius, x, y, gw) { let oval = GOval(x, y, radius * 2, radius * 2); gw.add(oval); } let gw = GWindow(400, 400); makecircle(50, 200, 200);

function createsquare(config) { return { width: config.width, height: config.height, color: config.color, area: config.width * config.height, perimeter: config.width * 2 + config.height * 2 } } console.log("creating square with color green!"); let square = createsquare({width: 2, height: 2, colour: "Green"}); console.log("the new square's color is: " + square.color);

function createsquare(config) { return { width: config.width, height: config.height, color: config.color, area: config.width * config.height, perimeter: config.width * 2 + config.height * 2 } } console.log("creating square with color green!"); let square = createsquare({width: 2, height: 2, colour: "Green"}); console.log("the new square's color is: " + square.color);

Type Annotation let variablename: TypeName; const CONSTANT_NAME: TypeName; function functionname(param1: Type1, param2: Type2): ReturnType {

Type Annotation let age: number = 20; const ALPHABET: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; function divides(divisor: number, dividend: number): boolean {

Type Inference let foo = "Hello"; let foo: string = "Hello"; These statements are equivalent because of type inference.

Type Inference let foo = 1; let foo: string = 1; These statements are not equivalent because of type inference. (The second statement throws an error; 1 is not a string!)

What types are there? Here are types you ve worked with! number string boolean null undefined object function any

What types are there? Here are types you ve worked with! number string boolean null undefined object function any Primitive Types: simple types You build all other types out of primitive types.

What types are there? Here are types you ve worked with! number string boolean null undefined object function any Primitive Types: simple types You build all other types out of primitive types. Non-Primitive Types: Everything else.

What types are there? Here are types you ve worked with! number string boolean null undefined object function any Primitive Types: simple types You build all other types out of primitive types. Non-Primitive Types: Everything else. any: A special type that can represent anything!

What types are there? Here are types you ve worked with! number string boolean null undefined object function any And some types you haven t. symbol ask me about these after class! never Primitive Types: simple types You build all other types out of primitive types. Non-Primitive Types: Everything else. any: A special type that can represent anything!

What types are there? Here are types you ve worked with! number string boolean null undefined object function any Primitive Types: simple types You build all other types out of primitive types. Non-Primitive Types: Everything else. any: A special type that can represent anything!

What types are there? Here are types you ve worked with! number string boolean null undefined object Seems like this describes an awful lot... function any

let dog = { type: 'mammal', name: 'dog', sounds: ['woof', 'bark', 'yip', 'ruff'] }; let cat = { type: 'mammal', name: 'cat', sounds: ['meow', 'purr', 'hiss'] }; let enigma = { rotors: [], lamps: [], keys: [] }; let key = {}; key.letter = "A"; key.mousedownaction = function () { }; let jonathan = { favoritecolor: "Green", name: "Jonathan Kula", status: "Active", classes: [ { name: "CS106AJ", role: "SL", grade: -1 }, { name: "CS103", role: "Student", grade: 87.5 } ] }; let profile = { name: "Jonathan Kula", imageurl: "http://image.url/img.png", language: "English" };

Interfaces Interfaces describe the structure of objects.

Interfaces Interfaces describe the structure of objects. Interfaces are not objects. Interfaces have no functionality they only describe other objects. interface InterfaceName { property1: Type1 property2: Type2 }

Interfaces Interfaces describe the structure of objects. Interfaces are not objects. Interfaces have no functionality they only describe other objects. interface Point { x: number y: number }

Function Annotations What if we wanted to make an interface for an Enigma key?

Function Annotations What if we wanted to make an interface for an Enigma key? interface WithFunction { func: (param1: Type1, param2: Type2) => ReturnType }

Function Annotations What if we wanted to make an interface for an Enigma key? interface Key { letter: string onmousedown: () => void } void is a special type meaning doesn t return anything

Map Annotations What about using objects as maps?

Map Annotations What about using objects as maps? interface Phonebook { [name: KeyType]: ValueType } The KeyType can be either string or number.

Map Annotations What about using objects as maps? interface Phonebook { [name: string]: string } The KeyType can be either string or number.

Classes in TypeScript Think of them like Interfaces with functionality You use class-like factory functions in Teaching Machine, Adventure, and when coding using object-oriented ideas. Classes are types too, much like interfaces!

Classes in TypeScript Make an object of a class by using the new keyword. Refer to properties of the class using the this keyword. this inside a class refers to the current object. let jonathan = new Profile("Jonathan Kula", "http://image.url/", "English"); let ryan = new Profile("Ryan Eberhardt", "http://image.url/", "English"); jonathan.getname(); // this now refers to jonathan returns Jonathan Kula ryan.getname(); // this now refers to ryan returns Ryan Eberhardt

Acquiring Typescript Download nodejs LTS from https://nodejs.org/en/ Open a Powershell (Windows) or Terminal (macos or Linux) Type npm install -g typescript

Setting Up Typescript Download TypeScript configuration file from the course website. I can break it down after class if you re interested! Put that file in your project folder.

Using TypeScript Manually: Open Powershell/Terminal, go to your project directory using cd, then type tsc to build all.ts files into.js files! Better: Get an IDE that supports TypeScript! I use both WebStorm and Visual Studio Code. I prefer WebStorm, but it s only free while you re a student. Visual Studio Code is also quite good, and free. I have a slide deck about how to acquire WebStorm here.