TypeScript. Часть II. Старков Дима

Similar documents
Nick Senger & Jesse van den Kieboom

ОБЪЕКТНО- ОРИЕНТИРОВАННОЕ ПРОГРАММИРОВАНИЕ. Лекция 1 / г.

JavaScript Lecture 2

TypeScript. Jonathan Kula 12/3/2018

COMP519 Web Programming Lecture 14: JavaScript (Part 5) Handouts

An Introduction to TypeScript. Personal Info

Client-Side Web Technologies. JavaScript Part I

JavaScript: Sort of a Big Deal,

TypeScript. Types. CS144: Web Applications

Symbols. accessor properties, attributes, creating, adding properties, 8 anonymous functions, 20, 80

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

Principles of Programming Languages

Карта «Кофейные регионы Эфиопии» Коллеги из Trabocca любезно предоставили нам карту кофейных регионов Эфиопии, за что

COMPUTER APPLICATIONS

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance (Outsource: )

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

3/6/2018 Spectacle PARALLELIZING PRODUCT DEVELOPMENT WITH GRAPHQL.

Announcements/Follow-ups

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Introduction to Java

TypeScript für Fortgeschrittene

Object Oriented Programming Part I of II. Steve Ryder 08/22/05, Session 8351 JSR Systems (JSR)

TypeScript. TypeScript. RxJS

AP CS Unit 6: Inheritance Notes

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

ANSWER KEY. Study Guide. Completely fill in the box corresponding to your answer choice for each question.

Java Programming. Atul Prakash

AP CS Unit 6: Inheritance Exercises

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

TypeScript. Language Specification. Version 1.8

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Introduction to Web Tech and Programming

CS 1331 Exam 1 ANSWER KEY

JScript Reference. Contents

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

B l o c k B i n d i n g s

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

ADsafety. Type-based Verification of JavaScript Sandboxing. Joe Gibbs Politz Spiridon Aristides Eliopoulos Arjun Guha Shriram Krishnamurthi

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Typen nach JavaScript tragen TypeScript. Johannes Dienst

Common Misunderstandings from Exam 1 Material

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

AP CS Unit 8: Inheritance Exercises

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CSC 1214: Object-Oriented Programming

Ruby: Objects and Dynamic Types

Lecture 10. Overriding & Casting About

The results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

CS-202 Introduction to Object Oriented Programming

JavaScript for C# Programmers Kevin

MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output

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

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

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

Informatik II (D-ITET) Tutorial 6

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

What if Type Systems were more like Linters?

Experimental New Directions for JavaScript

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

Variables and Typing

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

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Crash Course Review Only. Please use online Jasmit Singh 2

JavaScript: Features, Trends, and Static Analysis

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

Objects. say something to express one's disapproval of or disagreement with something.

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Principles of Programming Languages

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

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

Ch02. True/False Indicate whether the statement is true or false.

Checking Multiple Conditions

Name:... ID:... class A { public A() { System.out.println( "The default constructor of A is invoked"); } }

CSC Java Programming, Fall Java Data Types and Control Constructs

UMBC CMSC 331 Final Exam

Building Your own Widget with ArcGIS API for JavaScript

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

JavaScript CS 4640 Programming Languages for Web Applications

iwiki Documentation Release 1.0 jch

Variable Declarations

Create a Questionnaire. Triangle

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS313D: ADVANCED PROGRAMMING LANGUAGE

Classes vs Simple Object Delegation In JavaScript

Proxies Design Principles for Robust OO Intercession APIs

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Computer Science II (20082) Week 1: Review and Inheritance

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Transcription:

TypeScript Часть II Старков Дима 1

Сегодня Вывод типов Структурная типизация Более сложные типы Обобщенные типы Type Guards 2

TypeScript? Спасет от выстрелов себе в ногу ESNext прямо сейчас Средство против TypeError Пишет код за вас Документация к коду Но... 3

4

Вывод типов let n: number = 42 let s: string = 'Hello, world!' let a: number[] = [1, 2, 3, 4] let n = 42 let s = 'Hello, world!' let a = [1, 2, 3, 4] 5

Наиболее общий тип let shapes = [new Circle(), new Square()] shapes.push(new Triangle()) 6

Наиболее общий тип let shapes = [new Circle(), new Square()] // Argument of type 'Triangle' // is not assignable to parameter of type 'Square Circle'. shapes.push( new Triangle() ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7

Наиболее общий тип let shapes = [new Circle(), new Square()] shapes.push(new Triangle()) 8

Наиболее общий тип let shapes = [new Circle(), new Square()] shapes.push(new Triangle()) 9

Наиболее общий тип let shapes = [new Circle(), new Square()] shapes.push(new Triangle()) 10

Наиболее общий тип let shapes = [new Circle(), new Square()] // Argument of type 'Triangle' // is not assignable to parameter of type 'Square Circle'. shapes.push( new Triangle() ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 11

Наиболее общий тип let shapes: Shape[] = [new Circle(), new Square()] shapes.push(new Triangle()) 12

Совместимость типов class Human { name: string class Robot { name: string let human: Human = new Robot() 13

Проверка типа в runtime function addshape(shapes: Shape[], obj: object) { if (obj instanceof Shape) { shapes.push(obj as Shape) throw new TypeError('Argument is not instanceof Shape') 14

Проверка типа в runtime function addshape(shapes: Shape[], obj: object) { if (obj instanceof Shape) { shapes.push(obj as Shape) throw new TypeError('Argument is not instanceof Shape') 15

Проверка типа в runtime function addshape(shapes: Shape[], obj: object) { if (obj instanceof Shape) { shapes.push(obj as Shape) throw new TypeError('Argument is not instanceof Shape') 16

Проверка типа в runtime function addshape(shapes: Shape[], obj: object) { if (obj instanceof Shape) { shapes.push(obj as Shape) throw new TypeError('Argument is not instanceof Shape') 17

Проверка типа в runtime function addshape(shapes: Shape[], obj: object) { if (obj instanceof Shape) { shapes.push(obj as Shape) throw new TypeError('Argument is not instanceof Shape') 18

Type Guard function addshape(shapes: Shape[], obj: object) { if (obj instanceof Shape) { shapes.push(obj) throw new TypeError('Argument is not instanceof Shape') 19

TypeScript крут. Но можем ли мы описать весь JavaScript? 20

Вспомним TypeScript 1.0 Интерфейсы Классы Обобщенные типы Перегрузки функций Чего еще желать? 21

// String.split split(separator:?, limit: number): string[] 22

// String.split split(separator: string RegExp, limit: number): string[] 23

// String.split split(separator: string RegExp, limit: number): string[] Решение: Union Types 24

Union Type Guard function negate(n: string number) { if (typeof n === 'string') { return '-'.concat(n); else { return -n; 25

Union Type Guard function negate(n: string number) { if (typeof n === 'string') { return '-'.concat(n); else { return -n; 26

Union Type Guard function negate(n: string number) { if (typeof n === 'string') { return '-'.concat(n); else { return -n; 27

Union Type Guard function negate(n: string number) { if (typeof n === 'string') { return '-'.concat(n); else { return -n; 28

Union Type Guard function negate(n: string number) { if (typeof n === 'string') { return '-'.concat(n); return -n; 29

Intersection Types type Cat = { purr() 30

Intersection Types type Cat = { purr() type Dog = { woof() 31

Intersection Types type Cat = { purr() type Dog = { woof() type CatDog = Cat & Dog 32

Type Alias // String.split split(separator: string RegExp, limit: number): string[] 33

Type Alias type StringOrRegExp = string RegExp // String.split split(separator: StringOrRegExp, limit: number): string[] 34

Type vs Interface type Point = { x: number y: number interface Point { x: number y: number implements interface Type1 Type2 35

Тип Множество Можем объединять типы Можем пересекать типы & Можем вычитать из одного типа другой Фух, теперь точно всё... 36

А вот и нет! function get(obj, keyname) { return obj[keyname] function get(obj: any, keyname: string): any { return obj[keyname] // TypeError: Cannot read property 'prototype' of null get(null, 'prototype') 37

А вот и нет! function get(obj, keyname) { return obj[keyname] function get(obj: any, keyname: string): any { return obj[keyname] // TypeError: Cannot read property 'prototype' of null get(null, 'prototype') 38

А вот и нет! function get(obj, keyname) { return obj[keyname] function get(obj: any, keyname: string): any { return obj[keyname] // TypeError: Cannot read property 'prototype' of null get(null, 'prototype') 39

А вот и нет! function get(obj, keyname) { return obj[keyname] function get(obj: any, keyname: string): any { return obj[keyname] // TypeError: Cannot read property 'prototype' of null get(null, 'prototype') 40

Нужно уметь обрабатывать значения разных типов идентичным образом Кажется нам нужен... 41

42

Обобщенные типы function identity(arg: any): any { return arg; 43

Обобщенные типы function identity<t>(arg: T): T { return arg; 44

Обобщенные функции function identity<t>(arg: T): T { return arg; identity('string') // T is string identity(12131415) // T is number identity([4, 8, 15, 16, 23, 42]) // T is number[] 45

Встроенные обобщенные типы const fib: Array<number> = [1, 1, 2, 3, 5] // Argument of type 'string' // is not assignable to parameter of type 'number'. fib.push( '1') ~~~~~~~~~ const map: Map<number, string> = new Map() // Argument of type 'number' // is not assignable to parameter of type 'string'. map.set(1, ) 1~~~ 46

Обобщенные интерфейсы interface IStack<TItem> { push(item: TItem) pop(): TItem let numstack: IStack<number> = [1, 2, 3] 47

Обобщенные интерфейсы interface IStack<number> { push(item: number) pop(): number let numstack: IStack<number> = [1, 2, 3] 48

Обобщенные типы type AsyncResult<TResult> = Promise<TResult> TResult let result: AsyncResult<string> = Promise.resolve('200') let result: AsyncResult<string> = '200' 49

Обобщенные типы type AsyncResult<string> = Promise<string> string let result: AsyncResult<string> = Promise.resolve('200') let result: AsyncResult<string> = '200' 50

Обобщенные классы class Stack<TItem> implements IStack<TItem> { private state: TItem[] constructor() { this.state = [] push(item: TItem) { this.state.push(item) pop(): TItem { return this.state.pop() 51

Обобщенные классы class Stack<TItem> implements IStack<TItem> { private state: TItem[] = [] push(item: TItem) { this.state.push(item) pop(): TItem { return this.state.pop() 52

Обобщенные типы interface ISwim { swim() class Dog implements ISwim { swim() {... class Duck implements ISwim { swim() {... 53

Ограничения на обобщенные типы function swimtogether< T1 implements ISwim, T2 implements ISwim >(firstpal: T1, secondpal: T2) { firstpal.swim() secondpal.swim() 54

55

Обобщенные типы type TypeName<T> = T extends string? 'string' : T extends number? 'number' : T extends boolean? 'boolean' : T extends undefined? 'undefined' : T extends Function? 'function' : 'object' 56

Обобщенные типы type TypeName<string> = string extends string? 'string' : T extends number? 'number' : T extends boolean? 'boolean' : T extends undefined? 'undefined' : T extends Function? 'function' : 'object' 57

Обобщенные типы type TypeName<number> = number extends string? 'string' : number extends number? 'number' : T extends boolean? 'boolean' : T extends undefined? 'undefined' : T extends Function? 'function' : 'object' 58

Наша функция function get(obj: any, keyname: string): any { return obj[keyname] 59

Наша функция function get<t>(obj: T, keyname: string): any { return obj[keyname] 60

Хотим знать список полей объекта и их типы на этапе компиляции Решение: Lookup Types и keyof 61

Lookup типы interface IUser { login: string age: number gender: 'male' 'female' let login: IUser['login'] let login: string let loginorage: IUser['login' 'age'] let loginorage: string number 62

keyof interface IUser { login: string age: number gender: 'male' 'female' let key: keyof IUser let key: 'login' 'age' 'gender' 63

Наша простая функция function get(obj, keyname) { return obj[keyname] 64

Наша простая функция function get<t>(obj: T, keyname: keyof T): T[keyof T] { return obj[keyname] 65

Наша простая функция function get<t>(obj: T, keyname: keyof T): T[keyof T] { return obj[keyname] let a: number = get({ a: 1, 'a') 66

Наша простая функция function get<{ a: 1 >(obj: T, keyname: keyof T): T[keyof T] { return obj[keyname] let a: number = get({ a: 1, 'a') 67

Наша простая функция function get<{ a: 1 >(obj: T, keyname: 'a'): T['a'] { return obj[keyname] let a: number = get({ a: 1, 'a') 68

Наша простая функция function get<{ a: 1 >(obj: T, keyname: 'a'): number { return obj[keyname] let a: number = get({ a: 1, 'a') 69

Наша простая функция function get<t>(obj: T, keyname: keyof T): T[keyof T] { return obj[keyname] let a: number = get({ a: 1, 'a') // Argument of type '"c"' // is not assignable to parameter of type '"a" "b"'. let c: undefined = get({ a: 1, b: 2, 'c') ~~~~~~~~~ 70

Наша простая функция function get<t, K extends keyof T>(obj: T, keyname: K): T[K] { return obj[keyname] let a: number = get({ a: 1, 'a') let c: undefined = get({ a: 1, b: 2, 'c') 71

Перерыв 72

А что там в es5? interface IUser { login: string age: number gender: 'male' 'female' const user = { login: 'dimastark', age: 21, gender: 'male' const readonlyuser:? = Object.freeze(user) 73

А что там в es5? interface IFrozenUser { readonly login: string readonly age: number readonly gender: 'male' 'female' const user = { login: 'dimastark', age: 21, gender: 'male' const readonlyuser: IFrozenUser = Object.freeze(user) Решение: Mapped Types 74

75

Mapped Types interface IUser { login: string age: number gender: 'male' 'female' type Readonly<T> = { ; readonly [P in 'login' 'age' 'gender']: T[P]; const user = { login: 'dimastark', age: 21, gender: 'male' const readonlyuser: Readonly<IUser> = Object.freeze(user) 76

Mapped Types + keyof interface IUser { login: string age: number gender: 'male' 'female' type Readonly<T> = { ; readonly [P in keyof T]: T[P]; const user = { login: 'dimastark', age: 21, gender: 'male' const readonlyuser: Readonly<IUser> = Object.freeze(user) 77

infer type ValueOf<T> = T extends { [key: string]: infer U? U : never; ValueOf<{ a: string, b: string > // string ValueOf<{ a: string, b: number > // string number 78

Mapped Types interface IUser { login: string birthdate: { year: number month: number day: number gender: 'male' 'female' 79

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 80

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 81

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 82

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 83

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 84

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 85

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 86

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 87

Mapped Types type DeepReadonly<T> = { [P in keyof T]: T[P] extends (infer U)[]? DeepReadonly<U>[] : T[P] extends object? DeepReadonly<T[P]> : readonly T[P]; ; 88

Ссылочки TypeScript Handbook. Advanced. TypeScript Deep Dive Андрей Старовойт Эволюция TypeScript TypeScript Playground 89

Вопросы? 90

Спасибо! 91