Kotlin for Typescript Developers
Tags: kotlin, typescript, javascript
Published: 2020-10-12 22:10:05

I have been on a language bender lately, no matter what I try I always always end up returning to Typescript, this is probably more of a case of, "you know what you know", and "I can be productive with typescript" but what happens when you want Web Assembly powers? Well you could try native script, but I have found the experience with Native script was underwhelming and not really as easy as say Rust bindgen.

## Elephant in the room 🐘
The following stacks are possible.

| **Frontend** | **Backend**  |
|--------------|--------------|
| Typesript    | Typescript   |
| Kotlin       | Kotlin       |
| Kotlin       | Typesript    |
| Typescript   | Kotlin       |

So what's the deal? Both languages compile to thier respective targets, for Typescript, that's just JavaScript and impressively all of its past and future versions.
But for Kotlin, it's compiled to JVM bytecode or LLVM bytecode, so native and web assembly is also possible, something I dearly wished typescript would do, oh and Kotlin compiles to JavaScript too.

Wow Kotlin wins a different kind of compilation target war? It's scope is wider. For me I would still use Typescript on the frontend because, it looks a bit like JavaScript, that's the only reason honestly, I like my ducks to quack and look like ducks.

For exactly the same reason I would not use node and typescript on the backend and Kotlin on the Frontend, despite the contra sounding actually pretty reasonable.

### Use Cases
I started writing WASM with emacripten and C++ but I admit that's not a language I enjoy, in fact a lot of professional C++ programmers openly admit the language has some flaws but they love the speed. For me I'm just trying to make a game for fun, I would like ease of use, I think that kind of writes off Rust for this particular project as well, it's a fantastic pairing with typescript with rust-bindgen (look it up!).

So what looks like Typescript, runs pretty quickly, has arguably better features that I always wished JS could do (sorry JavaScript, I do love you) and compiles to a lot of things, Java of course! Oh did I say Java 😳, I meant Kotlin heh, not the same thing.. Kotlin is not Java, but you can run Java code inside Kotlin, that's nice, we have also Kotlin native can run C code (a common feature of most LLVM backed compiled languages to be fair). You can see that you would have a vast array of targets and libraries to choose from, far beyond just NPM, and that's saying something.

### Why not Typescript Today?
`1 + 1` is the answer, did you know that JavaScript under the hood does a hell of a lot to compute this? 
"Is it a string, maybe it's something else I can add together? No it's a number and another number, no need to cast, oh yes the answer is `2`". Thanks for the comentary JavaScript you say, but then you think, "wait I just did a lot of work to typedef all this code in Typescript only for you to go and guess?" Yes the runtime is still JavaScript, we can't avoid that, you can accept that and I personally have but always at the back of my mind thinking, this is secretly shity that I can't just tell js what to do.

### Kotlin is not Android
Kotlin is JetBrains offering to the JVM and an official Android supported language, (cool so learning this might lead to other avenues), however this fact means that Kotlin gets the same wrap as Typescript did, synonymous with OOP despite it's capability to just be JavaScript with types.

### Fullstack Kotlin and Typescript?
My day job is JavaScript, it is crucial that I don't move away from that, for me I can't afford to use Kotlin as a JavaScript transpiler. Because context switching is expensive, so let's leave it at that. Typescript on the Frontend with Kotlin on the Backend, now let's get to the good bit, the cost of context switching between Kotlin and Typescript.

```kt
println("Hello, world!")
```
``` ts
console.log("Hello, world!");
```
Not bad, not bad, now how about something a little less complex.
``` kt
var myVariable = 42
myVariable = 50
val myConstant = 42
```
``` ts
let myVariable = 42;
myVariable = 50;
const myConstant = 42;
```
Now this one's interesting, JavaScript has var but it sucks and nobody likes it anymore, Kotlin never had a var beforehand and so just uses var as let, const is simply val, I am val I do not change, not making a big song and dance about it.
``` kt
val explicitDouble: Double = 70.0
```
``` ts
const explicitDouble: number = 70;
```
Two things that sort of bug me, why are ts built in primitive types lowercase, and why can't we have more number types when JavaScript supports alot of different number types... (kind of), Kotlin, no problem here's a double.
``` kt
val apples = 3
val oranges = 5
val fruitSummary = "I have ${apples + oranges} " +
                   "pieces of fruit."
```
``` ts
const apples = 3;
const oranges = 5;
const fruitSummary = `I have ${apples + oranges} ` +
                   "pieces of fruit.";
```
So this looks normal, oh wait, no backticks, because 2 quotes is enough.

The list goes on and on, there are a lot of features that Typescript can't do because JavaScript can't do. Here's the full list. You might start to get all warm and fuzzy as you read this. Wow Kotlin I like you!

[Kotlin is like Typescript](https://gi-no.github.io/kotlin-is-like-typescript/)

### Why does Kotlin look a bit like Typescript
Perhaps it's convergent evolution or perhaps it's design was inspired by a common ancestor, either way who cares, the main thing is, Kotlin's logo is slightly cooler.

Thanks for reading!