SlideShare a Scribd company logo
TypeScript 2 in action
Александр Русаков / Techops
a_s_rusakov@mail.ru
github.com/arusakov
Немного истории
TypeScript 1.8 22 февраля 2016 г.
TypeScript 2.0 22 сентября 2016 г.
TypeScript 2.1 7 декабря 2016 г.
TypeScript 2.2 RC 2 февраля 2017 г.
blogs.msdn.microsoft.com/typescript/ 2
О чем пойдет речь
● Защита от Undefined / Null
● Literal Types и что это такое
● Размеченные объединения и Redux
● Типизация для React Component API
3
Undefined everywhere
4
Undefined / Null
TypeError: Cannot read property 'x' of undefined
TypeError: Cannot read property 'y' of null
5
Undefined / Null
type User = {
name: string
tags?: string[]
}
function getTags(user: User) {
return user.tags.join(', ')
}
6
type User = {
name: string
tags?: string[]
}
function getTags(user: User) {
return user.tags.join(', ')
}
Undefined / Null
7
type User = {
name: string
tags?: string[]
}
function getTags(user: User) {
return user.tags.join(', ')
}
Undefined / Null
8
Non-Nullable Types
https://github.com/Microsoft/TypeScript/pull/7140
// tsc --strictNullChecks
function getTags(user: User) {
return user.tags.join(', ')
}
// ERROR: Object is possibly 'undefined'
9
Non-Nullable Types
// tsc --strictNullChecks
function getTagStrict(user: User) {
return user.tags && user.tags.join(', ')
}
10
Non-Null Assertion !
// tsc --strictNullChecks
function getTagForce(user: User) {
return user.tags!.join(', ')
}
11
Literal Types
12
???
font-weight
13
Какие допустимые значения этого CSS свойства?
font-weight
14
Какие допустимые значения этого CSS свойства?
initial, inherit, unset,
normal, bold, bolder, lighter
100, 200, 300, 400, 500, 600, 700, 800, 900
https://www.w3.org/TR/css-fonts-3/#font-weight-prop
font-weight
15
type fontWeight = number | string
<div style={{ fontWeight: 1000 }} />
<div style={{ fontWeight: 'ligther' }} />
font-weight
16
type fontWeight = number | string
<div style={{ fontWeight: 1000 }} />
<div style={{ fontWeight: 'ligther' }} />
font-weight
17
type fontWeight = number | string
<div style={{ fontWeight: 1000 }} />
<div style={{ fontWeight: 'ligther' }} />
Literal Types
18
type fontWeight =
'initial' | 'inherit' | 'unset' |
'normal' | 'bold' | 'bolder' | 'lighter' |
100 | 200 | 300 | 400 | 500 |
600 | 700 | 800 | 900
https://github.com/Microsoft/TypeScript/pull/9407
Literal Types
19
<div style={{ fontWeight: 1000 }} />
<div style={{ fontWeight: 'ligther' }} />
// ERROR: Types of property 'fontWeight' are incompatible.
TypeScript 1 + Redux
import { Action } from 'redux' // Interface
const ACTION_TYPE_1 = 'type1'
interface Action1 extends Action {
data: number
}
20
TypeScript 1 + Redux
function reduce(state, action: Action) {
switch (action.type) {
case ACTION_TYPE_1:
action.data
}
}
21
TypeScript 1 + Redux
function reduce(state, action: Action) {
switch (action.type) {
case ACTION_TYPE_1:
action.data
}
}
// ERROR: Property 'data' does not exist on type 'Action'
22
TypeScript 1 + Redux
23
function reduce(state, action: Action) {
switch (action.type) {
case ACTION_TYPE_1:
(action as Action1).data // number
}
}
TypeScript 1 + Redux ≠ ♥
24
TypeScript 2
25
Discriminated Union Types
type Action =
{ type: 'type1', id: number } |
{ type: 'type2', name: string }
26https://github.com/Microsoft/TypeScript/pull/9163
Discriminated Union Types
const ACTION_TYPE1 = 'type1'
const ACTION_TYPE2 = 'type2'
27
Discriminated Union Types
const ACTION_TYPE1 = 'type1'
const ACTION_TYPE2 = 'type2'
type Action =
{ type: typeof ACTION_TYPE1, id: number } |
{ type: typeof ACTION_TYPE2, name: string }
28
TypeScript 2 ♥ Redux
https://spin.atomicobject.com/2016/09/27/typed-redux-reducers-typescript-2-0/
switch (action.type) {
case ACTION_TYPE1:
action.id // number
case ACTION_TYPE2:
action.name // string
}
29
React
30
TypeScript 1 + React
type State = { x: number, y: string }
this.state.x = 1
this.setState({ y: e.target.value })
31
TypeScript 1 + React
type State = { x: number, y: string }
this.state.x = 1
this.setState({ y: e.target.value })
// ERROR: Property 'x' is missing
32
TypeScript 1 + React
type State = { x: number, y: string }
this.state.x = 1
this.setState({ y: e.target.value } as State)
33
TypeScript 1 + React ≠ ♥
34
Index+Mapped Types
// react.d.ts
class Component<P, S> {
setState<K extends keyof S>(s: Pick<S, K>): void;
props: Readonly<P>;
state: Readonly<S>;
}
35
https://github.com/Microsoft/TypeScript/pull/11929
https://github.com/Microsoft/TypeScript/pull/12114
TypeScript 2 ♥ React
type State = { x: number, y: string }
this.state.x = 1
// ERROR: Cannot assign because it is a read-only property
this.setState({ y: e.target.value })
36
Predefined Mapped Types
// lib.es6.d.ts
Pick<T, K extends keyof T>
Readonly<T>
Partial<T>
Record<K extends string, T>
37
В заключение
38
github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript
github.com/Microsoft/TypeScript/wiki/Roadmap
Спасибо за внимание
Вопросы?
Презентация и материалы: bit.ly/2kzGfOP
Александр Русаков / Techops
a_s_rusakov@mail.ru
github.com/arusakov
Ad

Recommended

High-Quality JavaScript Code
High-Quality JavaScript Code
Den Odell
 
JavaScript objects and functions
JavaScript objects and functions
Victor Verhaagen
 
Lecture 5: Client Side Programming 1
Lecture 5: Client Side Programming 1
Artificial Intelligence Institute at UofSC
 
Typescript ppt
Typescript ppt
akhilsreyas
 
Getting started with typescript
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
TypeScript - das bessere JavaScript!?
TypeScript - das bessere JavaScript!?
Christian Kaltepoth
 
Launch Yourself into The AngularJS 2 And TypeScript Space
Launch Yourself into The AngularJS 2 And TypeScript Space
ColdFusionConference
 
TypeScript
TypeScript
Udaiappa Ramachandran
 
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
devCAT Studio, NEXON
 
TypeScript - An Introduction
TypeScript - An Introduction
NexThoughts Technologies
 
TypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
TypeScript, Dart, CoffeeScript and JavaScript Comparison
TypeScript, Dart, CoffeeScript and JavaScript Comparison
Haim Michael
 
Александр Русаков - TypeScript 2 in action
Александр Русаков - TypeScript 2 in action
MoscowJS
 
개발자라면, 블로그
개발자라면, 블로그
HyunSeob Lee
 
[123] electron 김성훈
[123] electron 김성훈
NAVER D2
 
Getting started with typescript and angular 2
Getting started with typescript and angular 2
Knoldus Inc.
 
Introduction to JavaScript
Introduction to JavaScript
Bryan Basham
 
Java script ppt
Java script ppt
The Health and Social Care Information Centre
 
Js ppt
Js ppt
Rakhi Thota
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Alphorm.com Formation TypeScript
Alphorm.com Formation TypeScript
Alphorm
 
Practical TypeScript
Practical TypeScript
ldaws
 
Type script is awesome
Type script is awesome
KeithMurgic
 
Introduction to TypeScript
Introduction to TypeScript
KeithMurgic
 
"Enhancing Your React with Advanced TypeScript", Titian Cernicova-Dragomir
"Enhancing Your React with Advanced TypeScript", Titian Cernicova-Dragomir
Fwdays
 
Flow or Type - how to React to that?
Flow or Type - how to React to that?
Krešimir Antolić
 
Introduction to typescript
Introduction to typescript
Mario Alexandro Santini
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
TypeScript 101
TypeScript 101
rachelterman
 
TypeScript.ppt LPU Notes Lecture PPT for 2024
TypeScript.ppt LPU Notes Lecture PPT for 2024
manveersingh2k05
 

More Related Content

Viewers also liked (13)

최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
devCAT Studio, NEXON
 
TypeScript - An Introduction
TypeScript - An Introduction
NexThoughts Technologies
 
TypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
TypeScript, Dart, CoffeeScript and JavaScript Comparison
TypeScript, Dart, CoffeeScript and JavaScript Comparison
Haim Michael
 
Александр Русаков - TypeScript 2 in action
Александр Русаков - TypeScript 2 in action
MoscowJS
 
개발자라면, 블로그
개발자라면, 블로그
HyunSeob Lee
 
[123] electron 김성훈
[123] electron 김성훈
NAVER D2
 
Getting started with typescript and angular 2
Getting started with typescript and angular 2
Knoldus Inc.
 
Introduction to JavaScript
Introduction to JavaScript
Bryan Basham
 
Java script ppt
Java script ppt
The Health and Social Care Information Centre
 
Js ppt
Js ppt
Rakhi Thota
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Alphorm.com Formation TypeScript
Alphorm.com Formation TypeScript
Alphorm
 
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
devCAT Studio, NEXON
 
TypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
TypeScript, Dart, CoffeeScript and JavaScript Comparison
TypeScript, Dart, CoffeeScript and JavaScript Comparison
Haim Michael
 
Александр Русаков - TypeScript 2 in action
Александр Русаков - TypeScript 2 in action
MoscowJS
 
개발자라면, 블로그
개발자라면, 블로그
HyunSeob Lee
 
[123] electron 김성훈
[123] electron 김성훈
NAVER D2
 
Getting started with typescript and angular 2
Getting started with typescript and angular 2
Knoldus Inc.
 
Introduction to JavaScript
Introduction to JavaScript
Bryan Basham
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Alphorm.com Formation TypeScript
Alphorm.com Formation TypeScript
Alphorm
 

Similar to TypeScript 2 in action (20)

Practical TypeScript
Practical TypeScript
ldaws
 
Type script is awesome
Type script is awesome
KeithMurgic
 
Introduction to TypeScript
Introduction to TypeScript
KeithMurgic
 
"Enhancing Your React with Advanced TypeScript", Titian Cernicova-Dragomir
"Enhancing Your React with Advanced TypeScript", Titian Cernicova-Dragomir
Fwdays
 
Flow or Type - how to React to that?
Flow or Type - how to React to that?
Krešimir Antolić
 
Introduction to typescript
Introduction to typescript
Mario Alexandro Santini
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
TypeScript 101
TypeScript 101
rachelterman
 
TypeScript.ppt LPU Notes Lecture PPT for 2024
TypeScript.ppt LPU Notes Lecture PPT for 2024
manveersingh2k05
 
Power Leveling your TypeScript
Power Leveling your TypeScript
Offirmo
 
Milano JS Meetup - Gabriele Petronella - Codemotion Milan 2016
Milano JS Meetup - Gabriele Petronella - Codemotion Milan 2016
Codemotion
 
Type Script Conceitos de ts para projetos front-end React - por ruben marcus
Type Script Conceitos de ts para projetos front-end React - por ruben marcus
Ruben Marcus Luz Paschoarelli
 
JavaScript, TypeScipt and React Native
JavaScript, TypeScipt and React Native
Mitchell Tilbrook
 
Type Driven Development with TypeScript
Type Driven Development with TypeScript
Garth Gilmour
 
Typescript presentation
Typescript presentation
Kartik Grewal
 
Typescript - interesting parts
Typescript - interesting parts
Mykyta Khmel
 
Type script
Type script
Zunair Shoes
 
TypeScript Interview Questions PDF By ScholarHat
TypeScript Interview Questions PDF By ScholarHat
Scholarhat
 
Typescript: Beginner to Advanced
Typescript: Beginner to Advanced
Talentica Software
 
An Introduction to TypeScript
An Introduction to TypeScript
WrapPixel
 
Practical TypeScript
Practical TypeScript
ldaws
 
Type script is awesome
Type script is awesome
KeithMurgic
 
Introduction to TypeScript
Introduction to TypeScript
KeithMurgic
 
"Enhancing Your React with Advanced TypeScript", Titian Cernicova-Dragomir
"Enhancing Your React with Advanced TypeScript", Titian Cernicova-Dragomir
Fwdays
 
Flow or Type - how to React to that?
Flow or Type - how to React to that?
Krešimir Antolić
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
TypeScript.ppt LPU Notes Lecture PPT for 2024
TypeScript.ppt LPU Notes Lecture PPT for 2024
manveersingh2k05
 
Power Leveling your TypeScript
Power Leveling your TypeScript
Offirmo
 
Milano JS Meetup - Gabriele Petronella - Codemotion Milan 2016
Milano JS Meetup - Gabriele Petronella - Codemotion Milan 2016
Codemotion
 
Type Script Conceitos de ts para projetos front-end React - por ruben marcus
Type Script Conceitos de ts para projetos front-end React - por ruben marcus
Ruben Marcus Luz Paschoarelli
 
JavaScript, TypeScipt and React Native
JavaScript, TypeScipt and React Native
Mitchell Tilbrook
 
Type Driven Development with TypeScript
Type Driven Development with TypeScript
Garth Gilmour
 
Typescript presentation
Typescript presentation
Kartik Grewal
 
Typescript - interesting parts
Typescript - interesting parts
Mykyta Khmel
 
TypeScript Interview Questions PDF By ScholarHat
TypeScript Interview Questions PDF By ScholarHat
Scholarhat
 
Typescript: Beginner to Advanced
Typescript: Beginner to Advanced
Talentica Software
 
An Introduction to TypeScript
An Introduction to TypeScript
WrapPixel
 
Ad

Recently uploaded (20)

Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
How Automation in Claims Handling Streamlined Operations
How Automation in Claims Handling Streamlined Operations
Insurance Tech Services
 
Automated Testing and Safety Analysis of Deep Neural Networks
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Simplify Insurance Regulations with Compliance Management Software
Simplify Insurance Regulations with Compliance Management Software
Insurance Tech Services
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
Why Edge Computing Matters in Mobile Application Tech.pdf
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
Which Hiring Management Tools Offer the Best ROI?
Which Hiring Management Tools Offer the Best ROI?
HireME
 
Top Time Tracking Solutions for Accountants
Top Time Tracking Solutions for Accountants
oliviareed320
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
How Automation in Claims Handling Streamlined Operations
How Automation in Claims Handling Streamlined Operations
Insurance Tech Services
 
Automated Testing and Safety Analysis of Deep Neural Networks
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Simplify Insurance Regulations with Compliance Management Software
Simplify Insurance Regulations with Compliance Management Software
Insurance Tech Services
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
Why Edge Computing Matters in Mobile Application Tech.pdf
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
 
Which Hiring Management Tools Offer the Best ROI?
Which Hiring Management Tools Offer the Best ROI?
HireME
 
Top Time Tracking Solutions for Accountants
Top Time Tracking Solutions for Accountants
oliviareed320
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
Ad

TypeScript 2 in action