github: https://github.com/Jessie-jzn
website:https://www.jessieontheroad.com/
1. Static Type Checking
TypeScript’s core advantage is its static type checking, which helps catch common errors during the compile phase rather than runtime. This enhances code reliability and stability.
2. Enhanced Code Editing Experience
TypeScript’s type system enables more accurate code completion, refactoring, navigation, and documentation features in editors, significantly boosting development efficiency.
3. Improved Code Maintainability
Type declarations make understanding code intentions and structure easier, which is particularly beneficial in team development environments.
4. Advanced Language Features
TypeScript supports advanced features not present in JavaScript, such as interfaces, enums, and generics, facilitating the development of more structured and scalable code.
5. Better Tool Support
TypeScript offers various compiler options to optimize generated JavaScript code and supports different JS environments by compiling TypeScript to compatible JavaScript.
|
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. |
Generics allow functions, classes, and interfaces to work with any type while still enforcing type safety.
Example:
function identity<T>(arg: T): T { return arg; } const numberIdentity = identity<number>(42); const stringIdentity = identity<string>("Hello");
In this example, the identity function uses a generic
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
Decorators are a special TypeScript feature that allows adding metadata or modifying classes, methods, properties, or parameters.
Types:
Examples:
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; } }
Usage:
Decorators are enabled by setting experimentalDecorators to true in tsconfig.json.
interface and type are both used to define object types, but they have some differences:
|
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. |
The above is the detailed content of 【Interview Essentials】ommon TypeScript Interview Questions. For more information, please follow other related articles on the PHP Chinese website!