TypeScript Assignment- 5

Basic Questions

  1. Write a TypeScript example that uses as for type assertion to convert a variable of type unknown into string.
  2. Write a function combineToString that accepts number | boolean and uses type assertion to safely return the value as a string.
  3. Define an object with an index signature where keys are strings and values are numbers.
  4. Write a function that accepts an object with unknown keys (string keys, boolean values).
  5. Use mapped types to make all properties of an interface Person optional.
  6. Create a type ReadonlyCar from Car interface using mapped types.
  7. Use keyof to extract all keys of an interface Book.
  8. Define a type PropertyType<T, K extends keyof T> that extracts the type of a property.
  9. Write a conditional type that checks if a type is string → returns “Text”, else “Other”.
  10. Use infer in a conditional type to extract the return type of a function.
  11. Show an example of declaration merging with two interfaces of the same name User.
  12. Write a code snippet where a namespace groups utility functions.
  13. Import a function from another file using ES6 import syntax.
  14. Export a class using default export.
  15. Create a function logProperties<T>(obj: T) that logs all keys of the object using keyofand generics.
  16. Define a type alias using indexed access type T[K].
  17. Create an example of using union type with index signatures.
  18. Write a function that demonstrates a namespace merged with an interface.
  19. Define a namespace MathHelpers with a function square, and in the same file create a normal class Calculator that also uses square.
  20. Create a type that makes all properties of an interface Employee nullable using mapped types.

Intermediate Questions

  1. Create a function toNumber that accepts a value of type string | number and uses type assertion to convert it into number.
  2. Define an object with index signature where keys are numbers and values are strings.
  3. Create a generic function that uses keyof to access dynamic object properties safely.
  4. Write a mapped type Mutable<T> that removes readonly from all properties of a type.
  5. Define a mapped type Nullable<T> that makes all properties nullable.
  6. Create a conditional type IsArray<T> that checks if a type is an array.
  7. Use infer to extract the parameter types of a function.
  8. Write a type ReturnType<T> without using the built-in ReturnType.
  9. Demonstrate declaration merging with multiple interfaces Animal having different properties.
  10. Create an interface Shape with method area(). Then define a class Circle and Square implementing Shape. Demonstrate polymorphism by storing both in an array and calling area().
  11. Use modules to separate a MathUtils file (exported functions) and main.ts file (import).
  12. Show how namespace can be used to wrap a set of constants and enums.
  13. Create a module resolution scenario in tsconfig.json with “baseUrl” and “paths”.
  14. Write an example where namespace merges with a class.
  15. Use a conditional type to extract the element type from an array using infer.
  16. Create a type-safe function getProperty<T, K extends keyof T>(obj: T, key: K): T[K].
  17. Write a mapped type DeepReadonly<T> that makes all properties deeply readonly.
  18. Show a code snippet where a type uses both keyof and mapped types.
  19. Create an example where conditional type decides between two different interfaces.
  20. Use namespaces and modules together in a single project example.

Advanced Questions

  1. Write a type Flatten<T> using conditional types and infer that flattens nested arrays.
  2. Implement a generic utility type DeepPartial<T> that recursively makes all properties optional.
  3. Write a mapped type PickByValue<T, V> that picks only the properties of type V from T.
  4. Implement a conditional type IsNever<T> that checks if a type is never.
  5. Create a utility type PromiseType<T> that extracts the resolved type of a promise using infer.
  6. Write a generic classRepository<T> with methods add(item: T), getAll(), and find(predicate: (item: T) => boolean): T[].
  7. Build a type-safe EventEmitter interface using index signatures and mapped types.
  8. Write a namespace Database that contains interfaces, classes, and constants together.
  9. Implement a generic utility type DeepMutable<T> that recursively removes readonly from all properties, and test it with a nested object type.
  10. Implement a generic class ApiClient<T> that uses conditional types and mapped types to infer request/response shapes.