SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
Flow or Type - how
to React to that?
Domagoj Cerjan
Kresimir Antolic
WHAT TO EXPECT?
Nothing revolutionary.
Hints and cases where *THIS* can help you!
Enough for you to start believing in the TYPE.
THE PROBLEM
● Familiar with these?
● What is the second argument of that function?
● I removed a function from a module, what code used it and where will it break?
OUR LORD AND SAVIOUR…*
* Not really but it helps A LOT.
● A safeguard preventing us from assigning apples to oranges
● A tool which helps to avoid ‘undefined is not a function’ or ‘cannot read property x of
undefined’ errors
● Gives a sense of hope and security in huge codebases
● Makes refactoring a little less heart attack prone
● Makes code more clear and explicit
A Type System!
IN REACT?!
… Not exactly.
● Around the React ecosystem… Yeah
● Not tied directly but it makes our life easier
● Defining input for your components and describing your data
API DATA
TRANSFORM
DATA
API DATA
TRANSFORM
DATA
COMBiNE
TRANSFORM
UI
REACT
COMPONENT
LOCAL
DATA
INTERACTION!
MODIFY
FUNCTION
CALL
DATA
OK.. WHAT THEN?
Flow
● Type-checking preprocessor bolted
onto JS
● More oriented towards FP
● Requires a type annotation removal
step before JS code can be consumed
(usually via @babel/preset-flow)
Typescript
● Actual language with its own
compiler
● More oriented towards OOP
● Has enums (it is a different
language)
● Requires a compilation step to
produce runnable JS code (via tsc)
Type System{s}?
Flow
● Prioritizes soundness - it rejects all
invalid code and some valid
● Type-related bugs do not slip through
● Objects, Interfaces and Functions are
structurally typed
● Classes are nominally typed (ie two
differently named classes having exact
same shape are different)
Typescript
● Prioritizes completeness - accepts
all valid code and some invalid
● Type-related bugs might slip
through
● Objects and Interfaces are
structurally typed
● Classes, Functions and somewhat
Enums are nominally typed
Type System{s}? - Continued
OK OK...
WHAT TO EXPECT?
Making sure an apple is an apple
Types
let call: number = 'maybe';
// Type '"maybe"' is not assignable to type 'number'
call = 2;
class Apple { private name: string; }
class Orange { private name: string; }
let thing: Apple;
thing = new Apple();
thing = new Orange();
// Type 'Orange' is not assignable to type 'Apple'.
// Types have separate declarations of a private property 'name'.
Typescript
let call: number = 'maybe';
// Cannot assign 'maybe' to call because string [1] is incompatible with number [2].
call = 2;
class Apple { _name: string; }
class Orange { _name: string; }
let thing: Apple;
thing = new Apple();
thing = new Orange();
// Cannot assign new Orange() to thing because Orange [1] is incompatible with Apple [2].
Flow
Describing contents in the box
Interfaces/Types
type PokemonType = 'water' | 'fire' | 'electric';
interface IPokemon {
name: string;
type: PokemonType[];
}
interface IProps {
list: IPokemon[];
}
interface IState {
isExpanded: boolean;
}
export class PokemonList extends React.PureComponent<IProps, IState> {
state = { isExpanded: false };
render() {
const { list } = this.props;
return list ? (<div>{list.map((p) => <div>{p.name}</div>)}</div>) : null;
}
}
Typescript
type PokemonType = 'water' | 'fire' | 'electric';
interface IPokemon {
name: string;
type: PokemonType[];
}
interface IProps {
list: IPokemon[];
}
interface IState {
isExpanded: boolean;
}
export class PokemonList extends PureComponent<IProps, IState> {
state = { isExpanded: false };
render() {
const { list } = this.props;
return list ? (<div>{list.map((p) => <div>{p.name}</div>)}</div>) : null;
}
}
Flow
Where type systems shine
Inference
class Apple { private name: string; }
class Orange { private name: string; }
const fruitBox = [new Orange(), new Orange(), new Apple()]
const makeOrangeJuice = (oranges: Orange[]): string =>
`Orange juice (${oranges.length})dl`;
makeOrangeJuice(fruitBox);
// Type 'Apple' is not assignable to type 'Orange'.
// Types have separate declarations of a private property 'name'.
Typescript
class Apple { _name: string; }
class Orange { _name: string; }
const fruitBox = [new Orange(), new Orange(), new Apple()]
const makeOrangeJuice = (oranges: Orange[]): string =>
`Orange juice (${oranges.length})dl`;
makeOrangeJuice(fruitBox);
// Cannot call makeOrangeJuice with fruitBox bound to oranges because Apple [1] is incompatible with Orange [2] in
array element.
Flow
:any is the enemy, believe in type inference
● Using :any type turns type inference off, any is bad, don’t be like any
● We don’t want to type types manually everywhere
● Humans make mistakes often, some developers do also
● Type inference systems usually don’t, but when they do, they do it consistently
● Do not assume types, let type inference do that for you!
● Relying on type inference mechanisms allows us to think less and code more
Organizing your boxes
Structuring your app
export class Pokemon {
static readonly type: string = 'electric';
public height: number = 0.5;
protected pokemonIdx: number = 0;
private name: string = 'pikachu';
constructor() {
console.log(this.name);
}
}
Typescript
export class Pokemon {
static +type: string = 'electric';
height: number = 0.5;
pokemonIdx: number = 0;
_name: string = 'pikachu';
constructor() {
console.log(this._name);
}
}
Flow
Working with the codebase
Stick, stones, wood, lava..
Flow
● IDE integration is lacking
compared to Typescript
● Slow, though improving a lot with
every new release
● Outstanding command line tools
● ESLint with flow-type plugin
Typescript
● Great IDE integration (Idea, VSCode...)
● Fast
● Excellent code-completion facilities
● Ok-ish command line tools
● Parallelised type checking
● TSLint (though lacking in comparison
with ESLint)
Working with the codebase
Working well with others?
● Both flow and typescript offer facilities to define library typings
○ Flow via *.flow.js files
○ Typescript via *.d.ts files
● Most commonly used libraries (lodash, react, redux…) have well-tested
and defined typings which provide both code completions and type-safety
● You can always extend, override or write yours
It's leviosa not leviosa
Advanced usages
Advanced usages
● Code generation!
○ Gives type safety on multiple levels
● Generic types
● Nominal vs Structural typing
● Type unions and intersections
GREAT - LET’S MOVE TO TYPES!
When to migrate?
When to migrate to either Typescript or Flow
● Big crucial project
● High people turnover rate
● Aiming to increase robustness of the project codebase
● OOP oriented - Typescript
● FP oriented - Flow
When not to think about it
● Small and simple project
Issues?
● Flow - opt in , TS - have to write everything..
● 3rd party typings - But sometimes… those typings leave a lot to be desired
● TS - if things are not easily typed.. Maybe you didn’t structure your code well
NO MORE ERRORS, NEVER?
Bad things...
● Type systems do not solve all of your mistakes, they only prevent some from happening
● They introduce complexity
● More programming knowledge is needed (Types? What are those?)
● You still need to know JS
● They don't make JS more performant
● Can lead to over-engineering
● Illusion of security (mistyped libraries, abusing any type)
OTHER SOLUTIONS?
● Elm - a Haskell inspired purely functional typed language that compiles down to JS
● Purescript - a Haskell inspired purely functional statically typed language that compiles down to JS
● Livescript - a Haskell, F# and clojure inspired functional statically typed language that also compiles
down to JS
● Dart - Google’s attempt at replacing JS - a more classical optionally-typed language that, you
guessed it, compiles down to JS
● Haxe - a strictly typed general purpose multi-paradigm language that also happens to also target JS
● All of that is great but it introduces new concepts, a new language and a new set of problems which
could be problematic for onboarding new people not familiar to them
Other typed solutions
REMIND ME AGAIN WHY
SHOULD I DO THIS?
Why?
● Refactoring
● Codebase robustness
● Helps with high people turnover rate
● Ease of development (code completion)
● Auto documentation - “what type is that second argument of a function from
over there?!?”
● Readability, static code analysis - catch errors early!
JUST TYPE IT.
THANKS!
@kantolic
@dcerjan

Weitere ähnliche Inhalte

Was ist angesagt?

JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...Edureka!
 
10 Things I Hate About Scala
10 Things I Hate About Scala10 Things I Hate About Scala
10 Things I Hate About ScalaMeir Maor
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)jeffz
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noiseNeeraj Bhusare
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_scriptVijay Kalyan
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScriptbinDebug WorkSpace
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part維佋 唐
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: FundamentalsMahmoud Abdallah
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8JavaBrahman
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)mircodotta
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to DartRamesh Nair
 

Was ist angesagt? (20)

JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
10 Things I Hate About Scala
10 Things I Hate About Scala10 Things I Hate About Scala
10 Things I Hate About Scala
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
pebble - Building apps on pebble
pebble - Building apps on pebblepebble - Building apps on pebble
pebble - Building apps on pebble
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
Applicative style programming
Applicative style programmingApplicative style programming
Applicative style programming
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_script
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScript
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: Fundamentals
 
Javascript
JavascriptJavascript
Javascript
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
C#ppt
C#pptC#ppt
C#ppt
 

Ähnlich wie Flow or Type - how to React to that?

Static vs dynamic types
Static vs dynamic typesStatic vs dynamic types
Static vs dynamic typesTerence Parr
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptEPAM Systems
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScriptOffirmo
 
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...Codemotion
 
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...Codemotion
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java MayaTofik
 
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdfDatabase & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdfInSync2011
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersDiego Freniche Brito
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n2
name name2 n2name name2 n2
name name2 n2callroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.pptcallroom
 

Ähnlich wie Flow or Type - how to React to that? (20)

Static vs dynamic types
Static vs dynamic typesStatic vs dynamic types
Static vs dynamic types
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
 
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
Beyond JavaScript Frameworks: Writing Reliable Web Apps With Elm - Erik Wende...
 
Clean code
Clean codeClean code
Clean code
 
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
Erik Wendel - Beyond JavaScript Frameworks: Writing Reliable Web Apps With El...
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
 
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdfDatabase & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 

Kürzlich hochgeladen

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Kürzlich hochgeladen (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Flow or Type - how to React to that?

  • 1. Flow or Type - how to React to that? Domagoj Cerjan Kresimir Antolic
  • 3. Nothing revolutionary. Hints and cases where *THIS* can help you! Enough for you to start believing in the TYPE.
  • 5. ● Familiar with these? ● What is the second argument of that function? ● I removed a function from a module, what code used it and where will it break?
  • 6. OUR LORD AND SAVIOUR…* * Not really but it helps A LOT.
  • 7. ● A safeguard preventing us from assigning apples to oranges ● A tool which helps to avoid ‘undefined is not a function’ or ‘cannot read property x of undefined’ errors ● Gives a sense of hope and security in huge codebases ● Makes refactoring a little less heart attack prone ● Makes code more clear and explicit A Type System!
  • 9. … Not exactly. ● Around the React ecosystem… Yeah ● Not tied directly but it makes our life easier ● Defining input for your components and describing your data
  • 12. Flow ● Type-checking preprocessor bolted onto JS ● More oriented towards FP ● Requires a type annotation removal step before JS code can be consumed (usually via @babel/preset-flow) Typescript ● Actual language with its own compiler ● More oriented towards OOP ● Has enums (it is a different language) ● Requires a compilation step to produce runnable JS code (via tsc) Type System{s}?
  • 13. Flow ● Prioritizes soundness - it rejects all invalid code and some valid ● Type-related bugs do not slip through ● Objects, Interfaces and Functions are structurally typed ● Classes are nominally typed (ie two differently named classes having exact same shape are different) Typescript ● Prioritizes completeness - accepts all valid code and some invalid ● Type-related bugs might slip through ● Objects and Interfaces are structurally typed ● Classes, Functions and somewhat Enums are nominally typed Type System{s}? - Continued
  • 14. OK OK... WHAT TO EXPECT?
  • 15. Making sure an apple is an apple Types
  • 16. let call: number = 'maybe'; // Type '"maybe"' is not assignable to type 'number' call = 2; class Apple { private name: string; } class Orange { private name: string; } let thing: Apple; thing = new Apple(); thing = new Orange(); // Type 'Orange' is not assignable to type 'Apple'. // Types have separate declarations of a private property 'name'. Typescript
  • 17. let call: number = 'maybe'; // Cannot assign 'maybe' to call because string [1] is incompatible with number [2]. call = 2; class Apple { _name: string; } class Orange { _name: string; } let thing: Apple; thing = new Apple(); thing = new Orange(); // Cannot assign new Orange() to thing because Orange [1] is incompatible with Apple [2]. Flow
  • 18. Describing contents in the box Interfaces/Types
  • 19. type PokemonType = 'water' | 'fire' | 'electric'; interface IPokemon { name: string; type: PokemonType[]; } interface IProps { list: IPokemon[]; } interface IState { isExpanded: boolean; } export class PokemonList extends React.PureComponent<IProps, IState> { state = { isExpanded: false }; render() { const { list } = this.props; return list ? (<div>{list.map((p) => <div>{p.name}</div>)}</div>) : null; } } Typescript
  • 20. type PokemonType = 'water' | 'fire' | 'electric'; interface IPokemon { name: string; type: PokemonType[]; } interface IProps { list: IPokemon[]; } interface IState { isExpanded: boolean; } export class PokemonList extends PureComponent<IProps, IState> { state = { isExpanded: false }; render() { const { list } = this.props; return list ? (<div>{list.map((p) => <div>{p.name}</div>)}</div>) : null; } } Flow
  • 21. Where type systems shine Inference
  • 22. class Apple { private name: string; } class Orange { private name: string; } const fruitBox = [new Orange(), new Orange(), new Apple()] const makeOrangeJuice = (oranges: Orange[]): string => `Orange juice (${oranges.length})dl`; makeOrangeJuice(fruitBox); // Type 'Apple' is not assignable to type 'Orange'. // Types have separate declarations of a private property 'name'. Typescript
  • 23. class Apple { _name: string; } class Orange { _name: string; } const fruitBox = [new Orange(), new Orange(), new Apple()] const makeOrangeJuice = (oranges: Orange[]): string => `Orange juice (${oranges.length})dl`; makeOrangeJuice(fruitBox); // Cannot call makeOrangeJuice with fruitBox bound to oranges because Apple [1] is incompatible with Orange [2] in array element. Flow
  • 24. :any is the enemy, believe in type inference ● Using :any type turns type inference off, any is bad, don’t be like any ● We don’t want to type types manually everywhere ● Humans make mistakes often, some developers do also ● Type inference systems usually don’t, but when they do, they do it consistently ● Do not assume types, let type inference do that for you! ● Relying on type inference mechanisms allows us to think less and code more
  • 26. export class Pokemon { static readonly type: string = 'electric'; public height: number = 0.5; protected pokemonIdx: number = 0; private name: string = 'pikachu'; constructor() { console.log(this.name); } } Typescript
  • 27. export class Pokemon { static +type: string = 'electric'; height: number = 0.5; pokemonIdx: number = 0; _name: string = 'pikachu'; constructor() { console.log(this._name); } } Flow
  • 28. Working with the codebase Stick, stones, wood, lava..
  • 29. Flow ● IDE integration is lacking compared to Typescript ● Slow, though improving a lot with every new release ● Outstanding command line tools ● ESLint with flow-type plugin Typescript ● Great IDE integration (Idea, VSCode...) ● Fast ● Excellent code-completion facilities ● Ok-ish command line tools ● Parallelised type checking ● TSLint (though lacking in comparison with ESLint) Working with the codebase
  • 30. Working well with others? ● Both flow and typescript offer facilities to define library typings ○ Flow via *.flow.js files ○ Typescript via *.d.ts files ● Most commonly used libraries (lodash, react, redux…) have well-tested and defined typings which provide both code completions and type-safety ● You can always extend, override or write yours
  • 31. It's leviosa not leviosa Advanced usages
  • 32. Advanced usages ● Code generation! ○ Gives type safety on multiple levels ● Generic types ● Nominal vs Structural typing ● Type unions and intersections
  • 33. GREAT - LET’S MOVE TO TYPES!
  • 34. When to migrate? When to migrate to either Typescript or Flow ● Big crucial project ● High people turnover rate ● Aiming to increase robustness of the project codebase ● OOP oriented - Typescript ● FP oriented - Flow When not to think about it ● Small and simple project
  • 35. Issues? ● Flow - opt in , TS - have to write everything.. ● 3rd party typings - But sometimes… those typings leave a lot to be desired ● TS - if things are not easily typed.. Maybe you didn’t structure your code well
  • 36. NO MORE ERRORS, NEVER?
  • 37. Bad things... ● Type systems do not solve all of your mistakes, they only prevent some from happening ● They introduce complexity ● More programming knowledge is needed (Types? What are those?) ● You still need to know JS ● They don't make JS more performant ● Can lead to over-engineering ● Illusion of security (mistyped libraries, abusing any type)
  • 39. ● Elm - a Haskell inspired purely functional typed language that compiles down to JS ● Purescript - a Haskell inspired purely functional statically typed language that compiles down to JS ● Livescript - a Haskell, F# and clojure inspired functional statically typed language that also compiles down to JS ● Dart - Google’s attempt at replacing JS - a more classical optionally-typed language that, you guessed it, compiles down to JS ● Haxe - a strictly typed general purpose multi-paradigm language that also happens to also target JS ● All of that is great but it introduces new concepts, a new language and a new set of problems which could be problematic for onboarding new people not familiar to them Other typed solutions
  • 40. REMIND ME AGAIN WHY SHOULD I DO THIS?
  • 41. Why? ● Refactoring ● Codebase robustness ● Helps with high people turnover rate ● Ease of development (code completion) ● Auto documentation - “what type is that second argument of a function from over there?!?” ● Readability, static code analysis - catch errors early!