{"id":21964676,"url":"https://github.com/apollo-level2-web-dev/conquer-the-typescript-conceptual-session","last_synced_at":"2025-10-12T23:19:13.608Z","repository":{"id":162691262,"uuid":"637181680","full_name":"Apollo-Level2-Web-Dev/conquer-the-typescript-conceptual-session","owner":"Apollo-Level2-Web-Dev","description":null,"archived":false,"fork":false,"pushed_at":"2023-05-07T06:29:51.000Z","size":23,"stargazers_count":7,"open_issues_count":0,"forks_count":6,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-03T20:51:11.632Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Apollo-Level2-Web-Dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-06T18:59:33.000Z","updated_at":"2024-06-07T17:35:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"99e7400a-108a-4128-875a-8e4e39569e34","html_url":"https://github.com/Apollo-Level2-Web-Dev/conquer-the-typescript-conceptual-session","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Apollo-Level2-Web-Dev/conquer-the-typescript-conceptual-session","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fconquer-the-typescript-conceptual-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fconquer-the-typescript-conceptual-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fconquer-the-typescript-conceptual-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fconquer-the-typescript-conceptual-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Apollo-Level2-Web-Dev","download_url":"https://codeload.github.com/Apollo-Level2-Web-Dev/conquer-the-typescript-conceptual-session/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Apollo-Level2-Web-Dev%2Fconquer-the-typescript-conceptual-session/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279003715,"owners_count":26083610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T12:26:43.853Z","updated_at":"2025-10-10T11:30:35.612Z","avatar_url":"https://github.com/Apollo-Level2-Web-Dev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Conquer the TypeScript (Conceptual Session)\n\n## Problem \u0026 Solution\n1. Create a function that takes an array of numbers as input and returns the sum of all the numbers in the array. Make sure to define the types for the input parameter and return value.\n```bash\nfunction sumArray(numbers: number[]): number {\n  let sum = 0;\n  for (let num of numbers) {\n    sum += num;\n  }\n  return sum;\n}\nconst numbers: number[] = [1, 2, 3, 4, 5];\nconst result: number = sumArray(numbers);\nconsole.log(result);  // Output: 15\n```\n\n2. Define an interface called Person that has the following properties: name (string), age (number), and email (string). Create an object based on this interface and print out its properties.\n```bash\ninterface Person {\n  name: string;\n  age: number;\n  email: string;\n}\n\nconst person: Person = {\n  name: \"John Doe\",\n  age: 25,\n  email: \"john.doe@example.com\",\n};\n\nconsole.log(person.name);   // Output: John Doe\nconsole.log(person.age);    // Output: 25\nconsole.log(person.email);  // Output: john.doe@example.com\n```\n\n3. Write a function called calculateArea that takes the length and width of a rectangle as parameters and returns its area. Define the types for the input parameters and return value.\n```bash\nconst length: number = 5;\nconst width: number = 10;\nconst area: number = calculateArea(length, width);\nconsole.log(area);  // Output: 50\n```\n\n4. Implement a generic function called reverseArray that takes an array of any type and returns the reversed version of the array. Test it with different types of arrays (e.g., numbers, strings) to ensure it works correctly.\n\n```bash\nfunction reverseArray\u003cT\u003e(array: T[]): T[] {\n  return array.reverse();\n}\nconst numbers: number[] = [1, 2, 3, 4, 5];\nconsole.log(reverseArray(numbers));  // Output: [5, 4, 3, 2, 1]\n\nconst strings: string[] = [\"apple\", \"banana\", \"cherry\"];\nconsole.log(reverseArray(strings));  // Output: [\"cherry\", \"banana\", \"apple\"]\n\n```\n5. Create a generic interface called Repository that represents a generic data repository. It should have methods like getAll, getById, create, update, and delete. Define the types for the methods and create a class that implements this interface.\n```bash\ninterface Repository\u003cT\u003e {\n  getAll(): T[];\n  getById(id: number): T | undefined;\n  create(item: T): void;\n  update(item: T): void;\n  delete(id: number): void;\n}\n\nclass GenericRepository\u003cT\u003e implements Repository\u003cT\u003e {\n  private items: T[];\n\n  constructor() {\n    this.items = [];\n  }\n\n  getAll(): T[] {\n    return this.items;\n  }\n\n  getById(id: number): T | undefined {\n    return this.items.find((item: any) =\u003e item.id === id);\n  }\n\n  create(item: T): void {\n    this.items.push(item);\n  }\n\n  update(item: T): void {\n    const index = this.items.findIndex((existingItem: any) =\u003e existingItem.id === item.id);\n    if (index !== -1) {\n      this.items[index] = item;\n    }\n  }\n\n  delete(id: number): void {\n    const index = this.items.findIndex((item: any) =\u003e item.id === id);\n    if (index !== -1) {\n      this.items.splice(index, 1);\n    }\n  }\n}\n\n// Example usage\ninterface User {\n  id: number;\n  name: string;\n  age: number;\n}\n\nconst userRepository = new GenericRepository\u003cUser\u003e();\n\nuserRepository.create({ id: 1, name: \"John Doe\", age: 25 });\nuserRepository.create({ id: 2, name: \"Jane Smith\", age: 30 });\n\nconst allUsers = userRepository.getAll();\nconsole.log(allUsers);\n\nconst user = userRepository.getById(1);\nconsole.log(user);\nuserRepository.update({ id: 1, name: \"John Doe\", age: 26 });\nuserRepository.delete(2);\n\n```\n\n6. Define a type alias called Coordinates that represents the latitude and longitude of a location. It should be an object with latitude and longitude properties, both of which are numbers. Create a variable of type Coordinates and assign some sample values to it.\n```bash\ntype Coordinates = {\n  latitude: number;\n  longitude: number;\n};\n\nconst location: Coordinates = {\n  latitude: 40.7128,\n  longitude: -74.0060,\n};\n\nconsole.log(location);  // Output: { latitude: 40.7128, longitude: -74.0060 }\n\n```\n\n7. Define an enum called Color with values representing different colors (e.g., Red, Green, Blue). Create a function that takes a Color value as input and prints out a corresponding message (e.g., \"You selected Red\").\n```bash\nenum Color {\n  Red = \"Red\",\n  Green = \"Green\",\n  Blue = \"Blue\",\n}\n\nfunction printColorMessage(color: Color): void {\n  switch (color) {\n    case Color.Red:\n      console.log(\"You selected Red\");\n      break;\n    case Color.Green:\n      console.log(\"You selected Green\");\n      break;\n    case Color.Blue:\n      console.log(\"You selected Blue\");\n      break;\n    default:\n      console.log(\"Unknown color\");\n  }\n}\nconst selectedColor: Color = Color.Red;\nprintColorMessage(selectedColor);  // Output: \"You selected Red\"\n\n```\n\n8. Create a base class called Shape with a method calculateArea that returns the area of the shape. Extend this class to create subclasses for specific shapes like Rectangle and Circle. Implement the calculateArea method in each subclass and test it with different dimensions.\n```bash\nabstract class Shape {\n  abstract calculateArea(): number;\n}\n\nclass Rectangle extends Shape {\n  constructor(private width: number, private height: number) {\n    super();\n  }\n\n  calculateArea(): number {\n    return this.width * this.height;\n  }\n}\n\nclass Circle extends Shape {\n  constructor(private radius: number) {\n    super();\n  }\n\n  calculateArea(): number {\n    return Math.PI * this.radius ** 2;\n  }\n}\n\n// Test the classes\nconst rectangle = new Rectangle(5, 10);\nconsole.log(rectangle.calculateArea());  // Output: 50\n\nconst circle = new Circle(3);\nconsole.log(circle.calculateArea());  // Output: 28.274333882308138\n\n```\n\n9. Define two interfaces: Car with properties like make, model, and year, and Driver with properties like name and licenseNumber. Create a function that takes objects of type Car and Driver and returns an object with the combined properties of both types.\n```bash\ninterface Car {\n  make: string;\n  model: string;\n  year: number;\n}\n\ninterface Driver {\n  name: string;\n  licenseNumber: string;\n}\n\nfunction combineCarAndDriver(car: Car, driver: Driver): { car: Car, driver: Driver } {\n  return {\n    car: car,\n    driver: driver\n  };\n}\n\n// Example usage\nconst myCar: Car = {\n  make: \"Toyota\",\n  model: \"Camry\",\n  year: 2022\n};\n\nconst myDriver: Driver = {\n  name: \"John Doe\",\n  licenseNumber: \"ABC123\"\n};\n\nconst combinedData = combineCarAndDriver(myCar, myDriver);\nconsole.log(combinedData);\n\n```\n\n10. Create a function that takes a parameter which can be either a string or an array of strings. If it's a string, return the uppercase version of the string. If it's an array of strings, return an array with each string in uppercase.\n```bash\nfunction convertToUppercase(input: string | string[]): string | string[] {\n  if (typeof input === 'string') {\n    return input.toUpperCase();\n  } else if (Array.isArray(input)) {\n    return input.map((str) =\u003e str.toUpperCase());\n  } else {\n    throw new Error('Invalid input');\n  }\n}\nconst strResult = convertToUppercase('hello');\nconsole.log(strResult);  // Output: \"HELLO\"\n\nconst arrResult = convertToUppercase(['apple', 'banana', 'cherry']);\nconsole.log(arrResult);  // Output: [\"APPLE\", \"BANANA\", \"CHERRY\"]\n\n```\n\n11. Declare a variable with an initial value of null and type it as string. Use type assertion to assign a string value to this variable and then print its length.\n```bash\nlet myString: string | null = null;\nmyString = \"Hello, TypeScript!\" as string;\nconsole.log(myString.length);\n```\n\n12. Create a function that takes an input parameter of type unknown. Inside the function, implement type guards to check if the input is of type string or number and perform different operations based on the type.\n```bash\nfunction processInput(input: unknown): void {\n  if (typeof input === 'string') {\n    console.log(`Input is a string: ${input.toUpperCase()}`);\n  } else if (typeof input === 'number') {\n    console.log(`Input is a number: ${input * 2}`);\n  } else {\n    console.log('Unknown input type');\n  }\n}\nprocessInput(\"hello\");  // Output: \"Input is a string: HELLO\"\nprocessInput(5);  // Output: \"Input is a number: 10\"\nprocessInput(true);  // Output: \"Unknown input type\"\n\n```\n13. Create a generic function that takes an array of elements and returns the first element of the array. Add a constraint to ensure that the generic type can be compared using the \u003e operator.\n```bash\nfunction getFirstElement\u003cT extends number | string\u003e(arr: T[]): T | undefined {\n  if (arr.length \u003e 0) {\n    return arr[0];\n  } else {\n    return undefined;\n  }\n}\nconst numbers: number[] = [1, 2, 3, 4, 5];\nconst firstNumber = getFirstElement(numbers);\nconsole.log(firstNumber);  // Output: 1\n\nconst strings: string[] = [\"apple\", \"banana\", \"cherry\"];\nconst firstString = getFirstElement(strings);\nconsole.log(firstString);  // Output: \"apple\"\n\n```\n\n14. Create a function that takes two parameters: one can be either a string or number, and the other can be either a boolean or an array of strings. Implement logic in the function to perform different operations based on the types of the parameters.\n```bash\nfunction performOperations(param1: string | number, param2: boolean | string[]): void {\n  if (typeof param1 === 'string' \u0026\u0026 Array.isArray(param2)) {\n    console.log('Performing operation 1');\n    console.log(`Param1: ${param1.toUpperCase()}`);\n    console.log(`Param2 Length: ${param2.length}`);\n  } else if (typeof param1 === 'number' \u0026\u0026 typeof param2 === 'boolean') {\n    console.log('Performing operation 2');\n    console.log(`Param1 Squared: ${param1 * param1}`);\n    console.log(`Param2: ${param2}`);\n  } else {\n    console.log('Invalid parameter types');\n  }\n}\nperformOperations(\"hello\", [\"apple\", \"banana\"]);  // Output: Performing operation 1 ... Param1: HELLO ... Param2 Length: 2\nperformOperations(5, true);  // Output: Performing operation 2 ... Param1 Squared: 25 ... Param2: true\nperformOperations(10, [\"apple\", \"banana\"]);  // Output: Invalid parameter types\n\n```\n\n15. Create a generic function called filterArray that takes an array of any type and a predicate function as parameters. The function should return a new array that contains only the elements for which the predicate function returns true. Ensure that the function is flexible enough to work with different types of arrays.\n```bash\nfunction filterArray\u003cT\u003e(arr: T[], predicate: (value: T) =\u003e boolean): T[] {\n  const filteredArray: T[] = [];\n\n  for (const element of arr) {\n    if (predicate(element)) {\n      filteredArray.push(element);\n    }\n  }\n\n  return filteredArray;\n}\nconst numbers = [1, 2, 3, 4, 5];\nconst evenNumbers = filterArray(numbers, (num) =\u003e num % 2 === 0);\nconsole.log(evenNumbers);  // Output: [2, 4]\n\nconst names = ['Alice', 'Bob', 'Charlie', 'Dave'];\nconst longNames = filterArray(names, (name) =\u003e name.length \u003e 4);\nconsole.log(longNames);  // Output: ['Alice', 'Charlie']\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fconquer-the-typescript-conceptual-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollo-level2-web-dev%2Fconquer-the-typescript-conceptual-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollo-level2-web-dev%2Fconquer-the-typescript-conceptual-session/lists"}