Advertisement
satishfrontenddev5

Untitled

Feb 25th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sure, here's the Q&A for TypeScript interview preparation in a concise bullet-point format:
  2.  
  3. 1. **Primitive Types in TypeScript**
  4.   - string, number, boolean
  5.  
  6. 2. **Arrays in TypeScript**
  7.   - Ordered collections of values
  8.   - Syntax: `let values: number[] = [];`
  9.  
  10. 3. **Any Type**
  11.   - Store values of any type
  12.   - Usage: When the type of a variable is unknown in advance
  13.  
  14. 4. **Void Type**
  15.   - Indicates absence of type on a variable
  16.   - Usage: Functions that don't return a value
  17.  
  18. 5. **Unknown Type**
  19.    - Type-safe counterpart of any
  20.    - Assignable to itself and any without type assertion
  21.    - Usage: When the type is unknown and requires type assertion
  22.  
  23. 6. **Variable Declaration Keywords**
  24.    - var, let, const
  25.  
  26. 7. **Function Syntax with Type Annotations**
  27.    - Syntax: `function greet(name: string): string { }`
  28.  
  29. 8. **Objects in TypeScript**
  30.    - Dictionary-like collections of keys and values
  31.    - Syntax: `{ key: value }`
  32.  
  33. 9. **Optional Properties in TypeScript**
  34.    - Add '?' after property name
  35.    - Usage: Properties that may or may not exist in an object
  36.  
  37. 10. **Null in TypeScript**
  38.     - Represents absence of value
  39.     - Usage: Indicate when a value is intentionally absent
  40.  
  41. 11. **Undefined in TypeScript**
  42.     - Assigned to a variable without value
  43.     - Different from null in its meaning
  44.  
  45. 12. **Never Type**
  46.     - Represents values that never occur
  47.     - Usage: Functions that never return or always throw exceptions
  48.  
  49. 13. **Enums in TypeScript**
  50.     - Named constants
  51.     - Syntax: `enum EnumName { }`
  52.  
  53. 14. **typeof Operator**
  54.     - Returns the type of an operand as a string
  55.     - Usage: Type introspection
  56.  
  57. 15. **Rest Parameters and Arguments**
  58.     - Allows indefinite number of arguments as an array
  59.     - Syntax: `function add(...values: number[]) { }`
  60.  
  61. 16. **Parameter Destructuring**
  62.     - Unpack object arguments into local variables
  63.     - Syntax: `function multiply({ a, b, c }: { a: number; b: number; c: number }) { }`
  64.  
  65. 17. **Class Syntax**
  66.     - Syntax: `class ClassName { }`
  67.  
  68. 18. **Arrow Function Syntax**
  69.     - Short and convenient function syntax
  70.     - Syntax: `(params) => expression`
  71.  
  72. 19. **Optional Parameters**
  73.     - Mark parameters as optional with '?'
  74.     - Usage: Parameters that may or may not be provided
  75.  
  76. 20. **tsconfig.json Purpose**
  77.     - Provides compiler options for TypeScript projects
  78.  
  79. 21. **Loop Variants in TypeScript**
  80.     - for loop, forEach function, for..of statement
  81.  
  82. 22. **Symbol Type**
  83.     - Primitive type for unique identifiers
  84.     - Usage: Create unique properties for objects
  85.  
  86. 23. **Optional Chaining**
  87.     - Allows access to properties in a chain-like fashion
  88.     - Usage: Safely access nested properties without causing errors
  89.  
  90. 24. **Function Overloads**
  91.     - Define multiple function signatures with the same name
  92.     - Usage: Define different function behaviors based on parameters
  93.  
  94. 25. **Type Inference**
  95.     - TypeScript infers the type of a variable when not explicitly provided
  96.     - Usage: Simplify variable declarations while maintaining type safety
  97.  
  98.  
  99. Certainly! Here's a concise Q&A format for the TypeScript interview preparation content provided:
  100.  
  101. **26. What is meant by contextual typing?**
  102. - TypeScript infers the type of a variable based on its context.
  103. - Example: `window.onmousedown = function (e) { console.log(e.button); }`.
  104.  
  105. **27. What is the purpose of noImplicitAny?**
  106. - It raises an error when TypeScript infers a variable as `any`.
  107. - Ensures type safety by preventing unintentional errors.
  108. - Example: `function parse(s: string) { console.log(s.split(' ')); }`.
  109.  
  110. **28. What is an interface?**
  111. - An interface defines a contract for the structure and behavior of an object.
  112. - It specifies the type of data and operations an object should have.
  113. - Example: `interface Employee { name: string; salary: number; }`.
  114.  
  115. **29. Explain the various ways to control member visibility in TypeScript.**
  116. - `public`: Accessible anywhere.
  117. - `protected`: Accessible in subclasses.
  118. - `private`: Accessible only within the class.
  119. - Example: `class MyClass { private x: number; }`.
  120.  
  121. **30. Does TypeScript support static classes? If not, why?**
  122. - No, TypeScript doesn't support static classes.
  123. - Not needed as TypeScript allows functions and data to exist independently.
  124.  
  125. **31. What are abstract classes? When should you use one?**
  126. - Abstract classes provide a partial implementation and cannot be instantiated.
  127. - Subclasses must implement abstract members.
  128. - Example: `abstract class Shape { abstract draw(): void; }`.
  129.  
  130. **32. What are anonymous functions? Provide their syntax in TypeScript.**
  131. - Functions without names.
  132. - Used as callbacks or immediately invoked.
  133. - Syntax: `(function() { console.log('Anonymous'); })();`.
  134.  
  135. **33. What are union types in TypeScript?**
  136. - Represents a value that can be of several types.
  137. - Syntax: `let value: string | number = "Foo";`.
  138.  
  139. **34. What are intersection types?**
  140. - Combines members of two or more types.
  141. - Syntax: `type Supervisor = Employee & Manager;`.
  142.  
  143. **35. What are type aliases? How do you create one?**
  144. - Gives a new name to an existing type.
  145. - Syntax: `type Name = string;`.
  146.  
  147. **36. Explain the tuple types in TypeScript.**
  148. - Fixed-size array with known types.
  149. - Syntax: `let values: [string, number] = ["Foo", 15];`.
  150.  
  151. **37. Explain how tuple destructuring works in TypeScript.**
  152. - Assigns tuple elements to variables.
  153. - Syntax: `let [name, age] = person;`.
  154.  
  155. **38. What are type assertions in TypeScript?**
  156. - Tells the compiler the type of a variable.
  157. - Syntax: `let strLength: number = (someValue as string).length;`.
  158.  
  159. **39. How to enforce strict null checks in TypeScript?**
  160. - Use `--strictNullChecks` flag or set `strictNullChecks` to `true` in `tsconfig.json`.
  161.  
  162. **40. How to make object properties immutable in TypeScript?**
  163. - Use `readonly` keyword.
  164. - Syntax: `interface Point { readonly x: number; }`.
  165.  
  166. **41. What is a type declaration file?**
  167. - Declares the types of variables without providing implementations.
  168. - Extension: `.d.ts`.
  169. - Example: `declare module 'library';`.
  170.  
  171. **42. What are triple-slash directives?**
  172. - Compiler instructions in single-line comments.
  173. - Used to include other files or order output.
  174. - Syntax: `/// <reference path="..." />`.
  175.  
  176. **43. Explain the purpose of the 'in' operator.**
  177. - Checks if a property exists in an object.
  178. - Syntax: `'prop' in obj`.
  179.  
  180. **44. What are the 'implements' clauses in TypeScript?**
  181. - Checks if a class satisfies an interface's contract.
  182. - Syntax: `class MyClass implements MyInterface { }`.
  183.  
  184. **45. What are string literal types?**
  185. - Refers to specific strings as types.
  186. - Example: `let status: "success" | "error";`.
  187.  
  188. **46. What are template literal types?**
  189. - Combine with concrete types to produce new types.
  190. - Example: `type Greeting = `Hello, ${name}`;`.
  191.  
  192. **47. Explain the concept of inheritance in TypeScript.**
  193. - Allows a class to inherit properties and methods from another class.
  194. - Syntax: `class ChildClass extends ParentClass { }`.
  195.  
  196. **48. What are conditional types? How do you create them?**
  197. - Selects one of two types based on a condition.
  198. - Syntax: `T extends U ? X : Y`.
  199.  
  200. **49. What is the Function type in TypeScript?**
  201. - Global type with properties like `bind`, `call`, etc.
  202. - Syntax: `function greet(fn: Function) { }`.
  203.  
  204. **50. List some utility types provided by TypeScript and explain their usage.**
  205. - `Partial<Type>`: Makes all properties optional.
  206. - `Required<Type>`: Makes all properties required.
  207. - `Readonly<Type>`: Makes all properties read-only.
  208. - `Record<Keys, Type>`: Creates an object type with specified keys and type.
  209. - Example: `type Point = { x: number; y: number; };`.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement