SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Practical TypeScript
... how to make better mistakes
and why I don't handwrite more
TypeScript
is a Microsoft-owned superset of
Javascript.
Most* modern web frontend
work is done via compiling
standards that don't yet exist in
older browsers to cross-platform
JavaScript.
* citation needed
TypeScript provides a compiler
that can handle standard
JavaScript, and prepare it for older
targets.
It also understands some of its
own syntax to add compile-time
checks.
Great solutions exist for using
TypeScript in React applications
among other destinations.
Why do I like it so much?
Reasons I like TypeScript:
The compiler is smarter
than me
// using strict null checking...
function fooify(thing: string) {
return thing.toUpperCase();
}
fooify(undefined);
// ^^^^^^^^^
// [ts] Argument of type 'undefined' is not assignable
// to parameter of type 'string'.
Can't pass an unde ned to a
string parameter.
const stringOrFalse = (returnString: boolean) =>
(returnString) ? "hi" : false;
const fooify = (thing: string) => thing.toUpperCase();
const value = stringOrFalse(true);
fooify(value);
// ^^^^^
// [ts] Argument of type 'false | "hi"' is not
// assignable to parameter of type 'string'.
// Type 'false' is not assignable to type 'string'.
Value could be false.
const stringOrFalse = (returnString: boolean) =>
(returnString) ? "hi" : false;
const fooify = (thing: string) => thing.toUpperCase();
const value = stringOrFalse(true);
if (value) {
// value can't be 'false', so no error
fooify(value);
}
Compiler is smart enough to nd
conditions that narrow a value.
type Eel = string | 7;
type SwallowType = 'African' | 'European';
enum FavouriteColour {
blue = '#0000ff',
yellow = '#ffff00',
}
interface IHovercraft {
contents: Eel[],
}
Most types are very exible.
interface IUser {
name: string,
isAdmin: boolean,
}
const addUser = (user: IUser) => true;
addUser({name: 'Jesse Doe', isAdmin: true});
Most types are picked up without
needing to annotate them.
addUser({name: 'Alex Nguyen'});
// ^^^^^^^^^^^^^^^^^^^^^
// [ts] Argument of type '{ name: string; }' is not
// assignable to parameter of type 'User'.
// Property 'isAdmin' is missing in type
// '{ name: string; }'.
Error messages tell you why the
type doesn't work here.
interface IFeatureFlagList { [k: string]: boolean };
const makeFlagTester = (flags: IFeatureFlagList) =>
(flag: keyof IFeatureFlagList) =>
flags[flag];
const testFlag = makeFlagTester({
hasBakedBeans: false,
// error due to value
invasiveTracking: 'always!',
// error due to key
true: false,
});
interface IFeatureFlagList { [k: string]: boolean };
const makeFlagTester = (flags: IFeatureFlagList) =>
(flag: keyof IFeatureFlagList) =>
flags[flag];
const testFlag = makeFlagTester({});
// error: true can never be a key of
// IFeatureFlagList
testFlag(true);
// error: boolean doesn't have `toUpperCase`
testFlag('gamification').toUpperCase();
Reasons I like TypeScript:
The community
Using types safely with untyped
libraries involves manually writing
extra declarations.
De nitelyTyped is a community
project that covers 4,924 external
libraries, at time of writing.
yarn add react-router-dom
yarn add -D @types/react-router-dom
declare module "markdown-it/lib/presets/commonmark" {
import { Options } from 'markdown-it';
export default Options;
}
Not particularly onerous to add
missing types if necessary.
Reasons I (sorta) like TypeScript:
It's still JavaScript
Still no ?. operator, for example.
Add TypeScript support now,
migrate whenever you want.
Valid JavaScript is valid*
TypeScript.
Completely fair
comparison
Fin.
Liam Dawson
@liamdaws

Mais conteúdo relacionado

Mais procurados

My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletModify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletCleasbyz
 
Typescript tips & tricks
Typescript tips & tricksTypescript tips & tricks
Typescript tips & tricksOri Calvo
 
Wade not in unknown waters. Part one.
Wade not in unknown waters. Part one.Wade not in unknown waters. Part one.
Wade not in unknown waters. Part one.PVS-Studio
 
Protocol in Swift
Protocol in SwiftProtocol in Swift
Protocol in SwiftYusuke Kita
 
Angular 2.0: Brighter future?
Angular 2.0: Brighter future?Angular 2.0: Brighter future?
Angular 2.0: Brighter future?Eugene Zharkov
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting outputWayneJones104
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsP3 InfoTech Solutions Pvt. Ltd.
 
Property based testing
Property based testingProperty based testing
Property based testingMSDEVMTL
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | LearnbayLearnbayin
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAbimbola Idowu
 
Javascript conditional statements
Javascript conditional statementsJavascript conditional statements
Javascript conditional statementsnobel mujuji
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Javascript Styles and some tips
Javascript Styles and some tipsJavascript Styles and some tips
Javascript Styles and some tipsVítor Baptista
 

Mais procurados (20)

What JS? Itself
What JS? ItselfWhat JS? Itself
What JS? Itself
 
Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletModify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutlet
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Typescript tips & tricks
Typescript tips & tricksTypescript tips & tricks
Typescript tips & tricks
 
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
 
Wade not in unknown waters. Part one.
Wade not in unknown waters. Part one.Wade not in unknown waters. Part one.
Wade not in unknown waters. Part one.
 
Linux shell
Linux shellLinux shell
Linux shell
 
Protocol in Swift
Protocol in SwiftProtocol in Swift
Protocol in Swift
 
Angular 2.0: Brighter future?
Angular 2.0: Brighter future?Angular 2.0: Brighter future?
Angular 2.0: Brighter future?
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting output
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
 
Property based testing
Property based testingProperty based testing
Property based testing
 
Function
FunctionFunction
Function
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | Learnbay
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Javascript conditional statements
Javascript conditional statementsJavascript conditional statements
Javascript conditional statements
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Javascript Styles and some tips
Javascript Styles and some tipsJavascript Styles and some tips
Javascript Styles and some tips
 

Semelhante a Practical TypeScript

Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScriptAleš Najmann
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003R696
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java DevelopersYakov Fain
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practicesIwan van der Kleijn
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Arthur Puthin
 
Clean & Typechecked JS
Clean & Typechecked JSClean & Typechecked JS
Clean & Typechecked JSArthur Puthin
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0Yaser Zhian
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesSiarhei Barysiuk
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascriptmpnkhan
 

Semelhante a Practical TypeScript (20)

Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
Rits Brown Bag - TypeScript
Rits Brown Bag - TypeScriptRits Brown Bag - TypeScript
Rits Brown Bag - TypeScript
 
Clean code
Clean codeClean code
Clean code
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
Einführung in TypeScript
Einführung in TypeScriptEinführung in TypeScript
Einführung in TypeScript
 
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...
 
Clean & Typechecked JS
Clean & Typechecked JSClean & Typechecked JS
Clean & Typechecked JS
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
C++ boot camp part 1/2
C++ boot camp part 1/2C++ boot camp part 1/2
C++ boot camp part 1/2
 
C++ Boot Camp Part 1
C++ Boot Camp Part 1C++ Boot Camp Part 1
C++ Boot Camp Part 1
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascript
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 

Último

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 

Último (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 

Practical TypeScript

  • 1. Practical TypeScript ... how to make better mistakes and why I don't handwrite more
  • 2. TypeScript is a Microsoft-owned superset of Javascript.
  • 3. Most* modern web frontend work is done via compiling standards that don't yet exist in older browsers to cross-platform JavaScript. * citation needed
  • 4.
  • 5. TypeScript provides a compiler that can handle standard JavaScript, and prepare it for older targets. It also understands some of its own syntax to add compile-time checks.
  • 6.
  • 7.
  • 8. Great solutions exist for using TypeScript in React applications among other destinations. Why do I like it so much?
  • 9. Reasons I like TypeScript: The compiler is smarter than me
  • 10. // using strict null checking... function fooify(thing: string) { return thing.toUpperCase(); } fooify(undefined); // ^^^^^^^^^ // [ts] Argument of type 'undefined' is not assignable // to parameter of type 'string'. Can't pass an unde ned to a string parameter.
  • 11. const stringOrFalse = (returnString: boolean) => (returnString) ? "hi" : false; const fooify = (thing: string) => thing.toUpperCase(); const value = stringOrFalse(true); fooify(value); // ^^^^^ // [ts] Argument of type 'false | "hi"' is not // assignable to parameter of type 'string'. // Type 'false' is not assignable to type 'string'. Value could be false.
  • 12. const stringOrFalse = (returnString: boolean) => (returnString) ? "hi" : false; const fooify = (thing: string) => thing.toUpperCase(); const value = stringOrFalse(true); if (value) { // value can't be 'false', so no error fooify(value); } Compiler is smart enough to nd conditions that narrow a value.
  • 13. type Eel = string | 7; type SwallowType = 'African' | 'European'; enum FavouriteColour { blue = '#0000ff', yellow = '#ffff00', } interface IHovercraft { contents: Eel[], } Most types are very exible.
  • 14. interface IUser { name: string, isAdmin: boolean, } const addUser = (user: IUser) => true; addUser({name: 'Jesse Doe', isAdmin: true}); Most types are picked up without needing to annotate them.
  • 15. addUser({name: 'Alex Nguyen'}); // ^^^^^^^^^^^^^^^^^^^^^ // [ts] Argument of type '{ name: string; }' is not // assignable to parameter of type 'User'. // Property 'isAdmin' is missing in type // '{ name: string; }'. Error messages tell you why the type doesn't work here.
  • 16. interface IFeatureFlagList { [k: string]: boolean }; const makeFlagTester = (flags: IFeatureFlagList) => (flag: keyof IFeatureFlagList) => flags[flag]; const testFlag = makeFlagTester({ hasBakedBeans: false, // error due to value invasiveTracking: 'always!', // error due to key true: false, });
  • 17. interface IFeatureFlagList { [k: string]: boolean }; const makeFlagTester = (flags: IFeatureFlagList) => (flag: keyof IFeatureFlagList) => flags[flag]; const testFlag = makeFlagTester({}); // error: true can never be a key of // IFeatureFlagList testFlag(true); // error: boolean doesn't have `toUpperCase` testFlag('gamification').toUpperCase();
  • 18. Reasons I like TypeScript: The community
  • 19. Using types safely with untyped libraries involves manually writing extra declarations.
  • 20. De nitelyTyped is a community project that covers 4,924 external libraries, at time of writing.
  • 21. yarn add react-router-dom yarn add -D @types/react-router-dom
  • 22. declare module "markdown-it/lib/presets/commonmark" { import { Options } from 'markdown-it'; export default Options; } Not particularly onerous to add missing types if necessary.
  • 23. Reasons I (sorta) like TypeScript: It's still JavaScript
  • 24.
  • 25. Still no ?. operator, for example.
  • 26.
  • 27. Add TypeScript support now, migrate whenever you want. Valid JavaScript is valid* TypeScript.
  • 29.
  • 30.