TYPESCRIPTS
TypeScript for Python Developers
A quick introduction to TypeScript for experienced Python developers
If you already know Python, you might be interested in learning TypeScript. It is getting more and more popular. It works in your browser. And in many facets, it is a very similar language.
However, most places to learn a programming language are focused on new developers. Very few articles or tutorials cut down to the essentials of the new language while leaving out the basic of programming. This article will provide a brief but dense overview how TypeScript works, and how you can get started easily. It contains the basic syntax, loops, types, shorthands are more.
The basics
TypeScript is a superset of Javascript. So any Javascript code is valid TypeScript code but not the other way around. TypeScript compiles into JavaScript code, which can then be interpreted by the executor, a browser or NodeJS.
TypeScript can be used as a front end language, generally as part of a framework or library. Or as a backend with node.js, often as a webserver but in theory also as a pure backend.
Syntax
Like most other languages, TypeScript uses brackets to create scopes, versus Python’s indentation based style. CamelCase TypeScript is also the convention versus Python’s snake_case .
Basics
Here are a list of basics to get started, first as Python code and then TypeScript code. I’ll use Python’s typing to compare to TypeScripts typing.
Assignments
First of all, assignment in TypeScript is done with a keyword. There are three key words: var, let and const. Learning this is quite easy: use const if the value is a constant and won’t change, use let is the value is going to change and don’t use var . The reason to not use var is about variable scoping, and is not covered here.
Types
These compare mypy types with TypeScript. The format is Python — TypeScript. A complete list of TypeScript types can be found here.
int, float, double, long—numberstr—stringList[T]—Array<T>orT[], withTbeing any other typeDict—ObjectTuple[str, int, dict]—[string, number, object]TypedDict— Custom interface
TypeScript and JavaScript only has one types for numbers without a specific reference to integers or floats. In some sense it makes sense, but makes casting to integers annoying.
Get Parthpritam’s stories in your inbox
Join Medium for free to get updates from this writer.
While talking about types, someone cannot skip the two different empty values in TypeScript: null and undefined.
nulldenotes an non-existing object or the fact that there is no input. It can be compared to Python’sNone.undefinedis returned when an attribute is referenced that does not exist. It is similar to Python’sKeyErrororAttributeError.
TypeScript doesn’t error out if you reference an non-existing value on an object. However, referencing anything on undefined does throw a TypeError. Both are falsy, and undefined i snot equal to null.
Conclusion
The good
- The TypeScript compiler and typechecker feels much better than mypy. For VS Code it also seems to have better compatability
- Null checking is super usefull, especially when working with user data.
- The shorthands can safe a lot of time
- It’s basically the only language accepted in a browser, and can also be used on a server. This allows for full JS/TS stacks
- I had a lot of fun with RxJS, which forced me to think in a functional way
- It’s a very flexible language (like Python), so you can always find a style that suits you
- A lot of libraries and a huge community
The bad
- Working with dates is really a pain in the ass, especially coming from datetime and dateutil
- Running a script is slightly more annoying as you can’t just to python script.py
- Some of the libraries are not as high quality as some in Python







