SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
tiny.cloud
PORTING 100K LINES OF
CODE TO TYPESCRIPT
MILLIE MACDONALD
SOFTWARE ENGINEER @ TINY
The Talk
● About Tiny and me
● The Conversion Project:
○ Motivation
○ Why TypeScript?
○ How we converted our JavaScript to TypeScript
○ Benefits, lessons learned and ongoing work
TL;DR: Types help. Gradual typing really helps.
About Tiny
● TinyMCE is a popular open source project
● A rich text editing platform that helped launch
and scale the adoption of Medium, WordPress,
Shopify, Tumblr, Atlassian, EventBrite & more
● In 2015, we launched a startup with offices in
Palo Alto, Australia and Sweden
By the numbers:
● 1 million+ users on Cloud
● 125,000 downloads/week
● 1,000 paying customers
● 232 contributors
About Me
● From Brisbane, Australia
● Bachelor of Engineering
(Honours), Software Engineering
● Intern then QA Engineer then
Software Engineer at Tiny
● Admin of the Australian gaming
community for Splatoon
“100k Lines of Code”?
TinyMCE is:
● Core is ~30k LoC
● User Interface is ~30k LoC
● 44 core plugins + several
premium plugins with ~40k
LoC
Plus various other projects
and libraries…The point of
conversion!
The Conversion Project
Before
● Over the years, we tried and rejected a lot of frameworks
● We also use a functional programming style
● So we wrote our own libraries and frameworks
○ Avoided upgrade problems and framework churn
○ Easier for us to fix problems
○ FP + JS means defensive code, which resulted in code size and runtime performance
issues
● Then ES2015 appeared...
Motivation for the Conversion Project
● ES2015 features - especially a standard module system - were
very enticing, so in 2017 we decided to convert our code.
● We had also been considering switching to an alternative JS
language. Our wish list was:
○ static typing
○ pattern matching
Why TypeScript?
● We looked at ReasonML, PureScript and TypeScript
● We considered:
○ how much of the functional programming feature set we needed
○ the learning curve for each language
○ the upfront development cost to switch
Why TypeScript?
● ReasonML
○ Rewrote our PowerPaste plugin for TinyMCE, replacing complicated regex with strong types and
pattern matching
○ Syntax is similar to JS so easy to learn
● PureScript:
○ Used for some of our internal prototypes
○ Side-effect tracking speeds up code reviews and training, and optics improve runtime efficiency
○ Lots of FP features we don’t need on the client side
● Neither is an easy drop-in replacement for the editor code
Why TypeScript?
● Unfortunately...
○ No pattern matching
● But...
○ Vanilla JS is valid TypeScript
○ Due to already using a module system with strict syntax, converting our code
could be mostly done with a script
○ Types!
○ Gradual types!
What is Gradual Typing?
● Process of starting with minimal types and adding more over time
● Requires a mix of static and dynamic types - like TypeScript
● Requires minimal initial work
● Can invest as however much time and developers as you like in
increasing types each week/month/etc.
● Can start with simple types then increase complexity as you learn and
adjust to the type system
● Low initial cost of adding types, flexible ongoing cost
The How
Conversion Process for each Library
1. Run script to convert modules to ES2015 and TypeScript
2. Manually check the script converted each file correctly
3. Update with changes from already-converted dependencies
4. Fix problems found by TypeScript
5. Run tests and fix any problems
6. Update build process
The Conversion Script
1. Transpile all modules using an Abstract Syntax Tree (AST)
a. Change import syntax
b. Pull code from inside the wrapping function to the root level
c. Change export syntax and apply export default <any>
2. Move files and rename to .ts
3. Copy templates for configuration files
4. Generate Main.ts from api folder
5. Update package.json and .gitignore
Module Conversion Example
define(
'Example Module',
[ ‘example.Dependency' ],
function (Dependency) {
var bar = 0;
var foo = function () { … };
return {
foo,
bar
};
}
);
import { Dependency } from 'example';
var bar = 0;
var foo = function () { … };
export default <any> {
foo,
bar
};
Adding Types
● The script doesn’t add types
● We started adding types to libraries, starting with the libraries
most commonly depended on
● We add types with each PR
○ New code must be at least partially typed
○ “If you touch a file, type it” was the rule for a while
● Now, many of our libraries are at least partially typed
Benefits of TypeScript and
Gradual Typing
Types
● Gained type-based tools e.g. find all usages and
automated refactoring
● Compile time type checking
● Even just adding simple types caught some serious bugs
● Typing our most common dependencies caught many
more
Example Bugs
Incorrect function argument list length
Missing comment resulted in accidental global
Gradual Typing
● Most of the team was able to go back to normal development work
quickly
○ Sometimes devs take a couple days to type a library
○ We add some types with most PRs
● Conversion script used export default <any> so minimal errors
and developer work upfront
● Gradually removing any from modules allowed us to type modules
slowly and methodically
Increased Readability
● Types make it easier to follow code
○ Especially in our giant projects where variables sometimes get passed around a lot
● Types make it easier to debug code
○ Type errors are generally nicely worded
○ Sometimes easier to match weird values to types, then track down which variable or function
they come from
● Types make it easier for new developers to get up to speed and learn
the code
Lessons Learned
Convert Common Libraries First
● Because of our modular architecture we have a lot of
libraries, and many depend on other libraries
● We mostly converted the most common dependencies first,
so we could cascade their types and bug fixes through other
libraries
● But some other libraries were done at the same time, and
they have duplicate type definitions, clashing types, etc.
Gradually Introduce Compiler Features
A couple times we enabled compiler features without testing them
properly on every library...
Write a Script to Test Everything
● Write a script that does every kind of check it can - types,
linting, build, tests, etc.
● Run it when you make a big change
● Ours is called the Really Cool ScriptTM
○ Contains a list of all our libraries
○ Clones each one, installs its dependencies, runs tsc and npm test
Developer Adjustments
● Discuss changes in coding style, tools and processes
● Communicate changes
● Script pre-commit or pre-build type checks to help developers
remember what to check
Continuing Work
Continuing Work
● More types!
● Enabling more compiler features
● Building a type definition file for TinyMCE’s editor API
Check out TinyMCE
● TinyMCE is open source
○ https://github.com/tinymce/tinymce
○ Contains TS files and most config and build files
○ Master branch is TinyMCE 4
○ 5.x branch for TinyMCE 5
● Found a bug? Win some swag!
https://go.tiny.cloud/blog/tinymce-5-developer-challenge/
Thank you!
Questions?

Contenu connexe

Tendances

TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
Patrick John Pacaña
 

Tendances (20)

Learning typescript
Learning typescriptLearning typescript
Learning typescript
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
Intro to Crystal Programming Language
Intro to Crystal Programming LanguageIntro to Crystal Programming Language
Intro to Crystal Programming Language
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
Crystal
CrystalCrystal
Crystal
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
Typescript - MentorMate Academy
Typescript - MentorMate AcademyTypescript - MentorMate Academy
Typescript - MentorMate Academy
 
Ruby, the language of devops
Ruby, the language of devopsRuby, the language of devops
Ruby, the language of devops
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
 
Getting Started with the TypeScript Language
Getting Started with the TypeScript LanguageGetting Started with the TypeScript Language
Getting Started with the TypeScript Language
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
AngularConf2015
AngularConf2015AngularConf2015
AngularConf2015
 
Thinkful - Intro to JavaScript
Thinkful - Intro to JavaScriptThinkful - Intro to JavaScript
Thinkful - Intro to JavaScript
 

Similaire à Porting 100k Lines of Code to TypeScript

ppt notes for python language variable data types
ppt notes for python language variable data typesppt notes for python language variable data types
ppt notes for python language variable data types
SukhpreetSingh519414
 

Similaire à Porting 100k Lines of Code to TypeScript (20)

Writing clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancodingWriting clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancoding
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
 
Benefits of Typescript.pptx
Benefits of Typescript.pptxBenefits of Typescript.pptx
Benefits of Typescript.pptx
 
Type script
Type scriptType script
Type script
 
Class_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdfClass_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdf
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
AirBNB's ML platform - BigHead
AirBNB's ML platform - BigHeadAirBNB's ML platform - BigHead
AirBNB's ML platform - BigHead
 
Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
 Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa... Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Go at uber
Go at uberGo at uber
Go at uber
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1
 
ppt notes for python language variable data types
ppt notes for python language variable data typesppt notes for python language variable data types
ppt notes for python language variable data types
 
TypeScript and Angular2 (Love at first sight)
TypeScript and Angular2 (Love at first sight)TypeScript and Angular2 (Love at first sight)
TypeScript and Angular2 (Love at first sight)
 
Managing Translation Workflows in Drupal 7
Managing Translation Workflows in Drupal 7Managing Translation Workflows in Drupal 7
Managing Translation Workflows in Drupal 7
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Moving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should KnowMoving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should Know
 
#RADC4L16: An API-First Archives Approach at NPR
#RADC4L16: An API-First Archives Approach at NPR#RADC4L16: An API-First Archives Approach at NPR
#RADC4L16: An API-First Archives Approach at NPR
 
Python for katana
Python for katanaPython for katana
Python for katana
 
Introduction to Python Programming Basics
Introduction  to  Python  Programming BasicsIntroduction  to  Python  Programming Basics
Introduction to Python Programming Basics
 

Plus de Tiny

Plus de Tiny (16)

Is block-based editing the future of web content management systems?
Is block-based editing the future of web content management systems?Is block-based editing the future of web content management systems?
Is block-based editing the future of web content management systems?
 
Engage 2019: Extending the editor in Connections
Engage 2019: Extending the editor in ConnectionsEngage 2019: Extending the editor in Connections
Engage 2019: Extending the editor in Connections
 
Engage 2019: Building a design system to modernize Connections
Engage 2019: Building a design system to modernize ConnectionsEngage 2019: Building a design system to modernize Connections
Engage 2019: Building a design system to modernize Connections
 
Introduction to TinyMCE Session #7 Integrating with frameworks
Introduction to TinyMCE Session #7 Integrating with frameworksIntroduction to TinyMCE Session #7 Integrating with frameworks
Introduction to TinyMCE Session #7 Integrating with frameworks
 
Introduction to TinyMCE Session #6 Working With Images
Introduction to TinyMCE Session #6 Working With ImagesIntroduction to TinyMCE Session #6 Working With Images
Introduction to TinyMCE Session #6 Working With Images
 
Introduction to TinyMCE Session #5 Popular Plugins
Introduction to TinyMCE Session #5 Popular PluginsIntroduction to TinyMCE Session #5 Popular Plugins
Introduction to TinyMCE Session #5 Popular Plugins
 
Introduction to TinyMCE Session #4 Working With Content
Introduction to TinyMCE Session #4 Working With ContentIntroduction to TinyMCE Session #4 Working With Content
Introduction to TinyMCE Session #4 Working With Content
 
Introduction to TinyMCE Session #3 Customizing Styles
Introduction to TinyMCE Session #3 Customizing StylesIntroduction to TinyMCE Session #3 Customizing Styles
Introduction to TinyMCE Session #3 Customizing Styles
 
Introduction to TinyMCE Session #1 Unboxing TinyMCE
Introduction to TinyMCE Session #1 Unboxing TinyMCEIntroduction to TinyMCE Session #1 Unboxing TinyMCE
Introduction to TinyMCE Session #1 Unboxing TinyMCE
 
Introduction to TinyMCE Session #2 Customizing TinyMCE
Introduction to TinyMCE Session #2 Customizing TinyMCEIntroduction to TinyMCE Session #2 Customizing TinyMCE
Introduction to TinyMCE Session #2 Customizing TinyMCE
 
Project to Product to Profit - Lessons learned trying to commercialize a majo...
Project to Product to Profit - Lessons learned trying to commercialize a majo...Project to Product to Profit - Lessons learned trying to commercialize a majo...
Project to Product to Profit - Lessons learned trying to commercialize a majo...
 
Going beyond the 'Bold' button by Jack Mason
Going beyond the 'Bold' button by Jack MasonGoing beyond the 'Bold' button by Jack Mason
Going beyond the 'Bold' button by Jack Mason
 
WebRadar
WebRadarWebRadar
WebRadar
 
Textbox.io for IBM Connections - IBM Connect 2016
Textbox.io for IBM Connections - IBM Connect 2016Textbox.io for IBM Connections - IBM Connect 2016
Textbox.io for IBM Connections - IBM Connect 2016
 
Webinar: Bring Web Content into the Modern Era with Ephox's EditLive! 9 Rich ...
Webinar: Bring Web Content into the Modern Era with Ephox's EditLive! 9 Rich ...Webinar: Bring Web Content into the Modern Era with Ephox's EditLive! 9 Rich ...
Webinar: Bring Web Content into the Modern Era with Ephox's EditLive! 9 Rich ...
 
Ephox corp's EditLive! rich text editor for IBM Connections to be Unveiled at...
Ephox corp's EditLive! rich text editor for IBM Connections to be Unveiled at...Ephox corp's EditLive! rich text editor for IBM Connections to be Unveiled at...
Ephox corp's EditLive! rich text editor for IBM Connections to be Unveiled at...
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Porting 100k Lines of Code to TypeScript

  • 1. tiny.cloud PORTING 100K LINES OF CODE TO TYPESCRIPT MILLIE MACDONALD SOFTWARE ENGINEER @ TINY
  • 2. The Talk ● About Tiny and me ● The Conversion Project: ○ Motivation ○ Why TypeScript? ○ How we converted our JavaScript to TypeScript ○ Benefits, lessons learned and ongoing work TL;DR: Types help. Gradual typing really helps.
  • 3. About Tiny ● TinyMCE is a popular open source project ● A rich text editing platform that helped launch and scale the adoption of Medium, WordPress, Shopify, Tumblr, Atlassian, EventBrite & more ● In 2015, we launched a startup with offices in Palo Alto, Australia and Sweden By the numbers: ● 1 million+ users on Cloud ● 125,000 downloads/week ● 1,000 paying customers ● 232 contributors
  • 4. About Me ● From Brisbane, Australia ● Bachelor of Engineering (Honours), Software Engineering ● Intern then QA Engineer then Software Engineer at Tiny ● Admin of the Australian gaming community for Splatoon
  • 5. “100k Lines of Code”? TinyMCE is: ● Core is ~30k LoC ● User Interface is ~30k LoC ● 44 core plugins + several premium plugins with ~40k LoC Plus various other projects and libraries…The point of conversion!
  • 7. Before ● Over the years, we tried and rejected a lot of frameworks ● We also use a functional programming style ● So we wrote our own libraries and frameworks ○ Avoided upgrade problems and framework churn ○ Easier for us to fix problems ○ FP + JS means defensive code, which resulted in code size and runtime performance issues ● Then ES2015 appeared...
  • 8. Motivation for the Conversion Project ● ES2015 features - especially a standard module system - were very enticing, so in 2017 we decided to convert our code. ● We had also been considering switching to an alternative JS language. Our wish list was: ○ static typing ○ pattern matching
  • 9. Why TypeScript? ● We looked at ReasonML, PureScript and TypeScript ● We considered: ○ how much of the functional programming feature set we needed ○ the learning curve for each language ○ the upfront development cost to switch
  • 10. Why TypeScript? ● ReasonML ○ Rewrote our PowerPaste plugin for TinyMCE, replacing complicated regex with strong types and pattern matching ○ Syntax is similar to JS so easy to learn ● PureScript: ○ Used for some of our internal prototypes ○ Side-effect tracking speeds up code reviews and training, and optics improve runtime efficiency ○ Lots of FP features we don’t need on the client side ● Neither is an easy drop-in replacement for the editor code
  • 11. Why TypeScript? ● Unfortunately... ○ No pattern matching ● But... ○ Vanilla JS is valid TypeScript ○ Due to already using a module system with strict syntax, converting our code could be mostly done with a script ○ Types! ○ Gradual types!
  • 12. What is Gradual Typing? ● Process of starting with minimal types and adding more over time ● Requires a mix of static and dynamic types - like TypeScript ● Requires minimal initial work ● Can invest as however much time and developers as you like in increasing types each week/month/etc. ● Can start with simple types then increase complexity as you learn and adjust to the type system ● Low initial cost of adding types, flexible ongoing cost
  • 14. Conversion Process for each Library 1. Run script to convert modules to ES2015 and TypeScript 2. Manually check the script converted each file correctly 3. Update with changes from already-converted dependencies 4. Fix problems found by TypeScript 5. Run tests and fix any problems 6. Update build process
  • 15. The Conversion Script 1. Transpile all modules using an Abstract Syntax Tree (AST) a. Change import syntax b. Pull code from inside the wrapping function to the root level c. Change export syntax and apply export default <any> 2. Move files and rename to .ts 3. Copy templates for configuration files 4. Generate Main.ts from api folder 5. Update package.json and .gitignore
  • 16. Module Conversion Example define( 'Example Module', [ ‘example.Dependency' ], function (Dependency) { var bar = 0; var foo = function () { … }; return { foo, bar }; } ); import { Dependency } from 'example'; var bar = 0; var foo = function () { … }; export default <any> { foo, bar };
  • 17. Adding Types ● The script doesn’t add types ● We started adding types to libraries, starting with the libraries most commonly depended on ● We add types with each PR ○ New code must be at least partially typed ○ “If you touch a file, type it” was the rule for a while ● Now, many of our libraries are at least partially typed
  • 18. Benefits of TypeScript and Gradual Typing
  • 19. Types ● Gained type-based tools e.g. find all usages and automated refactoring ● Compile time type checking ● Even just adding simple types caught some serious bugs ● Typing our most common dependencies caught many more
  • 20. Example Bugs Incorrect function argument list length Missing comment resulted in accidental global
  • 21. Gradual Typing ● Most of the team was able to go back to normal development work quickly ○ Sometimes devs take a couple days to type a library ○ We add some types with most PRs ● Conversion script used export default <any> so minimal errors and developer work upfront ● Gradually removing any from modules allowed us to type modules slowly and methodically
  • 22. Increased Readability ● Types make it easier to follow code ○ Especially in our giant projects where variables sometimes get passed around a lot ● Types make it easier to debug code ○ Type errors are generally nicely worded ○ Sometimes easier to match weird values to types, then track down which variable or function they come from ● Types make it easier for new developers to get up to speed and learn the code
  • 24. Convert Common Libraries First ● Because of our modular architecture we have a lot of libraries, and many depend on other libraries ● We mostly converted the most common dependencies first, so we could cascade their types and bug fixes through other libraries ● But some other libraries were done at the same time, and they have duplicate type definitions, clashing types, etc.
  • 25. Gradually Introduce Compiler Features A couple times we enabled compiler features without testing them properly on every library...
  • 26. Write a Script to Test Everything ● Write a script that does every kind of check it can - types, linting, build, tests, etc. ● Run it when you make a big change ● Ours is called the Really Cool ScriptTM ○ Contains a list of all our libraries ○ Clones each one, installs its dependencies, runs tsc and npm test
  • 27. Developer Adjustments ● Discuss changes in coding style, tools and processes ● Communicate changes ● Script pre-commit or pre-build type checks to help developers remember what to check
  • 29. Continuing Work ● More types! ● Enabling more compiler features ● Building a type definition file for TinyMCE’s editor API
  • 30. Check out TinyMCE ● TinyMCE is open source ○ https://github.com/tinymce/tinymce ○ Contains TS files and most config and build files ○ Master branch is TinyMCE 4 ○ 5.x branch for TinyMCE 5 ● Found a bug? Win some swag! https://go.tiny.cloud/blog/tinymce-5-developer-challenge/