Monthly Archives: December 2017

TypeScript

What is it?
It’s Typed-JavaScript. Javscript doesn’t have types, remember?
So you can do ridiculous things like:

var a = 2;
if( randomFunctionIsTrue() ) a = "2";
console.log( a+2 );

Sometimes, this might produce 4 and sometimes it produces “22”. Who knows.

So this is TypeScript

let a: number = 2;
a = "2" // throws compile time error

TypeScript prevents you from doing stupid stuff.

Notice I said TypeScript throws compile time error. TypeScript files (which end in .ts) compiles to .js files.

In the end, TypeScript is just another syntax for javascript and it creates javascript and runs anywhere that javascript can (frontend, backend, standalone).

But wait, there’s more
You may know that you can write classes in javascript, but they don’t look like classes. That’s because they’re functions. With TypeScript, you can write classes and interfaces that actually look like classes and interfaces.

interface Animal {
...
}

class Dog implements Animal {
...
}

This is not Java. This is TypeScript.

Why use it?
If you’re familiar with Java, you’ll adapt to TypeScript easily.

Because it’s typed, you catch errors early on in compile time, rather than at some arbitrary time during a production run.

Because it’s typed, IDE’s can leverage the compiler hints to give you heads up when there are potential errors. Think the power of Eclipse on java, applied to javascript.

It does everything javascript can because it compiles to javascript. This is just like scala compiling to java bytecode.

You can download a Starter kit example that shows a few interfaces and classes that performs a http request and mysql insert.

Advertisements
Tagged ,

Setting up your python environment

If you’ve worked with python but never used virtualenv, then you will probably have encountered this problem. You are working on 2 projects. One requires version X of a library; the other requires version Y of the same library.

So you use virtualenv to create isolated python environments so you don’t have messy and conflicting dev environments in your system.

But here’s another tool that’s a bit easier to use. It’s a wrapper for virtualenv called virtualenvwrapper.

Here’s the basics.

Download it

pip install virtualenvwrapper

Now add these 2 lines to your ~/.bash_profile (if you’re on OSX)

export WORKON_HOME=~/virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

(Note, the 2nd line may be different depending on where virtualenvwrapper was installed)

Create a virtual env

mkvirtualenv env1

Exit from the virtual env

deactivate

Next time you want to work on that project again

workon env1

If you forgot what virtual envs you have created, you can list them

lsvirtualenv
Tagged , ,