Title: Typescript Essentials — LearnCoachAssist

Q: What is TypeScript?
A: TypeScript is a statically typed superset of JavaScript developed by Microsoft that compiles to plain JavaScript code. It adds optional static typing, interfaces, classes, and modules to enhance code scalability, maintainability, and developer productivity.

Q: How does TypeScript relate to JavaScript?
A: TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript. TypeScript code is transpiled to JavaScript, allowing it to run in any JavaScript environment.

Q: How do you install TypeScript?
A: Install TypeScript globally using npm install -g typescript or locally as a dev dependency with npm install typescript --save-dev. Verify installation with tsc --version.

Q: What is the TypeScript compiler command?
A: The TypeScript compiler is invoked with tsc. Use tsc file.ts to compile a single file or tsc to compile based on tsconfig.json.

Q: What is tsconfig.json?
A: tsconfig.json is the TypeScript configuration file that specifies compiler options like target JavaScript version, module system, and strictness mode. It controls how TypeScript projects are compiled.

Q: What are primitive types in TypeScript?
A: Primitive types include number, string, boolean, null, undefined, symbol, and bigint. They are the basic building blocks for typing variables.

Q: How do you declare a typed variable?
A: Use a type annotation after the variable name with a colon: let name: string = "Alice";. TypeScript infers types when possible but explicit annotations improve clarity.

Q: What is type inference in TypeScript?
A: TypeScript automatically infers the type of a variable from its initializer. For example, let count = 5; infers number, reducing boilerplate while maintaining type safety.

Q: What is the 'any' type?
A: The any type disables type checking, allowing a value to be treated as any type. It should be used sparingly as it bypasses TypeScript's benefits; prefer unknown for safer alternatives.

Q: What is the 'unknown' type?
A: unknown is a top type like any but safer, requiring type checks or narrowing before use. It ensures type safety by preventing direct operations on unknown values.

Q: What is the 'never' type?
A: The never type represents values that never occur, such as exhaustive switch statements or impossible code paths. It is useful for function return types that always throw errors.

Q: How do you type an array?
A: Use array syntax like let numbers: number[] = [1, 2, 3]; or generic Array<number>. Arrays can hold homogeneous or heterogeneous types with tuples.

Q: What are tuples in TypeScript?
A: Tuples are fixed-length arrays with specific types per position: let tuple: [string, number] = ["age", 30];. They enforce length and type order unlike regular arrays.

Q: What are enums?
A: Enums define named constants: enum Color { Red, Green, Blue }. They can be numeric (default) or string-based, improving code readability for related values.

Q: What is type assertion?
A: Type assertion tells the compiler to treat a value as a specific type using as: let value = "5" as number; or angle-bracket syntax. Use cautiously to avoid runtime errors.

Q: How do you define an interface?
A: Interfaces describe object shapes: interface Person { name: string; age: number; }. They are used for type checking objects without runtime overhead.

Q: What is the difference between interfaces and types?
A: Interfaces are for object shapes and support declaration merging; type aliases handle unions, intersections, and primitives. Use interfaces for OOP-style contracts, types for complex utilities.

Q: How do you type a function parameter and return?
A: Specify types after parameters and before function body: function add(a: number, b: number): number { return a + b; }. Arrow functions use similar syntax.

Q: What are optional parameters?
A: Optional parameters use ?: function greet(name: string, greeting?: string): string. They can be omitted, defaulting to undefined.

Q: What are default parameters?
A: Default parameters provide fallback values: function greet(name: string, greeting: string = "Hi"): string. They are optional and don't require ?.

Q: How do you type rest parameters?
A: Rest parameters use ... with array type: function sum(...numbers: number[]): number. They collect remaining arguments into an array.

Q: What is an index signature?
A: Index signatures allow dynamic keys: interface StringDict { [key: string]: string; }. Useful for objects with string keys of unknown names.

Q: What are readonly properties?
A: readonly prevents assignment after initialization: interface Point { readonly x: number; y: number; }. Ideal for immutable data.

Q: How do you extend interfaces?
A: Use extends: interface Employee extends Person { role: string; }. It inherits all members from the base interface.

Q: What are union types?
A: Union types allow multiple types: let id: string | number;. Values must be compatible with at least one type in the union.

Q: What are intersection types?
A: Intersection types combine types: type Admin = User & { permissions: string[]; };. The resulting type requires all properties from intersected types.

Q: How do you define a class in TypeScript?
A: Classes use standard syntax with type annotations: class Animal { name: string; constructor(name: string) { this.name = name; } }. Supports access modifiers like public, private, protected.

Q: What are access modifiers?
A: public (default) allows access everywhere, private only within class, protected within class and subclasses. They enforce encapsulation at compile-time.

Q: What is a constructor in TypeScript classes?
A: Constructors initialize instances: constructor(private name: string) { }. Parameters can be auto-promoted to properties with access modifiers.

Q: How does class inheritance work?
A: Use extends and super: class Dog extends Animal { bark() { super.bark(); } }. Subclasses inherit and can override members.

Q: What are abstract classes?
A: Abstract classes can't be instantiated and may have abstract methods: abstract class Shape { abstract area(): number; }. Used as base for concrete subclasses.

Q: What are getters and setters?
A: Getters/setters control property access: get name(): string { return this._name; } set name(value: string) { this._name = value; }. They act like properties but add logic.

Q: What are generics?
A: Generics parameterize types: function identity<T>(arg: T): T { return arg; }. They enable reusable, type-safe components like Array<T>.

Q: How do you constrain generics?
A: Use extends: function longest<T extends { length: number }>(a: T, b: T): T. Ensures type parameters have required properties.

Q: What are generic interfaces?
A: interface Box<T> { contents: T; }. Allows interfaces to work with any type while preserving type information.

Q: What are generic classes?
A: class Stack<T> { push(item: T): void { } pop(): T | undefined { } }. Classes can be generic for type-safe data structures.

Q: How do you import modules in TypeScript?
A: Use import { ModuleName } from './module'; for named exports or import * as Module from './module'; for namespace imports. Supports ES6 and CommonJS.

Q: How do you export from a module?
A: Use export function func() { } for named or export default class Class { } for default. Re-export with export { something } from './other';.

Q: What are namespaces?
A: Namespaces group related code: namespace Utils { export function helper() { } }. Used for avoiding global pollution pre-modules, now less common.

Q: What are type guards?
A: Type guards narrow types in conditionals: function isString(value: any): value is string { return typeof value === 'string'; }. Enables safe access to narrowed properties.

Q: What are discriminated unions?
A: Discriminated unions use a common literal discriminant: type Shape = { kind: 'circle'; r: number } | { kind: 'square'; side: number };. Switch on discriminant for exhaustive checks.

Q: What are conditional types?
A: Conditional types use ternary: type NonNullable<T> = T extends null | undefined ? never : T;. Infer types based on conditions for advanced utilities.

Q: What are mapped types?
A: Mapped types transform keys: type Readonly<T> = { readonly [P in keyof T]: T[P]; };. Utility types like Partial, Pick are built on this.

Q: What are utility types?
A: Built-in utilities include Partial<T> (all optional), Required<T> (all required), Pick<T, K> (select keys), Omit<T, K> (exclude keys). They simplify common transformations.

Q: What is 'keyof' operator?
A: The keyof operator yields union of property keys: keyof Person // "name" | "age". Used in mapped and conditional types.

Q: What does 'in' do in mapped types?
A: The in keyword iterates keys in mapped types: { [P in keyof T]: T[P] }. Essential for transforming all or subset of properties.

Q: What are template literal types?
A: Template literals create string literal unions: type Event = `on${'click' | 'hover'}`; // "onclick" | "onhover". Useful for string manipulation at type level.

Q: What is strict mode in tsconfig?
A: The "strict": true option enables all strict type-checking options like strictNullChecks. Recommended for new projects to catch more errors.

Q: What is 'noImplicitAny'?
A: "noImplicitAny": true errors on inferred any types, forcing explicit annotations. Promotes type safety by avoiding implicit type escapes.

Q: What are declaration files (.d.ts)?
A: .d.ts files provide type definitions for JavaScript libraries: declare function jQuery(): void;. Installed via @types packages like npm install @types/node.

Q: How do you target different JS versions?
A: Set "target": "ES2020" in tsconfig.json to specify output JavaScript version. Common options: ES5, ES2015, ESNext.