Make JavaScript Java again

Tony Ruby
5 min readJan 2, 2020

--

Do we need TypeScript in 2020?

Make JavaScript Java again

TypeScript became one of must-have skills for modern web developers. In 2019 it’s included in GitHub top 10 languages, Create react app fully added its support and you can find a lot of others proofs of its growing popularity. At the same time such languages like Java and C are continuinue losing their positions.

When we talk about TypeScript advantages usually we consider next list:

  • TypeScript supports static typing
  • TypeScript makes code easier to read and understand
  • TypeScript can help us to avoid painful bugs that developers commonly run into when writing JavaScript by type checking the code
  • TypeScript encourages developers to use best OOP practices
  • As a result TypeScript saves developers time

What is interesting about this list is that we accept it as a truth without any critical thinking. Today I want to invite you to go deeper in every point and figure out if it is really so beneficial for us.

Since the main idea of typescript is decreasing amount of bugs in our code, first of all let’s define why it’s so important? The answer is pretty simple: the less number of bugs saves pricy time of developers and QA engineers therefore we can get valuable product cheaper and it can start bring benefits earlier.

Keeping this in mind let’s try to find out how TypeScript helps us to be more efficient and increase our performance.

Static typing — magic pill against bugs?

The main feature of TypeScript is static types support. It’s common belief that dynamic typing is one of main source of all JavaScript developers problems.

I really wonder what is so bad about dynamic typing? Why languages like Python or Ruby don’t meet such huge amount of criticism about it?

I can only assume that the problem is not in dynamic typing itself, but in the types conversion. Sometimes it’s really frustrating to see something like this:

0 == ‘0’; 0 == []; [] != ‘0’

But it’s about the lack of JavaScript knowledge, not about JavaScript itself. If you have a rapid sport car, but you driver skills are not really superb — are you going to enforce manufacturer artificially decrease speed of this car? Or you should learn advanced driving skills and became really great proffessional? This is what TypeScript offers to us — restrict dynamic typing instead of encouraging developers learn JavaScript properly.

Other question to dynamic typing opponents is if strict typing is so effective why we still able to find bugs in Java or C# programms? Yes, we can catch errors related to types on compilation stage but let’s be honest — it’s not enough. We have to follow SOLID principles and other best practices to ensure quality of code. But again it’s more about developers skills and knowledge, not about programming language.

Is TypeScript easier to understand?

Let me introduce 2 Redux Thunk examples:

const getUsers = () => async dispatch => {
...
try {
const users = await APIService.get('/users')
dispatch(successGetUsers(users))
} catch(err) {
dispatch(failedGetUsers(err))
}
}

and .ts version:

const getUsers = (): ThunkAction<void, {}, {}, AnyAction> => 
async (dispatch: ThunkDispatch<{}, {}, AnyAction>) => {
...
try {
const users: User[] = await APIService.get('/users')
dispatch(successGetUsers(users))
} catch(err) {
dispatch(failedGetUsers(err))
}
}

What all these generics means? For what purposes there’re two empty objects? How many time we should spend for documentation reading to understand it? It’s ok for redux thunk, because it’s widly spread Redux middleware with great support team and documentation. And what if code we have to maintain doesn’t have this?

No painful bugs anymore?

In previous example you saw how TypeScript makes code more verbose, and as we know — the bigger and more complex system the more opportunities to make some mistake. Along with errors we could make in our JavaScript code we have to pay additional attention if we haven’t made a typo in interface definition, in generics and so on. For the sake of truth need to say that compiler help us to find and fix such problems quick but without TypeSctipt we won’t meet such obstacles at all.

Best OOP practices

All we know that we must follow best practices if we want to create high quality easy to scale and maintainable code. And TypeScript has ability to help us with it. Sounds great and let’s consider simple Express example.

userRoute.js

const router = express.Router()router.get('/users', (req, res) => {
//get users from DB
res.json(users)
})
router.post('/users', (req, res) => {
//create user
res.json(userCreated)
})

userRoute.ts

class UserRouter {  public router = express.Router() 
public address = '/users'
constructor() {
this.initRoutes()
}
initRoutes() {
this.router.get(this.address, this.getUsers)
this.router.post(this.addressm this.createUser)
}
getUsers(req: express.Request, res: express.Response) {
//get users from DB
res.json(users)
}
createUser(req: express.Request, res: express.Response) {
//create user
res.json(userCreated)
}
}

First we can see — we need much more code to write. We use classes in order to write in more OOP way. We will have to create instances of these classes later to be able to use it. In addition we can use strict types, interfaces all other stuff. And in order not to make complete chaos in our code we’ll have nothing to do bur follow best practices. This is what is called “encourage to follow best practices”.

When developers voted for simple, agile and dynamic languages like JavaScript and Python, when functional programming paradigm shown all its advantages and ability to solve many problems gracefully and more efficient (and in JavaScript we can combine both paradigms), TypeScript enforces us to write in old-fashioned Java/C# OOP way.

To sum up we saw that TypeScript doesn’t really saves our time, it neither protect us from plenty of bugs, nor increases out performance. In addition it requires us to be more verbose, makes us spend additional time for extra configuration, writing type definitions, reading more documentation. Newcomers rather write not purfect but working application with pure JavaScript or Ruby while you will create perfect software according to all principles TypeScript app. And later they will hire you to rewrite their startup where you can use TypeScript and do things right :)

It’s really funny to see that massive verbose overcomplicated languages like Java are going away because they couldn’t compete with more dynamic and simple languages and later these simple languages are going to refuse all things which help them to grow so fast and tend to be more heavy, verbose and overcomplicated.

I see this situation like: “Ok, we don’t want Java anymore, we want JavaScript. But we don’t like JavaScript as is, let’s make it more like Java. Ok, now we have everything we had in Java, but it’s not a Java. Let’s rock!”

--

--