Beginner's Guide to TypeScript

i'm trav
- i work at universal music
- head of forking at crablang
- streaming bad code live on twitch
- terrible takes @techsavvytravvy
- find me in the crablang lounge on discord
i've also been known to make music
Search for a command to run...

i'm trav
i've also been known to make music
No comments yet. Be the first to comment.
In this series, I will be going over the basics of popular and emerging technologies.
An overview of one of the most popular and loved programming languages.
I've always been intrigued by browser extensions and their ability to do crazy things and make the web cooler + get rid of ads. That said, going from concept to a working Chrome extension can be super annoying with repetitive setup tasks, configurati...

This is going to be a "quick" little guide to customizing the tmux status bar to include a scrolling Spotify “Now Playing” widget. I’m going to be doing this on macOS, but you should be able to adapt it to Linux and I'll try to assist where I can. If...

Go From Freelancer to Corporate Sellout In 43 Easy Steps

An overview of one of the most popular and loved programming languages.

The Importance of Mental Health as a Developer

If you're new to TypeScript and want to know more then you've come to the right place!
TypeScript was created to relieve some of the pain-points of JavaScript. Using TypeScript can help you avoid bugs you would otherwise encounter when writing regular JS. It is also Open Source, and has wide adoption and support.
This guide includes everything you need to get started with TypeScript, and nothing you don't.
TypeScript is a programming language that is a typed superset of JavaScript. That means that it contains all of the functionality of JavaScript, but with additional features that make it more powerful and easier to use. TypeScript is compiled to plain JavaScript, so it can be used in any JavaScript environment.
TypeScript is a great way to improve your code. It can catch errors and help you write more maintainable code.
TypeScript offers developers a number of benefits, including:
TypeScript provides static type checking. This can help you find bugs in your code more easily.
TypeScript code can be compiled to JavaScript code that runs on any browser or platform. The compiling process also allows developers to catch errors before runtime.
TypeScript comes with a number of tools that make it easy to work with, including an interactive shell and a compiler.
To get started with TypeScript, you will need to install it. The easiest way to do this is through npm.
Once you have TypeScript installed, you can compile TypeScript files using the tsc command. This will generate a corresponding JavaScript file.
tsc filename.ts
You can also use the -w flag to watch for changes and automatically recompile your files.
tsc -w filename.ts
TypeScript offers a number of basic types that you can use to annotate your variables.
let foo: string = "bar";
let baz: number = 42;
let qux: boolean = true;
You can also use the any type if you don't want to annotate your variables, though it's not recommended.
let foo: any = "bar";
let baz: any = 42;
let qux: any = true;
Interfaces are a powerful way to define contracts in your code. They can be used to define the shape of objects and functions.
interface Point {
x: number;
y: number;
}
let p: Point = { x: 0, y: 0 };
You can also use interfaces to define the types of function parameters and return values.
interface Point {
x: number;
y: number;
}
function addPoints(p1: Point, p2: Point): Point {
return { x: p1.x + p2.x, y: p1.y + p2.y };
}
let p1: Point = { x: 0, y: 0 };
let p2: Point = { x: 1, y: 1 };
let p3: Point = addPoints(p1, p2);
Classes are a way to encapsulate data and behavior. They can be used to define types and create objects.
class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
add(p: Point) {
return new Point(this.x + p.x, this.y + p.y);
}
}
let p1: Point = new Point(0, 0);
let p2: Point = new Point(1, 1);
let p3: Point = p1.add(p2);
Modules are a way to organize your code. They can be used to define types and encapsulate behavior.
module MyModule {
export class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
add(p: Point) {
return new Point(this.x + p.x, this.y + p.y);
}
}
}
let p1: MyModule.Point = new MyModule.Point(0, 0);
let p2: MyModule.Point = new MyModule.Point(1, 1);
let p3: MyModule.Point = p1.add(p2);
Decorators are a way to add metadata to your code. They can be used to annotate classes, properties, and methods.
@Component({
selector: 'my-component',
template: '<div>Hello, world!</div>'
})
class MyComponent {}
That's all you need to know to get started with TypeScript. For more information, check out the TypeScript Handbook.
Let me know in the comments if this was helpful or if you have any questions.
Be sure to follow me for more like this!