SlideShare a Scribd company logo
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?

More Related Content

PDF
PHP vs Node.js
PDF
Adopting language server for apache camel feedback from a java/Eclipse plugi...
PDF
Typescript for the programmers who like javascript
PPTX
TypeScript Overview
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
PDF
Power Leveling your TypeScript
PDF
Swift for back end: A new generation of full stack languages?
PPTX
Typescript Fundamentals
PHP vs Node.js
Adopting language server for apache camel feedback from a java/Eclipse plugi...
Typescript for the programmers who like javascript
TypeScript Overview
TypeScript - Silver Bullet for the Full-stack Developers
Power Leveling your TypeScript
Swift for back end: A new generation of full stack languages?
Typescript Fundamentals

What's hot (20)

PPT
Learning typescript
PPTX
TypeScript intro
PDF
Intro to Crystal Programming Language
PDF
TypeScript Best Practices
PDF
Introduction to TypeScript by Winston Levi
PPT
TypeScript Presentation
PPTX
Typescript in 30mins
PDF
Crystal
PDF
Introduction to Angular for .NET Developers
PDF
TypeScript: Angular's Secret Weapon
PPTX
Typescript ppt
PDF
Introduction to TypeScript
PPTX
TypeScript . the JavaScript developer best friend!
PDF
Typescript - MentorMate Academy
PPTX
Ruby, the language of devops
PDF
Getting Started with TypeScript
PDF
Getting Started with the TypeScript Language
PPTX
Getting started with typescript
PPTX
AngularConf2015
PDF
Thinkful - Intro to JavaScript
Learning typescript
TypeScript intro
Intro to Crystal Programming Language
TypeScript Best Practices
Introduction to TypeScript by Winston Levi
TypeScript Presentation
Typescript in 30mins
Crystal
Introduction to Angular for .NET Developers
TypeScript: Angular's Secret Weapon
Typescript ppt
Introduction to TypeScript
TypeScript . the JavaScript developer best friend!
Typescript - MentorMate Academy
Ruby, the language of devops
Getting Started with TypeScript
Getting Started with the TypeScript Language
Getting started with typescript
AngularConf2015
Thinkful - Intro to JavaScript
Ad

Similar to Porting 100k Lines of Code to TypeScript (20)

PDF
Python Programming .pdf
PPTX
MODULE 2.pptx Python data handling types function
PPTX
Writing clean scientific software Murphy cleancoding
PDF
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
PPTX
Benefits of Typescript.pptx
PDF
Applied Machine learning for business analytics
PDF
Type script
PDF
Class_X_PYTHON_J.pdf
PPTX
Design Like a Pro: Scripting Best Practices
PDF
AirBNB's ML platform - BigHead
PDF
Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
PPTX
Design Like a Pro: Scripting Best Practices
PDF
Go at uber
PDF
Keeping code clean
KEY
Programming with Python: Week 1
PDF
ppt notes for python language variable data types
PPTX
TypeScript and Angular2 (Love at first sight)
PDF
Managing Translation Workflows in Drupal 7
PPT
Intro to Python
PPTX
Moving From JavaScript to TypeScript: Things Developers Should Know
Python Programming .pdf
MODULE 2.pptx Python data handling types function
Writing clean scientific software Murphy cleancoding
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Benefits of Typescript.pptx
Applied Machine learning for business analytics
Type script
Class_X_PYTHON_J.pdf
Design Like a Pro: Scripting Best Practices
AirBNB's ML platform - BigHead
Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
Design Like a Pro: Scripting Best Practices
Go at uber
Keeping code clean
Programming with Python: Week 1
ppt notes for python language variable data types
TypeScript and Angular2 (Love at first sight)
Managing Translation Workflows in Drupal 7
Intro to Python
Moving From JavaScript to TypeScript: Things Developers Should Know
Ad

More from Tiny (16)

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

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
sap open course for s4hana steps from ECC to s4
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

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/