{"id":28558338,"url":"https://github.com/moinuddin-dotcom/l2-a1","last_synced_at":"2025-06-10T08:08:31.737Z","repository":{"id":292124505,"uuid":"979899790","full_name":"Moinuddin-dotcom/L2-A1","owner":"Moinuddin-dotcom","description":null,"archived":false,"fork":false,"pushed_at":"2025-05-08T09:27:01.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-08T09:39:29.849Z","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/Moinuddin-dotcom.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,"zenodo":null}},"created_at":"2025-05-08T08:44:37.000Z","updated_at":"2025-05-08T09:27:05.000Z","dependencies_parsed_at":"2025-05-08T09:49:38.411Z","dependency_job_id":null,"html_url":"https://github.com/Moinuddin-dotcom/L2-A1","commit_stats":null,"previous_names":["moinuddin-dotcom/l2-a1"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moinuddin-dotcom%2FL2-A1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moinuddin-dotcom%2FL2-A1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moinuddin-dotcom%2FL2-A1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moinuddin-dotcom%2FL2-A1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Moinuddin-dotcom","download_url":"https://codeload.github.com/Moinuddin-dotcom/L2-A1/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moinuddin-dotcom%2FL2-A1/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259033839,"owners_count":22795771,"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","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":"2025-06-10T08:08:31.196Z","updated_at":"2025-06-10T08:08:31.722Z","avatar_url":"https://github.com/Moinuddin-dotcom.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **What are some differences between interfaces and types in TypeScript?**\n\nIn TypeScript, both `interface` and `type` can be used to describe the shape of data. While they often appear similar, there are key differences that make each suited for different scenarios.\n\n### **✅ Primitive Types: Only with Type Alias:**\n\n    - type RollNumber = number;\n\nWe cannot use `interface` for primitives like `number`, `string`, etc., because `interface` is designed to describe the shape of objects.\n\n### **✅ Objects: Both Type Alias and Interface**\nBoth type and interface can be used to describe object shapes:\n\n```\n// Using interface\ninterface User {\n  name: string;\n  age: number;\n}\n\n// Using type alias\ntype User = {\n  name: string;\n  age: number;\n};\n```\n\n**Explanation:**\n\nBoth type and interface work well for defining objects. We can use either based on preference.\n\n- interface is better suited for building extensible and extendable shapes (especially with extends).\n- type allows more complex combinations like unions and intersections.\n\n### **✅ Extending Types**\n\nWe can extend or compose types in both:\n\n### **Using type with intersection:**\n\n```\ntype User = {\n  name: string;\n  age: number;\n};\n\ntype UserWithRole = User \u0026 { role: string };\n\nconst user: UserWithRole = {\n  name: 'Forklin',\n  age: 22,\n  role: 'admin',\n};\n```\n\n### **Using interface with extends:**\n\n```\ninterface User {\n  name: string;\n  age: number;\n}\n\ninterface UserWithRole extends User {\n  role: string;\n}\n\nconst user: UserWithRole = {\n  name: 'Forklin',\n  age: 22,\n  role: 'admin',\n};\n```\n\n**Explanation:**\n\n- type uses intersection (\u0026) to combine multiple types.\n\n- interface uses extends to inherit properties from other interfaces.\n\n- Both are useful for composition, but interface is generally preferred when working with class-like or hierarchical structures.\n\n### **✅ Arrays: Possible with Both, Cleaner with Type Alias**\n\nType aliases are generally cleaner for arrays:\n```\ntype RollNumbers = number[];\n\nconst rolls: RollNumbers = [1, 2, 3];\n```\n\n\nUsing interface for arrays requires index signatures:\n```\ninterface RollNumbers {\n  [index: number]: number;\n}\n\nconst rolls: RollNumbers = [1, 2, 3];\n```\n\n**Explanation:**\n\n- While both type and interface can define array shapes, type offers a much cleaner and intuitive syntax.\n\n- Use interface if you need more complex index signatures or object-like array behaviors.\n\n### **✅ Functions: Both Work**\n\nWe can describe function types using both:\n\n```\n// Using type alias\ntype Add = (num1: number, num2: number) =\u003e number;\n\n// Using interface\ninterface AddFn {\n  (num1: number, num2: number): number;\n}\n\nconst add: AddFn = (num1, num2) =\u003e num1+ num2;\n```\n\n**Explanation:**\n\n- Both `type` and `interface` can be used to define function signatures.\n- `type` is often simpler and easier to read for standalone function types.\n- `interface` is useful when the function is part of a structure or object contract.\n\n\n## **🎯 Finally**\n- Use `type` for **primitives**, **arrays**, **tuples, and union types**.\n- Use `interface` when working with **object shapes**, especially when you expect to extend them.\n- For **arrays and functions**, `type` is often cleaner, but both approaches are valid.\n\n\n_____________________________________________________________________________________________\n\n# **🔑 What is the use of the keyof keyword in TypeScript?**\n\nThe `keyof` keyword in TypeScript is used to extract the **keys of an object type** as a union of string literal types. It enables type-safe property access and helps prevent runtime errors when accessing object properties dynamically.\n\n### **✅ Example 1: Extracting Keys Automatically**\n```\ntype Vehicle = {\n  bike: string;\n  car: string;\n  ship: string;\n};\n\ntype Owner = 'bike' | 'car' | 'ship';  // manual\ntype Owner2 = keyof Vehicle;          // automatic\n```\n**Explanation:**\nInstead of manually writing 'bike' | 'car' | 'ship', we can use keyof Vehicle to get the same result. This is helpful when the object type is large or frequently changes.\n\n### **✅ Example 2: Safe Dynamic Property Access**\n\n```\nconst getUserData = \u003cO, K extends keyof O\u003e(obj: O, key: K) =\u003e {\n  return obj[key];\n};\n\nconst user = {\n  name: 'Moinuddin',\n  age: 27,\n  address: 'CTG'\n};\n\nconst result = getUserData(user, 'age'); // 27\n```\n### **Explanation:**\nThe function ensures that the key must be a valid property of the object obj.\nIf we try getUserData(user, 'invalidKey'), TypeScript will give a compile-time error, making your code safer.\n\n## **🚀 Final Thoughts**\nThe keyof keyword is a game-changer when writing generic utilities in TypeScript. It helps maintain correctness, improves developer experience, and makes APIs safer by catching bugs at compile-time.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoinuddin-dotcom%2Fl2-a1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoinuddin-dotcom%2Fl2-a1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoinuddin-dotcom%2Fl2-a1/lists"}