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

More Related Content

PPT
High-Quality JavaScript Code
PDF
JavaScript objects and functions
PPTX
Lecture 5: Client Side Programming 1
PPTX
Typescript ppt
PPTX
Getting started with typescript
PDF
TypeScript - das bessere JavaScript!?
PDF
Launch Yourself into The AngularJS 2 And TypeScript Space
PPTX
High-Quality JavaScript Code
JavaScript objects and functions
Lecture 5: Client Side Programming 1
Typescript ppt
Getting started with typescript
TypeScript - das bessere JavaScript!?
Launch Yourself into The AngularJS 2 And TypeScript Space

Viewers also liked (13)

PDF
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
PDF
TypeScript - An Introduction
PDF
TypeScript Introduction
PDF
TypeScript, Dart, CoffeeScript and JavaScript Comparison
PDF
Александр Русаков - TypeScript 2 in action
PDF
개발자라면, 블로그
PPTX
[123] electron 김성훈
ODP
Getting started with typescript and angular 2
PDF
Introduction to JavaScript
PPT
Js ppt
PDF
TypeScript: coding JavaScript without the pain
PDF
Alphorm.com Formation TypeScript
최호영, TYPESCRIPT - Javascript의 안정성을 높이기 위한 시도, NDC2013
TypeScript - An Introduction
TypeScript Introduction
TypeScript, Dart, CoffeeScript and JavaScript Comparison
Александр Русаков - TypeScript 2 in action
개발자라면, 블로그
[123] electron 김성훈
Getting started with typescript and angular 2
Introduction to JavaScript
Js ppt
TypeScript: coding JavaScript without the pain
Alphorm.com Formation TypeScript
Ad

Similar to TypeScript 2 in action (20)

PDF
Reactive, component 그리고 angular2
PDF
Using ReasonML For Your Next JavaScript Project
PDF
Software Developer Training
PDF
Feedback using Angularjs + Typescript at Serenytics
PPTX
Typescript language extension of java script
PDF
Suite Script 2.0 API Basics
PPTX
JSLounge - TypeScript 소개
PPTX
Rails World 2023: Powerful Rails Features You Might Not Know
PPTX
2. Design patterns. part #2
PPTX
Rits Brown Bag - TypeScript
PDF
Petcube epic battle: architecture vs product. UA Mobile 2017.
PPTX
GDG On Campus NBNSCOE Web Development Workshop Day 2 : JavaScript and DOM
PDF
Review of c_sharp2_features_part_ii
PPTX
UNIT 1 (7).pptx
PDF
ClojureScript interfaces to React
PPT
DS Unit 6.ppt
PDF
Introduction to typescript
PDF
TypeScript for Java Developers
PPTX
Applying Compiler Techniques to Iterate At Blazing Speed
PDF
Interoperable Component Patterns
Reactive, component 그리고 angular2
Using ReasonML For Your Next JavaScript Project
Software Developer Training
Feedback using Angularjs + Typescript at Serenytics
Typescript language extension of java script
Suite Script 2.0 API Basics
JSLounge - TypeScript 소개
Rails World 2023: Powerful Rails Features You Might Not Know
2. Design patterns. part #2
Rits Brown Bag - TypeScript
Petcube epic battle: architecture vs product. UA Mobile 2017.
GDG On Campus NBNSCOE Web Development Workshop Day 2 : JavaScript and DOM
Review of c_sharp2_features_part_ii
UNIT 1 (7).pptx
ClojureScript interfaces to React
DS Unit 6.ppt
Introduction to typescript
TypeScript for Java Developers
Applying Compiler Techniques to Iterate At Blazing Speed
Interoperable Component Patterns
Ad

Recently uploaded (20)

PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPT
Introduction Database Management System for Course Database
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
top salesforce developer skills in 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Digital Strategies for Manufacturing Companies
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
ai tools demonstartion for schools and inter college
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
CHAPTER 2 - PM Management and IT Context
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms I-SECS-1021-03
Design an Analysis of Algorithms II-SECS-1021-03
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Introduction Database Management System for Course Database
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
top salesforce developer skills in 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Digital Strategies for Manufacturing Companies
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
ai tools demonstartion for schools and inter college
history of c programming in notes for students .pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus

TypeScript 2 in action