github:https://github.com/Jessie-jzn
網址:https://www.jessieontheroad.com/
1。靜態型別檢查
TypeScript 的核心優勢是它的靜態類型檢查,這有助於在編譯階段而不是運行時捕獲常見錯誤。這增強了程式碼的可靠性和穩定性。
2。增強的程式碼編輯體驗
TypeScript 的類型系統可以在編輯器中實現更準確的程式碼補全、重構、導航和文件功能,顯著提高開發效率。
3。提高程式碼可維護性
類型聲明使理解程式碼意圖和結構變得更容易,這在團隊開發環境中特別有益。
4。高階語言功能
TypeScript 支援 JavaScript 中不存在的進階功能,例如介面、枚舉和泛型,有助於開發更結構化和可擴展的程式碼。
5。更好的工具支援
TypeScript 提供各種編譯器選項來最佳化產生的 JavaScript 程式碼,並透過將 TypeScript 編譯為相容的 JavaScript 來支援不同的 JS 環境。
TypeScript | JavaScript | |
---|---|---|
Type System | Static typing with compile-time type checks. Types can be specified for variables, function parameters, and return values. | Dynamic typing with runtime type checks, which can lead to type-related runtime errors. |
Type Annotations | Supports type annotations to explicitly define types. E.g., let name: string = "Alice"; | No type annotations. Types are determined at runtime. |
Compilation | Requires compilation to JavaScript. TypeScript compiler checks for type errors and generates equivalent JavaScript code. | Runs directly in browsers or Node.js without a compilation step. |
Object-Oriented Programming | Richer OOP features such as classes, interfaces, abstract classes, and access modifiers. | Basic OOP features with prototype-based inheritance. |
Advanced Features | Includes all ES6 and ES7 features, plus additional features like generics, enums, and decorators. | Supports ES6 and later standards, but lacks some of the advanced features provided by TypeScript. |
ジェネリックを使用すると、型安全性を確保しながら、関数、クラス、インターフェイスが任意の型で動作できるようになります。
例:
function identity<T>(arg: T): T { return arg; } const numberIdentity = identity<number>(42); const stringIdentity = identity<string>("Hello");
この例では、アイデンティティ関数は汎用の
let anyVar: any; let unknownVar: unknown; anyVar = 5; anyVar.toUpperCase(); // No compile-time error, but might cause runtime error unknownVar = "Hello"; if (typeof unknownVar === "string") { unknownVar.toUpperCase(); // Type check ensures safety }
const obj = { name: "John" }; obj.name = "Doe"; // Allowed interface User { readonly id: number; name: string; } const user: User = { id: 1, name: "John" }; user.name = "Doe"; // Allowed user.id = 2; // Error, `id` is readonly
デコレーターは、メタデータの追加、またはクラス、メソッド、プロパティ、パラメーターの変更を可能にする特別な TypeScript 機能です。
タイプ:
例:
function sealed(constructor: Function) { Object.seal(constructor); Object.seal(constructor.prototype); } @sealed class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return `Hello, ${this.greeting}`; } }
function logMethod(target: any, propertyName: string, descriptor: PropertyDescriptor) { const originalMethod = descriptor.value; descriptor.value = function (...args: any[]) { console.log(`Method ${propertyName} called with args: ${JSON.stringify(args)}`); return originalMethod.apply(this, args); }; } class Calculator { @logMethod add(a: number, b: number): number { return a + b; } }
使用法:
デコレーターは、tsconfig.json で ExperimentalDecorators を true に設定することで有効になります。
interface と type は両方ともオブジェクト タイプを定義するために使用されますが、いくつかの違いがあります。
interface | type | |
---|---|---|
Basic Usage | Defines the shape of objects, including properties and methods. | Defines primitive types, object types, union types, intersection types, etc. |
Extension | Supports declaration merging. Multiple declarations of the same interface are automatically merged. | Does not support declaration merging. |
Union and Intersection Types | Not supported. | Supports union (` |
Primitive Type Aliases | Not supported. | Supports aliasing primitive types. |
Mapped Types | Not supported. | Supports mapped types. |
Class Implementation | Supports class implementation using implements. | Does not support direct class implementation. |
タイプ
以上是【面試精要】常見TypeScript面試題的詳細內容。更多資訊請關注PHP中文網其他相關文章!