Explore the fundamentals of TypeScript with our collection of essential beginner-level questions.
Deepen your TypeScript knowledge with our advanced questions that cover state management and cross-platform compatibility.
Master TypeScript with our expert-level questions that delve into performance optimization and the inner workings of the TypeScript bridge.
Answer: - **TypeScript** is a strongly typed superset of JavaScript developed by Microsoft. - **Benefits**: - Adds static types to JavaScript, helping catch errors during development. - Offers better tooling with autocompletion, navigation, and refactoring. - Supports modern JavaScript features that may not yet be supported in all browsers.
Answer: - **Compilation**: - Use the TypeScript Compiler (tsc) to compile TypeScript files. - Run `tsc filename.ts` to generate a JavaScript file. - **Configuration**: - A `tsconfig.json` file can be used to configure project-wide settings.
Answer: - **TypeScript**: - Supports static typing. - Includes interfaces and generics. - Requires compilation to JavaScript. - **JavaScript**: - Dynamically typed. - No interfaces or generics by default. - Interpreted directly by browsers.
Answer: - **Variable Declaration**: - Use `let`, `const`, or `var` keywords, similar to JavaScript. - Specify a type explicitly if needed: `let count: number = 10;` - **Type Inference**: - TypeScript can infer types based on initial values: `let name = 'TypeScript';`
Answer: - **Interfaces**: - Define a contract for objects, specifying property names and types. - Can be used to ensure objects adhere to a particular shape. - **Example**: ```typescript interface User { name: string; age: number; } const user: User = { name: 'Alice', age: 30 }; ```
Answer: - **Classes**: - Provide a blueprint for creating objects. - Support features like inheritance, access modifiers, and decorators. - **Example**: ```typescript class Animal { name: string; constructor(name: string) { this.name = name; } makeSound(): void { console.log('Sound...'); } } ```
Answer: - **Modules**: - Allow organizing code into separate files and namespaces. - Support export and import keywords to share code between files. - **Example**: ```typescript // file1.ts export function greet() { console.log('Hello'); } // file2.ts import { greet } from './file1'; greet(); ```
Answer: - **Generics**: - Provide a way to create reusable components that can work with any data type. - Allow developers to define a placeholder for types that will be specified later. - **Example**: ```typescript function identity<T>(arg: T): T { return arg; } const num = identity<number>(42); const str = identity<string>('TypeScript'); ```
Answer: - **Type Aliases**: - Create a new name for a type, simplifying complex type definitions. - Can be used with any type, including primitives, arrays, and objects. - **Example**: ```typescript type Point = { x: number; y: number; }; const origin: Point = { x: 0, y: 0 }; ```
Answer: - **Handling**: - TypeScript has strict null checks that can be enabled in the `tsconfig.json`. - Types can include `null` and `undefined` as possible values. - **Example**: ```typescript let maybe: string | null = null; maybe = 'hello'; ```
Answer: - **keyof Operator**: - A type query that produces a union of literal types representing the keys of a given object type. - **Example**: ```typescript type Person = { name: string; age: number }; type PersonKeys = keyof Person; // 'name' | 'age' ```
Answer: - **Interface**: - Primarily used to define the shape of an object. - Supports extension via the `extends` keyword. - **Type Alias**: - Can define any type, including primitives, unions, and tuples. - Cannot be extended but can intersect other types. - **Example**: ```typescript interface Dog { breed: string; } type Cat = { color: string }; ```
Answer: - **Type Inference**: - TypeScript automatically infers the type of a variable based on its value. - Reduces the need for explicit type annotations. - **Example**: ```typescript let count = 10; // inferred as number let message = 'Hello'; // inferred as string ```
Answer: - **Enums**: - Enums allow defining a set of named constants. - Can be numeric or string-based. - **Example**: ```typescript enum Direction { North, South, East, West } let heading: Direction = Direction.North; ```
Answer: - **Unknown Type**: - A safer version of the `any` type. - Requires explicit type checking before performing operations on variables of this type. - **Example**: ```typescript let value: unknown = 'Hello'; if (typeof value === 'string') { console.log(value.toUpperCase()); } ```
Answer: - **Never Type**: - Represents the type of values that never occur. - Typically used for functions that throw exceptions or never return. - **Example**: ```typescript function error(message: string): never { throw new Error(message); } ```
Answer: - **Decorators**: - Special types of declarations that can be attached to classes and methods to modify their behavior. - Experimental feature, enabled via `experimentalDecorators` in `tsconfig.json`. - **Example**: ```typescript function log(target: any, key: string) { console.log(key + ' was called'); } class Person { @log sayHello() { console.log('Hello'); } } ```
Answer: - **Utility Types**: - Built-in types that provide common type transformations. - Examples include `Partial<T>`, `ReadOnly<T>`, and `Pick<T, K>`. - **Example**: ```typescript interface Task { title: string; description: string; } const task: Partial<Task> = { title: 'Study TypeScript' }; ```
Answer: - **Union Types**: - Allow a variable to hold more than one type. - Use the pipe (`|`) symbol to define union types. - **Example**: ```typescript let value: string | number; value = 'Hello'; value = 42; ```
Answer: - **Abstract Class**: - Can have both concrete and abstract methods. - Supports access modifiers and can have instance fields. - **Interface**: - Only describes the shape of an object. - Cannot have implementation details or instance fields. - **Example**: ```typescript abstract class Animal { abstract makeSound(): void; } interface Bird { fly(): void; } ```