Static Types
in JavaScript
Eindhoven Front-end Meetup
JavaScript is a dynamic language
Doesn't really matter what type you use
Plain JS
let name = 'Gaya';
name = 42;
TypeScript
let name: string = 'Gaya';
name = 42; // Type '42' is not assignable to type 'string'.
Describe your functions
function giveName(name: string): string {
return name;
}
const name: string = giveName('Gaya');
const fakeName: number = giveName('Gaya');
// Type 'string' is not assignable to type 'number'.
giveName(42);
// Argument of type '42' is not assignable
// to parameter of type 'string'.
Makes your code a bit more predictable
If you like Object Oriented programming,
little weird but okay.
you'll like TypeScript.
In a way you're documenting your code
Intellisense / Hinting
Also for installed modules
Works great with frameworks
- Create React App
- Vue CLI
- Next.js
You can define complex stuff
- Promises and the resolved type
- Shapes of objects
- What arguments a callback will be called with
It will make refactoring easier
TypeScript is not JavaScript
- They add their own features
- .ts and .tsx files
The learning curve is steep
Prepare to use any
a lot
I found TypeScript setup to be a pain in the butt
Flow Type was a bit easier
Helps solidify your code
Slows down proof of concepts
TypeScript or Flow Type?
TypeScript. Mainly because of popularity