TypeScript-Migration von JavaScript
Ein bestehendes JavaScript-Projekt schrittweise zu TypeScript migrieren.
Migrationsstrategie
- tsconfig.json hinzufügen
- .js in .ts umbenennen
- Schrittweise Typannotationen hinzufügen
- Strict Mode aktivieren
tsconfig.json konfigurieren
tsconfig.json
{
"compilerOptions": {
// Anfangsphase: gelockerte Konfiguration
"target": "ES2020",
"module": "commonjs",
"strict": false,
"noImplicitAny": false,
"strictNullChecks": false,
"skipLibCheck": true,
// JS-Dateien zulassen
"allowJs": true,
"checkJs": false,
// Ausgabeverzeichnis
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
"compilerOptions": {
// Anfangsphase: gelockerte Konfiguration
"target": "ES2020",
"module": "commonjs",
"strict": false,
"noImplicitAny": false,
"strictNullChecks": false,
"skipLibCheck": true,
// JS-Dateien zulassen
"allowJs": true,
"checkJs": false,
// Ausgabeverzeichnis
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
Strenge Prüfungen schrittweise aktivieren
In Phasen aktivieren
// Phase 1: Grundlegende Migration
{
"compilerOptions": {
"strict": false,
"noImplicitAny": false
}
}
// Phase 2: Typprüfung aktivieren
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true
}
}
// Phase 3: Vollständig strikt
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true
}
}
{
"compilerOptions": {
"strict": false,
"noImplicitAny": false
}
}
// Phase 2: Typprüfung aktivieren
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true
}
}
// Phase 3: Vollständig strikt
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true
}
}
JSDoc-Typannotationen
JSDoc in JavaScript verwenden, um Typen hinzuzufügen.
utils.js
/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
function add(a, b) {
return a + b;
}
/**
* @typedef {Object} User
* @property {number} id
* @property {string} name
* @property {string} email
*/
/**
* @param {number} id
* @returns {Promise<User>}
*/
function getUser(id) {
return fetch(`/api/users/${id}`).then(r => r.json());
}
* @param {number} a
* @param {number} b
* @returns {number}
*/
function add(a, b) {
return a + b;
}
/**
* @typedef {Object} User
* @property {number} id
* @property {string} name
* @property {string} email
*/
/**
* @param {number} id
* @returns {Promise<User>}
*/
function getUser(id) {
return fetch(`/api/users/${id}`).then(r => r.json());
}
Ausgabe:
JSDoc 注释添加成功
Typdeklarationsdateien
Deklarationen für Module ohne Typdefinitionen erstellen.
src/types/my-module.d.ts
declare module "my-module" {
export function doSomething(param: string): void;
export class MyClass {
constructor(options: { name: string });
name: string;
}
}
export function doSomething(param: string): void;
export class MyClass {
constructor(options: { name: string });
name: string;
}
}
declare-Schlüsselwort
Beispiel
// Globale Variable deklarieren
declare var GLOBAL_CONFIG: {
apiUrl: string;
version: string;
};
// Globale Funktion deklarieren
declare function myFunction(param: string): void;
// Namespace deklarieren
declare namespace MyNamespace {
function doSomething(): void;
}
// Verwendung
console.log(GLOBAL_CONFIG.apiUrl);
myFunction("hello");
MyNamespace.doSomething();
declare var GLOBAL_CONFIG: {
apiUrl: string;
version: string;
};
// Globale Funktion deklarieren
declare function myFunction(param: string): void;
// Namespace deklarieren
declare namespace MyNamespace {
function doSomething(): void;
}
// Verwendung
console.log(GLOBAL_CONFIG.apiUrl);
myFunction("hello");
MyNamespace.doSomething();
Ausgabe:
Declaration successful
Migrationswerkzeuge
- tsc --allowJs:JS-Dateien kompilieren
- checkJs:JS-Typen prüfen
- // @ts-check:Einzeldatei-Typprüfung
- // @ts-ignore:Fehler ignorieren
legacy.js
// @ts-check
// @ts-ignore
var result = someLegacyFunction();
// @ts-ignore
var result = someLegacyFunction();
Best Practices
- Migration von kritischen Modulen aus starten
- Unit-Tests hinzufügen
- Strict-Modus schrittweise aktivieren
- JSDoc-Kommentare verwenden
- Typdeklarationsdateien erstellen
Zusammenfassung
- Inkrementell:Schrittweise Migration
- JSDoc:Typannotationen
- Deklarationsdateien:.d.ts
- Strict-Modus:In Phasen aktivieren
Weitere Erweiterungen