TypeScript Answers
0:000:00
TypeScript Answers
Basic TypeScript Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is TypeScript? | A superset of JavaScript that adds static typing | It compiles down to plain JavaScript |
2 | Why use TypeScript? | Improves code maintainability, catches errors early, provides better tooling | Type checking during development, better code readability, autocompletion |
3 | What is static typing? | Checking variable types at compile time (before the code runs) | let name: string = "Alice";, let age: number = 30; |
4 | How do you declare a variable with a type? | Using a colon (:) followed by the type name | let isActive: boolean = true; |
5 | What are basic primitive types? | string, number, boolean, null, undefined, symbol, bigint | let message: string = "Hello";, let count: number = 100; |
6 | How do you define an array type? | Using Type[] or Array | let names: string[] = ["Bob", "Charlie"];, let numbers: Array |
7 | How do you define an object type? | Using an interface or type alias | interface User { name: string; age: number; } let user: User = { name: "Dave", age: 40 }; |
8 | What is an interface? | Defines the shape of an object | interface Point { x: number; y: number; } |
9 | How do you define a function type? | Specifying types for parameters and the return value | function add(a: number, b: number): number { return a + b; } |
10 | What is the any type? | Allows a variable to hold a value of any type (use sparingly) | let data: any = "anything"; data = 123; |
11 | What is the void type? | Indicates a function does not explicitly return a value | function logMessage(): void { console.log("Logged"); } |
12 | What is the unknown type? | Similar to any, but requires type checking before use | let value: unknown = "test"; if (typeof value === 'string') { console.log(value.toUpperCase()); } |
13 | What is a union type ( | )? | Allows a variable to hold a value of several different types | let id: string | number = "abc"; id = 123; |
14 | What is an intersection type (&)? | Combines multiple types into a single type | type Person = { name: string }; type Employee = { id: number }; type FullTime = Person & Employee; |
15 | How do you compile TypeScript code? | Using the tsc command (TypeScript Compiler) | tsc myFile.ts |
Intermediate TypeScript Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | Explain the difference between interface and type. | interface is primarily for object shapes; type can define object shapes, unions, primitives, etc. | interface User { name: string; }, type Point = { x: number; }, `type ID = string |
2 | What are Generics? | Allow you to write components that can work with a variety of types without losing type safety | function identity |
3 | What is an Enum? | Defines a set of named constants | enum Color { Red, Green, Blue } let myColor: Color = Color.Green; |
4 | What is a Tuple? | An array with a fixed number of elements and known types for each element | let coordinate: [number, number] = [10, 20]; |
5 | What is a Class in TypeScript? | A blueprint for creating objects with properties and methods | class Dog { name: string; constructor(name: string) { this.name = name; } bark() { console.log("Woof!"); } } |
6 | What are access modifiers (public, private, protected)? | Control the visibility of class members | class Animal { public name: string; private secret: string; protected species: string = "unknown"; } |
7 | What is inheritance in TypeScript? | Allows a class to inherit properties and methods from another class (extends) | class Cat extends Animal { meow() { console.log("Meow!"); } } |
8 | What is an abstract class? | A class that cannot be instantiated directly, serves as a base for other classes | abstract class Shape { abstract draw(): void; } class Circle extends Shape { draw() { console.log("Drawing Circle"); } } |
9 | What is a Type Assertion? | Tells TypeScript to treat a value as a specific type (use with caution) | let myInput = document.getElementById("myInput") as HTMLInputElement; |
10 | What is a Type Guard? | Used to narrow down the type of a variable at runtime | function processValue(value: string | number) { if (typeof value === 'string') { console.log(value.toUpperCase()); } } |
11 | How do you make a property optional in an object interface/type? | Using a question mark (?) after the property name | interface Config { url: string; timeout?: number; } |
12 | What is the readonly modifier? | Prevents a property from being reassigned after initialization | class Point { readonly x: number; constructor(x: number) { this.x = x; } } |
13 | What is a Type Alias? | Creates a new name for an existing type | type Coordinates = { x: number; y: number; }; let point: Coordinates = { x: 10, y: 20 }; |
14 | What is a literal type? | Defines a type based on a specific literal value (string, number, boolean) | type Status = "pending" | "processing" | "completed"; let status: Status = "pending"; |
15 | What are utility types? | Built-in types for common type transformations (e.g., Partial, Required, Pick, Omit) | let partialUser: Partial |
Advanced TypeScript Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | Explain Decorators. | A way to add metadata or behavior to classes, methods, properties, or parameters | @Component({}) class MyClass { ... }, @logMethod() function myMethod() { ... } |
2 | What are Conditional Types? | Allows you to create a type based on a condition (similar to a ternary operator) | type IsString |
3 | What are Mapped Types? | Creates a new type by iterating over the properties of an existing type | type Readonly |
4 | What is the keyof operator? | Gets a union of the property names of an object type | type UserKeys = keyof User; (UserKeys is "name" | "age") |
5 | What is the typeof operator? | Gets the type of a variable or expression | let name = "Grace"; type NameType = typeof name; (NameType is string) |
6 | What is the infer keyword? | Used within conditional types to infer a type from a part of a type | type GetReturnType |
7 | Explain TypeScript Module Resolution. | How TypeScript finds module files when you imports | Involves baseUrl, paths, moduleResolution compiler options in tsconfig.json |
8 | What is tsconfig.json? | Configuration file for the TypeScript compiler (tsc) | Specifies compiler options like target, module, outDir, rootDir, strict |
9 | What is the strict flag in tsconfig.json? | Enables a set of stricter type-checking options | strict: true enables noNullChecks, noImplicitAny, strictFunctionTypes, etc. |
10 | What are Type Declaration Files (.d.ts)? | Files that provide type definitions for JavaScript libraries that don't have TypeScript types | declare module 'lodash';, declare function jQuery(selector: string): any; |
11 | How do you install types for a JavaScript library? | Using @types/library-name via npm or yarn | npm install --save-dev @types/react, then import * as React from 'react'; |
12 | What is type inference? | The process where TypeScript automatically determines the type of a variable based on its initial value | let message = "Hello"; (TypeScript infers message is string) |
13 | What is noNullChecks? | Requires explicit checks for null and undefined values | let name: string | null = null; if (name !== null) { ... } (prevents runtime errors) |
14 | What is noImplicitAny? | Prevents TypeScript from implicitly using the any type when a type cannot be inferred | let value; (causes an error if noImplicitAny is true) |
15 | Explain the concept of namespaces in TypeScript. | A way to organize code and provide global scope for sharing code across files | namespace Utils { export class Helper { ... } }, import helper = Utils.Helper; |