I have been using ES2015 actively using Babe.js and I was already convinced that compiling more dev-friendly language to javascript is useful. One of the langauges that has been praised a lot in this domain is Typescript.
Install Typescript
$ yarn global add typescript
If you get an error saying tsc
command is not found, check out this
SO answer on this issue.
Linked list in Typescript
Here is a linked list you can start to play with written in Typescript.
// index.ts
import { Node } from './node';
export class List {
head: Node;
tail: Node;
constructor() {
}
push(val) {
let node = new Node(val);
node.next = this.head;
this.head = node;
}
print() {
let node = this.head;
while (node !== undefined) {
console.log(node.val);
node = node.next;
}
}
}
// list.ts
import { Node } from './node';
export class List {
head: Node;
tail: Node;
constructor() {
}
push(val) {
let node = new Node(val);
node.next = this.head;
this.head = node;
}
print() {
let node = this.head;
while (node !== undefined) {
console.log(node.val);
node = node.next;
}
}
}
// node.ts
export class Node {
val: number;
next: Node;
constructor(val) {
if (!val) {
return;
}
this.val = val;
}
}
Now compile typescript into javascript.
$ tsc index.ts
This should generate index.js
, list.js
and node.js
.
Let's run these javascript files.
$ node index.js
This should print
3
2
1
What I think about Typescript after using it for an hour
Learning Typescript was intuitive and JS I was writing felt more precise and controlled. This allowed me write less error prone javascript in a short period time. I should learn more about tooling around Typescript so that it feels more integrated into the javascript development workflow.