Static typing in JavaScript
What is static typing?
What is static typing
- JavaScript has types but they are only known at runtime
- Static typing adds support for type checking to a language at compile time
- Can you look at a random piece of code and tell what might be the type of a variable before it runs?
- You can sometimes - it's called type inference
- But cannot in many cases - where one might need explicit type definitions
- Enforces a contract on the type of shape a value can take beforehand
Examples of statically typed languages
- Java, C, C++
- Haskell, Purescript, Elm
- Scala
- Fsharp
- Csharp
- Objective C
Pros of static typing
-
Thinking in types first
- Domain driven development with types
- Early bug detection
- Some unit tests are no longer needed
- Documentation which can be trusted at all times
-
Refactor with confidence
- Both small and full architecture refactors
- Editor/IDE support
- More information about a variable
- Better and more intelligent autocomplete
- Automatic refactorings
- In a big team, defines interfaces which everyone can understand
- Adds a layer of safety and correction
Cons of static typing
- Higher upfront cost
- Have to think about types before you see anything on the screen
- Learning curve
- Thinking in types is a skill which needs time to master
- Third party libraries might not be typed
- Even if they are typed, the types might be very shallow (e.g. "any" type for everything)
- Adds to development time
- For team new to react, better investments might be in
- react patterns
- redux patterns
- testing strategies
Available options
- Typescript
- More mature system
- Has better support for third party typed libraries
- Has better editor support system
- Specially with vscode
- Needs bigger commitment - all or none
- Flow
- Is new but allows gradual introduction to current system (not a problem we have right now)
- Has problem with a lot of breaking changes with every new release
- Statically typed languages which compile down to javascript - Elm, Purescript, Reasonml
Thanks
Static typing in JavaScript
Static typing in JavaScript
By mukeshsoni
Static typing in JavaScript
- 232