dev-resources.site
for different kinds of informations.
Typescript quick concept refresher and reference
Published at
9/25/2024
Categories
webdev
typescript
cheatsheet
programming
Author
keshav___dev
Author
12 person written this
keshav___dev
open
TypeScript is a powerful superset of JavaScript that adds static types, making your code more robust and maintainable. Whether you're a beginner or an experienced developer, having a cheat sheet can be incredibly handy. This post provides a comprehensive cheat sheet to help you quickly reference key TypeScript concepts and syntax.
Basic Types
-
any
: A type that can be anything.
let variable: any;
-
boolean
: True or false values.
let isDone: boolean = false;
-
number
: All numbers, including integers and floats.
let decimal: number = 6;
-
string
: Textual data.
let color: string = "blue";
-
array
: Arrays of a specific type.
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];
-
tuple
: An array with a fixed number of elements of specific types.
let x: [string, number];
x = ["hello", 10];
-
enum
: A way to define a set of named constants.
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
-
void
: The absence of any type, commonly used for functions that do not return a value.
function warnUser(): void {
console.log("This is my warning message");
}
-
null
andundefined
: Their own types as well as subtypes of all other types.
let u: undefined = undefined;
let n: null = null;
Advanced Types
-
union
: A variable that can hold multiple types.
let value: string | number;
value = "hello";
value = 42;
-
type alias
: Creating a new name for a type.
type Name = string;
type NameOrNumber = Name | number;
-
interface
: Describes the shape of an object.
interface Person {
firstName: string;
lastName: string;
}
let user: Person = { firstName: "John", lastName: "Doe" };
-
type assertion
: Telling the compiler to treat a variable as a different type.
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
Functions
- Function Types: Describing the types of functions.
function add(x: number, y: number): number {
return x + y;
}
let myAdd: (x: number, y: number) => number = function(x, y) { return x + y; };
- Optional and Default Parameters: Parameters that are optional or have default values.
function buildName(firstName: string, lastName?: string): string {
return firstName + " " + (lastName || "");
}
Classes
- Basic Class: Defining a class with properties and methods.
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
- Inheritance: Extending a class to create a new class.
class Animal {
move(distanceInMeters: number = 0) {
console.log(`Animal moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal {
bark() {
console.log("Woof! Woof!");
}
}
let dog = new Dog();
dog.bark();
dog.move(10);
Generics
- Generic Functions: Functions that work with any data type.
function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>("myString");
- Generic Classes: Classes that work with any data type.
class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
}
let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; };
Utility Types
- Partial: Makes all properties in a type optional.
interface Todo {
title: string;
description: string;
}
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
return { ...todo, ...fieldsToUpdate };
}
- Readonly: Makes all properties in a type read-only.
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: "Delete inactive users"
};
Conclusion
This cheat sheet covers the essentials of TypeScript, providing a quick reference for common types, functions, classes, and more. Keep it handy as you work on your TypeScript projects to streamline your development process and ensure you're using the language to its full potential.
cheatsheet Article's
30 articles in total
Vim cheat sheet
read article
The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh
read article
From FZF file preview to a browser for cht.sh to discovering the ideal solution
read article
Seaborn Cheat Sheet
read article
SQL Quick Reference: Simplifying Database Management
read article
Terraform Commands Cheat Sheet
read article
JavaScript Interview Cheat Sheet - Part 1
read article
JavaScript Interview Cheat Sheet - Part 2
read article
Arch Linux Pacman: A Detailed Guide with Commands and Examples π©π§
read article
The Art of AI Conversation: 6 Essential Tips for Chat LLM Success
read article
Master CSS Selectors
read article
End-to-End Flexbox vs. Grid vs. Traditional.
read article
Linux Commands Cheat Sheet :)
read article
sql joins: moving in together
read article
A Yocto Cheatsheet
read article
Typescript quick concept refresher and reference
currently reading
cheat sheet for go mod package management
read article
Git para Iniciantes: Tudo o que vocΓͺ precisa saber para comeΓ§ar a usar
read article
Git Commands You Need for Hacktoberfest 2024 - Git cheat sheet
read article
Git Cheatsheet that will make you a master in Git
read article
How to learn HTML: 46 great sites, courses and books (all free)
read article
Top 5 Cheat sheets for Developers
read article
CSS: List of Properties for Text
read article
What's in a name?
read article
The HTML History and Optimization Cheat Sheet
read article
Kubernetes Cheat Sheet: Essential Commands for Beginners
read article
π¦ GitLab Cheatsheet - 16 - CICD Catalog
read article
π SQL Cheat Sheet for Developers
read article
The Ultimate SQL JOIN Cheat Sheet
read article
JavaScript Cheat Sheets
read article
Featured ones: