Ściąga TypeScript: Opanuj podstawowe koncepcje i najlepsze praktyki
Instalacja, uruchamianie, typy, klasy – wszystkie podstawy.
Page content
Oto moje zwięzłe poradnik TypeScript obejmujące kluczowe pojęcia, składnię i przykłady kodu, do których często odwołują się programiści:

Poradnik TypeScript
Wprowadzenie
- Instalacja globalna:
npm install -g typescript - Kompilacja pliku:
tsc filename.ts - Sprawdzenie wersji:
tsc --version
Podstawowe typy
let a: number = 10;
let s: string = "TypeScript";
let b: boolean = true;
let arr: number[] = [1, 2, 3];
let tuple: [string, number] = ["TS", 2025];
let anyValue: any = "Flexible";
let unknownVal: unknown = 5;
let notDefined: undefined = undefined;
let notPresent: null = null;
Typy Zbioru i Literały
let id: number | string = 42;
let direction: 'left' | 'right' = 'left';
Alias Typu i Interfejsy
type Point = { x: number; y: number };
interface Person { name: string; age: number; }
let user: Person = { name: "Alice", age: 25 };
Funkcje
function sum(a: number, b: number): number {
return a + b;
}
const multiply = (a: number, b: number): number => a * b;
function log(msg: string): void { console.log(msg); }
Klasy
class Animal {
name: string;
constructor(name: string) { this.name = name; }
move(distance: number = 0): void { console.log(`${this.name} moved ${distance}`); }
}
Modyfikatory Dostępu
public(domyślny),private,protected,readonly
class Point {
constructor(private x: number, private y: number) {}
}
Typy Powszechne (Generics)
function identity(arg: T): T { return arg; }
let output = identity("myString");
Wymienione Typy (Enums)
enum ResourceType { BOOK, FILE, FILM }
let r: ResourceType = ResourceType.BOOK;
Assert Typów / Castowanie
let someVal: unknown = "Hello";
let strLength: number = (someVal as string).length;
Moduły
// Eksport
export function foo() {}
// Import
import { foo } from "./module";
Zaawansowane Typy
- Intersekcja:
type A = { a: number }; type B = { b: number }; type AB = A & B; // { a: number, b: number } - Wartości Warunkowe:
type IsString = T extends string ? true : false; - Mapowane:
type Readonly = { readonly [P in keyof T]: T[P]; } - Typ Szablonu Literałowego:
type EventName = `on${Capitalize}`;
Przydatne Typy Utilitarne
Partial, Required, Readonly, Pick, Omit, Record
Wężenie Typów (Narrowing)
- Użyj
typeofiinstanceofdo wązienia typów:
function padLeft(value: string, padding: string | number) {
if (typeof padding === "number") {
return Array(padding + 1).join(" ") + value;
}
return padding + value;
}
To podsumowanie dostarcza podstawowej składni i funkcji niezbędnych dla większości procesów deweloperskich z użyciem TypeScript.
Więcej narzędzi dla programistów i skróconych przewodników znajdziesz w Narzędzia dla Deweloperów: Kompletny przewodnik po nowoczesnych workflowch deweloperskich.
Przydatne linki
- Poradnik VSCode
- Poradnik Golang
- Poradnik Terraform - przydatne polecenia i przykłady
- Poradnik Docker
- Poradnik Docker Compose - Najbardziej użyteczne polecenia z przykładami
- Instalacja Node.js
- Instalacja Flutter
- Kodowanie - dekodowanie Base64 na Windows, Linux i Mac
- Dekodowanie i drukowanie tokenu JWT
- Skróty klawiszowe Ubuntu: Kompletny poradnik