Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sure, here's the Q&A for TypeScript interview preparation in a concise bullet-point format:
- 1. **Primitive Types in TypeScript**
- - string, number, boolean
- 2. **Arrays in TypeScript**
- - Ordered collections of values
- - Syntax: `let values: number[] = [];`
- 3. **Any Type**
- - Store values of any type
- - Usage: When the type of a variable is unknown in advance
- 4. **Void Type**
- - Indicates absence of type on a variable
- - Usage: Functions that don't return a value
- 5. **Unknown Type**
- - Type-safe counterpart of any
- - Assignable to itself and any without type assertion
- - Usage: When the type is unknown and requires type assertion
- 6. **Variable Declaration Keywords**
- - var, let, const
- 7. **Function Syntax with Type Annotations**
- - Syntax: `function greet(name: string): string { }`
- 8. **Objects in TypeScript**
- - Dictionary-like collections of keys and values
- - Syntax: `{ key: value }`
- 9. **Optional Properties in TypeScript**
- - Add '?' after property name
- - Usage: Properties that may or may not exist in an object
- 10. **Null in TypeScript**
- - Represents absence of value
- - Usage: Indicate when a value is intentionally absent
- 11. **Undefined in TypeScript**
- - Assigned to a variable without value
- - Different from null in its meaning
- 12. **Never Type**
- - Represents values that never occur
- - Usage: Functions that never return or always throw exceptions
- 13. **Enums in TypeScript**
- - Named constants
- - Syntax: `enum EnumName { }`
- 14. **typeof Operator**
- - Returns the type of an operand as a string
- - Usage: Type introspection
- 15. **Rest Parameters and Arguments**
- - Allows indefinite number of arguments as an array
- - Syntax: `function add(...values: number[]) { }`
- 16. **Parameter Destructuring**
- - Unpack object arguments into local variables
- - Syntax: `function multiply({ a, b, c }: { a: number; b: number; c: number }) { }`
- 17. **Class Syntax**
- - Syntax: `class ClassName { }`
- 18. **Arrow Function Syntax**
- - Short and convenient function syntax
- - Syntax: `(params) => expression`
- 19. **Optional Parameters**
- - Mark parameters as optional with '?'
- - Usage: Parameters that may or may not be provided
- 20. **tsconfig.json Purpose**
- - Provides compiler options for TypeScript projects
- 21. **Loop Variants in TypeScript**
- - for loop, forEach function, for..of statement
- 22. **Symbol Type**
- - Primitive type for unique identifiers
- - Usage: Create unique properties for objects
- 23. **Optional Chaining**
- - Allows access to properties in a chain-like fashion
- - Usage: Safely access nested properties without causing errors
- 24. **Function Overloads**
- - Define multiple function signatures with the same name
- - Usage: Define different function behaviors based on parameters
- 25. **Type Inference**
- - TypeScript infers the type of a variable when not explicitly provided
- - Usage: Simplify variable declarations while maintaining type safety
- Certainly! Here's a concise Q&A format for the TypeScript interview preparation content provided:
- **26. What is meant by contextual typing?**
- - TypeScript infers the type of a variable based on its context.
- - Example: `window.onmousedown = function (e) { console.log(e.button); }`.
- **27. What is the purpose of noImplicitAny?**
- - It raises an error when TypeScript infers a variable as `any`.
- - Ensures type safety by preventing unintentional errors.
- - Example: `function parse(s: string) { console.log(s.split(' ')); }`.
- **28. What is an interface?**
- - An interface defines a contract for the structure and behavior of an object.
- - It specifies the type of data and operations an object should have.
- - Example: `interface Employee { name: string; salary: number; }`.
- **29. Explain the various ways to control member visibility in TypeScript.**
- - `public`: Accessible anywhere.
- - `protected`: Accessible in subclasses.
- - `private`: Accessible only within the class.
- - Example: `class MyClass { private x: number; }`.
- **30. Does TypeScript support static classes? If not, why?**
- - No, TypeScript doesn't support static classes.
- - Not needed as TypeScript allows functions and data to exist independently.
- **31. What are abstract classes? When should you use one?**
- - Abstract classes provide a partial implementation and cannot be instantiated.
- - Subclasses must implement abstract members.
- - Example: `abstract class Shape { abstract draw(): void; }`.
- **32. What are anonymous functions? Provide their syntax in TypeScript.**
- - Functions without names.
- - Used as callbacks or immediately invoked.
- - Syntax: `(function() { console.log('Anonymous'); })();`.
- **33. What are union types in TypeScript?**
- - Represents a value that can be of several types.
- - Syntax: `let value: string | number = "Foo";`.
- **34. What are intersection types?**
- - Combines members of two or more types.
- - Syntax: `type Supervisor = Employee & Manager;`.
- **35. What are type aliases? How do you create one?**
- - Gives a new name to an existing type.
- - Syntax: `type Name = string;`.
- **36. Explain the tuple types in TypeScript.**
- - Fixed-size array with known types.
- - Syntax: `let values: [string, number] = ["Foo", 15];`.
- **37. Explain how tuple destructuring works in TypeScript.**
- - Assigns tuple elements to variables.
- - Syntax: `let [name, age] = person;`.
- **38. What are type assertions in TypeScript?**
- - Tells the compiler the type of a variable.
- - Syntax: `let strLength: number = (someValue as string).length;`.
- **39. How to enforce strict null checks in TypeScript?**
- - Use `--strictNullChecks` flag or set `strictNullChecks` to `true` in `tsconfig.json`.
- **40. How to make object properties immutable in TypeScript?**
- - Use `readonly` keyword.
- - Syntax: `interface Point { readonly x: number; }`.
- **41. What is a type declaration file?**
- - Declares the types of variables without providing implementations.
- - Extension: `.d.ts`.
- - Example: `declare module 'library';`.
- **42. What are triple-slash directives?**
- - Compiler instructions in single-line comments.
- - Used to include other files or order output.
- - Syntax: `/// <reference path="..." />`.
- **43. Explain the purpose of the 'in' operator.**
- - Checks if a property exists in an object.
- - Syntax: `'prop' in obj`.
- **44. What are the 'implements' clauses in TypeScript?**
- - Checks if a class satisfies an interface's contract.
- - Syntax: `class MyClass implements MyInterface { }`.
- **45. What are string literal types?**
- - Refers to specific strings as types.
- - Example: `let status: "success" | "error";`.
- **46. What are template literal types?**
- - Combine with concrete types to produce new types.
- - Example: `type Greeting = `Hello, ${name}`;`.
- **47. Explain the concept of inheritance in TypeScript.**
- - Allows a class to inherit properties and methods from another class.
- - Syntax: `class ChildClass extends ParentClass { }`.
- **48. What are conditional types? How do you create them?**
- - Selects one of two types based on a condition.
- - Syntax: `T extends U ? X : Y`.
- **49. What is the Function type in TypeScript?**
- - Global type with properties like `bind`, `call`, etc.
- - Syntax: `function greet(fn: Function) { }`.
- **50. List some utility types provided by TypeScript and explain their usage.**
- - `Partial<Type>`: Makes all properties optional.
- - `Required<Type>`: Makes all properties required.
- - `Readonly<Type>`: Makes all properties read-only.
- - `Record<Keys, Type>`: Creates an object type with specified keys and type.
- - Example: `type Point = { x: number; y: number; };`.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement