TypeScript-Compiler-Optionen

Der TypeScript-Compiler (tsc) verfügt über zahlreiche Compiler-Optionen. Dieses Tutorial bietet eine ausführliche Einführung in häufig verwendete Compiler-Optionen und ihre Zwecke.

SVG-Diagramm: TypeScript-Kompilierungsprozess Background Title TypeScript Compilation Process and Compiler Options Source Code TypeScript Source Code .ts / .tsx Contains Type Annotations Arrow 1 TypeScript Compiler TypeScript Compiler (tsc) Compiler Options Compiler Option Functions: • Type checking (strict) • Output format (module) • Target version (target) • Declaration files (declaration) ...More options Arrow 2 Compilation Output Compilation Output .js - JavaScript .d.ts - Type Declarations .map - Source Map Lower Section: Compiler Option Categories Compiler Option Categories Category 1 Type Checking strict noImplicitAny strictNullChecks Category 2 Output Control outDir declaration sourceMap Category 3 Module System module moduleResolution esModuleInterop Category 4 Language Features target lib jsx Arrow Marker

Optionen zur Ausgabesteuerung

Beispiel

{
    "compilerOptions": {
        // Ausgabeverzeichnis
        "outDir": "./dist",

        // Quellstammverzeichnis
        "rootDir": "./src",

        // .d.ts-Deklarationsdateien generieren
        "declaration": true,

        // Ausgabeverzeichnis für Deklarationsdateien
        "declarationDir": "./types",

        // Source-Maps generieren
        "sourceMap": true,

        // .js.map-Dateien generieren
        "mapRoot": "./map"
    }
}

Optionen für die Typprüfung

Beispiel

{
    "compilerOptions": {
        // Strict-Modus (empfohlen, immer zu aktivieren)
        "strict": true,

        // null und undefined prüfen
        "strictNullChecks": true,

        // this-Parameter prüfen
        "noImplicitThis": true,

        // Strikte Funktionstypen
        "strictFunctionTypes": true,

        // Strikte Eigenschaftsinitialisierung
        "strictPropertyInitialization": true,

        // Implizites any verbieten
        "noImplicitAny": true,

        // Rückgabe von void verbieten
        "noImplicitReturns": true,

        // Alle strikten Prüfungen aktivieren
        "strict": true
    }
}

Moduloptionen

Beispiel

{
    "compilerOptions": {
        // Modulsystem
        "module": "commonjs",

        // Strategie zur Modulauflösung
        "moduleResolution": "node",

        // Basispfad für die Auflösung
        "baseUrl": ".",

        // Pfad-Aliase
        "paths": {
            "@/*": ["src/*"]
        },

        // ES-Modul-Interoperabilität
        "esModuleInterop": true,

        // Default-Importe erlauben
        "allowSyntheticDefaultImports": true,

        // Module isolieren
        "isolatedModules": true
    }
}

ES-Funktionsoptionen

Beispiel

{
    "compilerOptions": {
        // Kompilierungsziel
        "target": "ES2020",

        // Aktivierte Bibliotheken
        "lib": ["ES2020", "DOM"],

        // Unbenutzte lokale Variablen zulassen
        "noUnusedLocals": true,

        // Unbenutzte Parameter zulassen
        "noUnusedParameters": true,

        // Code-Downleveling
        "downlevelIteration": true
    }
}

Experimentelle Optionen

Beispiel

{
    "compilerOptions": {
        // Dekoratoren aktivieren
        "experimentalDecorators": true,

        // Dekorator-Metadaten aktivieren
        "emitDecoratorMetadata": true,

        // Asynchrone Iteratoren aktivieren
        "emitDecoratorMetadata": true,

        // Bibliotheksprüfungen überspringen
        "skipLibCheck": true
    }
}

Häufige Kombinationen von Compiler-Optionen

Beispiel

// Empfohlene Konfiguration für Node.js-Projekte
{
    "compilerOptions": {
        "target": "ES2020",
        "module": "commonjs",
        "lib": ["ES2020"],
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "moduleResolution": "node",
        "declaration": true
    }
}

Tabelle der Compiler-Optionen

Kategorie Häufige Optionen
Ausgabe outDir, rootDir, declaration, sourceMap
Typüberprüfung strict, strictNullChecks, noImplicitAny
Modul module, moduleResolution, paths, esModuleInterop
ES-Funktionen target, lib, downlevelIteration
Experimentell experimentalDecorators, emitDecoratorMetadata

Zusammenfassung

  • Strict-Modus:Immer strict: true aktivieren
  • Zielversion:Wählen Sie das passende target abhängig von der Umgebung
  • Modulsystem:Wählen Sie module abhängig von der Bereitstellungsumgebung
  • Typdeklarationen:Aktivieren Sie declaration bei der Entwicklung von Bibliotheken
Andere Erweiterungen