SlideShare a Scribd company logo
Старовойт Андрей
Зачем нужен тип “true”?
PROFESSIONAL JS CONFERENCE
8-9 NOVEMBER‘19 KYIV, UKRAINE
@JetBrains. All rights reserved
About
—
• Работаю в команде WebStorm 5 лет
• Занимаюсь поддержкой JavaScript, TypeScript, JSX, …
@JetBrains. All rights reserved
Type “true”
—
@JetBrains. All rights reserved
Type “true”
—
let a: true;
TypeScript 2.0
@JetBrains. All rights reserved
Type “true”
—
let a: true;
a = true; //ok
a = false; //error
a = 1; //error
TypeScript 2.0
@JetBrains. All rights reserved
Зачем?
Type “true”
—
@JetBrains. All rights reserved
String Literal
Overloads
—
function create(p: “string"):string;
function create(p: "number"):number;
function create(p):any;
TypeScript 1.1
@JetBrains. All rights reserved
function create(p: true): string;
function create(p: false): number;
function create(p: boolean): string | number;
Type “true”
—
Boolean literals in overloads
@JetBrains. All rights reserved
_
@types/node TSLSocket
@JetBrains. All rights reserved
function check(checkType?: true);Optional
—
@JetBrains. All rights reserved
function check(checkType?: true);
check();
Optional
—
@JetBrains. All rights reserved
function check(checkType?: true);
check();
check(true);
Optional
—
@JetBrains. All rights reserved
_
@types/babel__core
@JetBrains. All rights reserved
TypeScript 2.0
Features
—
@JetBrains. All rights reserved
TypeScript 2.0
Features
—
@JetBrains. All rights reserved
Type Guard
—
@JetBrains. All rights reserved
class Cat { meow() }
class Dog { woof() }
function say(animal: Cat | Dog) {
if (animal instanceof Cat) {
animal.meow();
animal.woof();
}
}
Type Guard
—
@JetBrains. All rights reserved
class Cat { meow() }
class Dog { woof() }
function say(animal: Cat | Dog) {
if (animal instanceof Cat) {
animal.meow(); //ok, no cast
animal.woof(); //error
}
}
Type Guard
—
@JetBrains. All rights reserved
interface ICat { meow() }
interface IDog { woof() }
function say(animal: ICat | IDog) {
if (animal instanceof ICat) {
animal.meow();
}
}
Type Guard
—
@JetBrains. All rights reserved
interface ICat { meow() }
interface IDog { woof() }
function say(animal: ICat | IDog) {
if (animal instanceof ICat) {
animal.meow();
}
}
Type Guard
—
@JetBrains. All rights reserved
interface ICat { meow() }
interface IDog { woof() }
function say(animal: ICat | IDog) {
if (animal instanceof ICat) {
animal.meow();
}
}
//Compiled file
function say(animal) {
if (animal instanceof ICat) {
animal.meow();
}
}
Type Guard
—
@JetBrains. All rights reserved
Type Guard
—
Discriminated union types
@JetBrains. All rights reserved
interface ICat { kind: "cat"; meow() }
interface IDog { kind: "dog"; woof() }
function say(animal: ICat | IDog) {
if (animal.kind == "cat") {
animal.meow(); //ok
}
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
interface ICat { kind: "cat"; meow() }
interface IDog { kind: "dog"; woof() }
function say(animal: ICat | IDog) {
if (animal.kind == "cat") {
animal.meow(); //ok
}
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
interface ICat { kind: "cat"; meow() }
interface IDog { kind: "dog"; woof() }
function say(animal: ICat | IDog) {
if (animal.kind == "cat") {
animal.meow(); //ok
}
}
say({ kind: "cat" }) //error
Discriminated
Union Types
—
@JetBrains. All rights reserved
interface ICat { kind: "cat"; meow() }
interface IDog { kind: "dog"; woof() }
function say(animal: ICat | IDog) {
if (animal.kind == "cat") {
animal.meow(); //ok
}
}
say({ kind: "cat" }) //error
say({ kind: "cat", meow() {}}) //ok
Discriminated
Union Types
—
@JetBrains. All rights reserved
Boolean literals?
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result = {
success: boolean;
callback?() //if success=true
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result = {
success: boolean;
callback?() //if success=true
}
function process(p: Result) {
p.callback(); //ok, can throw error
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
function process(p: Result) {
p.callback(); //error
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
function process(p: Result) {
if (p.success) {
p.callback(); //ok
}
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
function process(p: Result) {
if (p.success) {
p.callback(); //ok
}
}
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
function process(p: Result) {
if (p.success) {
p.callback(); //ok
}
}
process({success : true}) //error
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
function process(p: Result) {
if (p.success) {
p.callback(); //ok
}
}
process({success : true}) //error
process({success : true, callback() {}}) //ok
Discriminated
Union Types
—
@JetBrains. All rights reserved
type Result =
{ success: true, callback() } |
{ success: false };
function process(p: Result) {
if (p.success) {
p.callback(); //ok
}
}
process({success : true}) //error
process({success : true, callback() {}}) //ok
Discriminated
Union Types
—
@JetBrains. All rights reserved
_
@types/jsforce
@JetBrains. All rights reserved
Literal Types
—
@JetBrains. All rights reserved
Literal Types
—
: true
: false
@JetBrains. All rights reserved
Literal Types
—
: true
: false
: boolean
@JetBrains. All rights reserved
Literal Types
—
: true
: false
: boolean
@JetBrains. All rights reserved
Const
—
const checkValue = true;
@JetBrains. All rights reserved
Const
—
const checkValue = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
@JetBrains. All rights reserved
Let
—
let checkValue: true = true;
@JetBrains. All rights reserved
Let
—
let checkValue: true = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
@JetBrains. All rights reserved
Let
—
let checkValue: true = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
checkValue = false; //error
@JetBrains. All rights reserved
Let
—
let checkValue = true;
@JetBrains. All rights reserved
Let
—
let checkValue = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
Type: true
@JetBrains. All rights reserved
Let
—
let checkValue = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
checkValue = false;
Type: true
@JetBrains. All rights reserved
Let
—
let checkValue = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
checkValue = false; //ok
Type: boolean
Inferred type: true
@JetBrains. All rights reserved
Let
—
let checkValue = true;
let falseValue: false = checkValue; //error
let trueValue: true = checkValue; //ok
checkValue = false; //ok
falseValue: false = checkValue; //ok
trueValue: true = checkValue; //error
Type: boolean
Inferred type: true
Inferred type: false
@JetBrains. All rights reserved
function getBoolean() {
let value: true = true;
return value;
}
Return Type
—
@JetBrains. All rights reserved
function getBoolean() {
let value: true = true;
return value;
} //: true
Type: true
Inferred type: true
Return Type
—
@JetBrains. All rights reserved
function getBoolean() {
let value = true;
return value;
}
Return Type
—
Type: boolean
Inferred type: true
@JetBrains. All rights reserved
function getBoolean() {
let value = true;
return value;
} //: boolean
Return Type
—
Type: boolean
Inferred type: true
@JetBrains. All rights reserved
function getNumber() {
let value: number | string = 1;
return value;
}
Type: number | string
Inferred type: 1
Return Type
—
@JetBrains. All rights reserved
function getNumber() {
let value: number | string = 1;
return value;
}
: 1
: number | string
Type: number | string
Inferred type: 1
Return Type
—
@JetBrains. All rights reserved
function getNumber() {
let value: number | string = 1;
return value;
}
: 1
: number
: number | string
Type: number | string
Inferred type: 1
Return Type
—
@JetBrains. All rights reserved
Return Type
—
function getNumber() {
let value: number | string = 1;
return value;
}
: 1
: number
: number | string
Return Type = Fixed(Inferred Type)
Type: number | string
Inferred type: 1
@JetBrains. All rights reserved
function getBoolean1() {
let value: true = true;
return value;
}
function getBoolean2() {
let value = true;
return value;
}
Return Type
—
@JetBrains. All rights reserved
function getBoolean1() {
let value: true = true;
return value; //: true
}
function getBoolean2() {
let value = true;
return value;
}
Return Type
—
@JetBrains. All rights reserved
function getBoolean1() {
let value: true = true;
return value; //: true
}
function getBoolean2() {
let value = true;
return value; //: true(widening)
}
Return Type
—
@JetBrains. All rights reserved
• Литеральные типы: расширяемый и нерасширяемый
• Литералы используемые в коде имеют расширяемый
тип
• При некоторые операциях типы расширяются до
соответствующих примитивных типов
Widening
Literal Types
—
@JetBrains. All rights reserved
Операции расширяющие тип:
• Вычисление возвращаемого типа функции
• Присваивание
• Вызов метода
Widening
Literal Types
—
@JetBrains. All rights reserved
Операции расширяющие тип*:
• Вычисление возвращаемого типа функции
• Присваивание
• Вызов метода
* Есть исключения
Widening
Literal Types
—
@JetBrains. All rights reserved
Afterwords
—
@JetBrains. All rights reserved
function getBoolean1() {
let value: true = true;
return value; //: true
}
function getBoolean2() {
let value = true;
return value; //: true(widening)
}
Literal Types
—
@JetBrains. All rights reserved
function getBoolean1(): boolean {
let value: true = true;
return value; //: true
}
function getBoolean2(): boolean {
let value = true;
return value; //: true(widening)
}
Literal Types
—
@JetBrains. All rights reserved
Используйте явные типы!
Widening
Literal Types
—
jetbrains.com
@JetBrains. All rights reserved
Thanks
—
@anstarovoyt
andrey.starovoyt@jetbrains.com

More Related Content

What's hot (19)

PDF
Discovering functional treasure in idiomatic Groovy
Naresha K
 
PDF
JDK8 : parallel programming made (too ?) easy
José Paumard
 
PDF
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan s.r.o.
 
PDF
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
 
PDF
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Anthony Ferrara
 
PDF
Free your lambdas
José Paumard
 
PDF
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
Joshua Lawrence
 
PDF
The Swift Compiler and Standard Library
Santosh Rajan
 
PDF
Going reactive in java
José Paumard
 
PDF
Autumn collection JavaOne 2014
José Paumard
 
PDF
RubyConf Portugal 2014 - Why ruby must go!
Gautam Rege
 
PDF
PHP7: Hello World!
Pavel Nikolov
 
PPTX
Akka in-action
Raymond Roestenburg
 
PDF
Free your lambdas
José Paumard
 
PDF
The Sincerest Form of Flattery
José Paumard
 
PPTX
The Sincerest Form of Flattery
José Paumard
 
PDF
PHP 7.0 new features (and new interpreter)
Andrea Telatin
 
PDF
Developing A Procedural Language For Postgre Sql
Joshua Drake
 
PDF
Kotlin Types for Java Developers
Chandra Sekhar Nayak
 
Discovering functional treasure in idiomatic Groovy
Naresha K
 
JDK8 : parallel programming made (too ?) easy
José Paumard
 
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan s.r.o.
 
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
 
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Anthony Ferrara
 
Free your lambdas
José Paumard
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
Joshua Lawrence
 
The Swift Compiler and Standard Library
Santosh Rajan
 
Going reactive in java
José Paumard
 
Autumn collection JavaOne 2014
José Paumard
 
RubyConf Portugal 2014 - Why ruby must go!
Gautam Rege
 
PHP7: Hello World!
Pavel Nikolov
 
Akka in-action
Raymond Roestenburg
 
Free your lambdas
José Paumard
 
The Sincerest Form of Flattery
José Paumard
 
The Sincerest Form of Flattery
José Paumard
 
PHP 7.0 new features (and new interpreter)
Andrea Telatin
 
Developing A Procedural Language For Postgre Sql
Joshua Drake
 
Kotlin Types for Java Developers
Chandra Sekhar Nayak
 

Similar to JS Fest 2019/Autumn. Андрей Старовойт. Зачем нужен тип "true" в TypeScript? (20)

PDF
Static types on javascript?! Type checking approaches to ensure healthy appli...
Arthur Puthin
 
PDF
Type Systems on the example of TypeScript
Artur Skowroński
 
PPTX
Type Driven Development with TypeScript
Garth Gilmour
 
PDF
Types For Frontend Developers
Jesse Williamson
 
PDF
TypeScript Introduction
Hans Höchtl
 
PDF
Typescript is the best
GlobalLogic Ukraine
 
PDF
Typescript is the best by Maxim Kryuk
GlobalLogic Ukraine
 
PPTX
Type script by Howard
LearningTech
 
PPTX
TypeScript by Howard
LearningTech
 
PPTX
Howard type script
LearningTech
 
PDF
JavaScript, TypeScipt and React Native
Mitchell Tilbrook
 
PDF
Ask The Expert - Typescript: A stitch in time saves nine
Gianluca Carucci
 
PDF
Introduction to typescript
Mario Alexandro Santini
 
PDF
Introduction to TypeScript
André Pitombeira
 
PPTX
Introduction to TypeScript
Tomas Corral Casas
 
PDF
Swift in SwiftUI
Bongwon Lee
 
PDF
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
 
PDF
Strongly typed web applications by Adel Salakh
OdessaJS Conf
 
PDF
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
Artur Skowroński
 
PDF
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’a
Artur Skowroński
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Arthur Puthin
 
Type Systems on the example of TypeScript
Artur Skowroński
 
Type Driven Development with TypeScript
Garth Gilmour
 
Types For Frontend Developers
Jesse Williamson
 
TypeScript Introduction
Hans Höchtl
 
Typescript is the best
GlobalLogic Ukraine
 
Typescript is the best by Maxim Kryuk
GlobalLogic Ukraine
 
Type script by Howard
LearningTech
 
TypeScript by Howard
LearningTech
 
Howard type script
LearningTech
 
JavaScript, TypeScipt and React Native
Mitchell Tilbrook
 
Ask The Expert - Typescript: A stitch in time saves nine
Gianluca Carucci
 
Introduction to typescript
Mario Alexandro Santini
 
Introduction to TypeScript
André Pitombeira
 
Introduction to TypeScript
Tomas Corral Casas
 
Swift in SwiftUI
Bongwon Lee
 
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
 
Strongly typed web applications by Adel Salakh
OdessaJS Conf
 
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
Artur Skowroński
 
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’a
Artur Skowroński
 
Ad

More from JSFestUA (20)

PDF
JS Fest 2019/Autumn. Роман Савіцький. Webcomponents & lit-element in production
JSFestUA
 
PDF
JS Fest 2019/Autumn. Erick Wendel. 10 secrets to improve Javascript Performance
JSFestUA
 
PDF
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JSFestUA
 
PDF
JS Fest 2019/Autumn. Anton Cherednikov. Choreographic or orchestral architect...
JSFestUA
 
PDF
JS Fest 2019/Autumn. Adam Leos. So why do you need to know Algorithms and Dat...
JSFestUA
 
PDF
JS Fest 2019/Autumn. Marko Letic. Saving the world with JavaScript: A Data Vi...
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Александр Товмач. JAMstack
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Дмитрий Жарков. Blockchainize your SPA or Integrate Java...
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Maciej Treder. Angular Schematics - Develop for developers
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Kyle Boss. A Tinder Love Story: Create a Wordpress Blog ...
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Eyal Eizenberg. Tipping the Scale
JSFestUA
 
PDF
JS Fest 2019/Autumn. Sota Ohara. Сreate own server less CMS from scratch
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Джордж Евтушенко. Как стать программистом, которого хотят
JSFestUA
 
PDF
JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust
JSFestUA
 
PDF
JS Fest 2019/Autumn. Daniel Ostrovsky. Falling in love with decorators ES6/Ty...
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Андрей Андрийко. Гексагональна архітектура в Nodejs проекті
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Борис Могила. Svelte. Почему нам не нужно run-time ядро
JSFestUA
 
PPTX
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JSFestUA
 
PDF
JS Fest 2019. Виктор Турский. 6 способов взломать твое JavaScript приложение
JSFestUA
 
JS Fest 2019/Autumn. Роман Савіцький. Webcomponents & lit-element in production
JSFestUA
 
JS Fest 2019/Autumn. Erick Wendel. 10 secrets to improve Javascript Performance
JSFestUA
 
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JSFestUA
 
JS Fest 2019/Autumn. Anton Cherednikov. Choreographic or orchestral architect...
JSFestUA
 
JS Fest 2019/Autumn. Adam Leos. So why do you need to know Algorithms and Dat...
JSFestUA
 
JS Fest 2019/Autumn. Marko Letic. Saving the world with JavaScript: A Data Vi...
JSFestUA
 
JS Fest 2019/Autumn. Александр Товмач. JAMstack
JSFestUA
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JSFestUA
 
JS Fest 2019/Autumn. Дмитрий Жарков. Blockchainize your SPA or Integrate Java...
JSFestUA
 
JS Fest 2019/Autumn. Maciej Treder. Angular Schematics - Develop for developers
JSFestUA
 
JS Fest 2019/Autumn. Kyle Boss. A Tinder Love Story: Create a Wordpress Blog ...
JSFestUA
 
JS Fest 2019/Autumn. Eyal Eizenberg. Tipping the Scale
JSFestUA
 
JS Fest 2019/Autumn. Sota Ohara. Сreate own server less CMS from scratch
JSFestUA
 
JS Fest 2019/Autumn. Джордж Евтушенко. Как стать программистом, которого хотят
JSFestUA
 
JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust
JSFestUA
 
JS Fest 2019/Autumn. Daniel Ostrovsky. Falling in love with decorators ES6/Ty...
JSFestUA
 
JS Fest 2019/Autumn. Андрей Андрийко. Гексагональна архітектура в Nodejs проекті
JSFestUA
 
JS Fest 2019/Autumn. Борис Могила. Svelte. Почему нам не нужно run-time ядро
JSFestUA
 
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JSFestUA
 
JS Fest 2019. Виктор Турский. 6 способов взломать твое JavaScript приложение
JSFestUA
 
Ad

Recently uploaded (20)

PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
community health nursing question paper 2.pdf
Prince kumar
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 

JS Fest 2019/Autumn. Андрей Старовойт. Зачем нужен тип "true" в TypeScript?

  • 1. Старовойт Андрей Зачем нужен тип “true”? PROFESSIONAL JS CONFERENCE 8-9 NOVEMBER‘19 KYIV, UKRAINE
  • 2. @JetBrains. All rights reserved About — • Работаю в команде WebStorm 5 лет • Занимаюсь поддержкой JavaScript, TypeScript, JSX, …
  • 3. @JetBrains. All rights reserved Type “true” —
  • 4. @JetBrains. All rights reserved Type “true” — let a: true; TypeScript 2.0
  • 5. @JetBrains. All rights reserved Type “true” — let a: true; a = true; //ok a = false; //error a = 1; //error TypeScript 2.0
  • 6. @JetBrains. All rights reserved Зачем? Type “true” —
  • 7. @JetBrains. All rights reserved String Literal Overloads — function create(p: “string"):string; function create(p: "number"):number; function create(p):any; TypeScript 1.1
  • 8. @JetBrains. All rights reserved function create(p: true): string; function create(p: false): number; function create(p: boolean): string | number; Type “true” — Boolean literals in overloads
  • 9. @JetBrains. All rights reserved _ @types/node TSLSocket
  • 10. @JetBrains. All rights reserved function check(checkType?: true);Optional —
  • 11. @JetBrains. All rights reserved function check(checkType?: true); check(); Optional —
  • 12. @JetBrains. All rights reserved function check(checkType?: true); check(); check(true); Optional —
  • 13. @JetBrains. All rights reserved _ @types/babel__core
  • 14. @JetBrains. All rights reserved TypeScript 2.0 Features —
  • 15. @JetBrains. All rights reserved TypeScript 2.0 Features —
  • 16. @JetBrains. All rights reserved Type Guard —
  • 17. @JetBrains. All rights reserved class Cat { meow() } class Dog { woof() } function say(animal: Cat | Dog) { if (animal instanceof Cat) { animal.meow(); animal.woof(); } } Type Guard —
  • 18. @JetBrains. All rights reserved class Cat { meow() } class Dog { woof() } function say(animal: Cat | Dog) { if (animal instanceof Cat) { animal.meow(); //ok, no cast animal.woof(); //error } } Type Guard —
  • 19. @JetBrains. All rights reserved interface ICat { meow() } interface IDog { woof() } function say(animal: ICat | IDog) { if (animal instanceof ICat) { animal.meow(); } } Type Guard —
  • 20. @JetBrains. All rights reserved interface ICat { meow() } interface IDog { woof() } function say(animal: ICat | IDog) { if (animal instanceof ICat) { animal.meow(); } } Type Guard —
  • 21. @JetBrains. All rights reserved interface ICat { meow() } interface IDog { woof() } function say(animal: ICat | IDog) { if (animal instanceof ICat) { animal.meow(); } } //Compiled file function say(animal) { if (animal instanceof ICat) { animal.meow(); } } Type Guard —
  • 22. @JetBrains. All rights reserved Type Guard — Discriminated union types
  • 23. @JetBrains. All rights reserved interface ICat { kind: "cat"; meow() } interface IDog { kind: "dog"; woof() } function say(animal: ICat | IDog) { if (animal.kind == "cat") { animal.meow(); //ok } } Discriminated Union Types —
  • 24. @JetBrains. All rights reserved interface ICat { kind: "cat"; meow() } interface IDog { kind: "dog"; woof() } function say(animal: ICat | IDog) { if (animal.kind == "cat") { animal.meow(); //ok } } Discriminated Union Types —
  • 25. @JetBrains. All rights reserved interface ICat { kind: "cat"; meow() } interface IDog { kind: "dog"; woof() } function say(animal: ICat | IDog) { if (animal.kind == "cat") { animal.meow(); //ok } } say({ kind: "cat" }) //error Discriminated Union Types —
  • 26. @JetBrains. All rights reserved interface ICat { kind: "cat"; meow() } interface IDog { kind: "dog"; woof() } function say(animal: ICat | IDog) { if (animal.kind == "cat") { animal.meow(); //ok } } say({ kind: "cat" }) //error say({ kind: "cat", meow() {}}) //ok Discriminated Union Types —
  • 27. @JetBrains. All rights reserved Boolean literals? Discriminated Union Types —
  • 28. @JetBrains. All rights reserved type Result = { success: boolean; callback?() //if success=true } Discriminated Union Types —
  • 29. @JetBrains. All rights reserved type Result = { success: boolean; callback?() //if success=true } function process(p: Result) { p.callback(); //ok, can throw error } Discriminated Union Types —
  • 30. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; Discriminated Union Types —
  • 31. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; Discriminated Union Types —
  • 32. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; function process(p: Result) { p.callback(); //error } Discriminated Union Types —
  • 33. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; function process(p: Result) { if (p.success) { p.callback(); //ok } } Discriminated Union Types —
  • 34. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; function process(p: Result) { if (p.success) { p.callback(); //ok } } Discriminated Union Types —
  • 35. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; function process(p: Result) { if (p.success) { p.callback(); //ok } } process({success : true}) //error Discriminated Union Types —
  • 36. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; function process(p: Result) { if (p.success) { p.callback(); //ok } } process({success : true}) //error process({success : true, callback() {}}) //ok Discriminated Union Types —
  • 37. @JetBrains. All rights reserved type Result = { success: true, callback() } | { success: false }; function process(p: Result) { if (p.success) { p.callback(); //ok } } process({success : true}) //error process({success : true, callback() {}}) //ok Discriminated Union Types —
  • 38. @JetBrains. All rights reserved _ @types/jsforce
  • 39. @JetBrains. All rights reserved Literal Types —
  • 40. @JetBrains. All rights reserved Literal Types — : true : false
  • 41. @JetBrains. All rights reserved Literal Types — : true : false : boolean
  • 42. @JetBrains. All rights reserved Literal Types — : true : false : boolean
  • 43. @JetBrains. All rights reserved Const — const checkValue = true;
  • 44. @JetBrains. All rights reserved Const — const checkValue = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok
  • 45. @JetBrains. All rights reserved Let — let checkValue: true = true;
  • 46. @JetBrains. All rights reserved Let — let checkValue: true = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok
  • 47. @JetBrains. All rights reserved Let — let checkValue: true = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok checkValue = false; //error
  • 48. @JetBrains. All rights reserved Let — let checkValue = true;
  • 49. @JetBrains. All rights reserved Let — let checkValue = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok Type: true
  • 50. @JetBrains. All rights reserved Let — let checkValue = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok checkValue = false; Type: true
  • 51. @JetBrains. All rights reserved Let — let checkValue = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok checkValue = false; //ok Type: boolean Inferred type: true
  • 52. @JetBrains. All rights reserved Let — let checkValue = true; let falseValue: false = checkValue; //error let trueValue: true = checkValue; //ok checkValue = false; //ok falseValue: false = checkValue; //ok trueValue: true = checkValue; //error Type: boolean Inferred type: true Inferred type: false
  • 53. @JetBrains. All rights reserved function getBoolean() { let value: true = true; return value; } Return Type —
  • 54. @JetBrains. All rights reserved function getBoolean() { let value: true = true; return value; } //: true Type: true Inferred type: true Return Type —
  • 55. @JetBrains. All rights reserved function getBoolean() { let value = true; return value; } Return Type — Type: boolean Inferred type: true
  • 56. @JetBrains. All rights reserved function getBoolean() { let value = true; return value; } //: boolean Return Type — Type: boolean Inferred type: true
  • 57. @JetBrains. All rights reserved function getNumber() { let value: number | string = 1; return value; } Type: number | string Inferred type: 1 Return Type —
  • 58. @JetBrains. All rights reserved function getNumber() { let value: number | string = 1; return value; } : 1 : number | string Type: number | string Inferred type: 1 Return Type —
  • 59. @JetBrains. All rights reserved function getNumber() { let value: number | string = 1; return value; } : 1 : number : number | string Type: number | string Inferred type: 1 Return Type —
  • 60. @JetBrains. All rights reserved Return Type — function getNumber() { let value: number | string = 1; return value; } : 1 : number : number | string Return Type = Fixed(Inferred Type) Type: number | string Inferred type: 1
  • 61. @JetBrains. All rights reserved function getBoolean1() { let value: true = true; return value; } function getBoolean2() { let value = true; return value; } Return Type —
  • 62. @JetBrains. All rights reserved function getBoolean1() { let value: true = true; return value; //: true } function getBoolean2() { let value = true; return value; } Return Type —
  • 63. @JetBrains. All rights reserved function getBoolean1() { let value: true = true; return value; //: true } function getBoolean2() { let value = true; return value; //: true(widening) } Return Type —
  • 64. @JetBrains. All rights reserved • Литеральные типы: расширяемый и нерасширяемый • Литералы используемые в коде имеют расширяемый тип • При некоторые операциях типы расширяются до соответствующих примитивных типов Widening Literal Types —
  • 65. @JetBrains. All rights reserved Операции расширяющие тип: • Вычисление возвращаемого типа функции • Присваивание • Вызов метода Widening Literal Types —
  • 66. @JetBrains. All rights reserved Операции расширяющие тип*: • Вычисление возвращаемого типа функции • Присваивание • Вызов метода * Есть исключения Widening Literal Types —
  • 67. @JetBrains. All rights reserved Afterwords —
  • 68. @JetBrains. All rights reserved function getBoolean1() { let value: true = true; return value; //: true } function getBoolean2() { let value = true; return value; //: true(widening) } Literal Types —
  • 69. @JetBrains. All rights reserved function getBoolean1(): boolean { let value: true = true; return value; //: true } function getBoolean2(): boolean { let value = true; return value; //: true(widening) } Literal Types —
  • 70. @JetBrains. All rights reserved Используйте явные типы! Widening Literal Types —
  • 71. jetbrains.com @JetBrains. All rights reserved Thanks — @anstarovoyt [email protected]