{"id":53540,"date":"2024-12-03T17:18:24","date_gmt":"2024-12-03T09:18:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53540\/"},"modified":"2024-12-03T17:18:24","modified_gmt":"2024-12-03T09:18:24","slug":"top-advanced-typescript-concepts-that-every-developer-should-know","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53540\/","title":{"rendered":"Top Advanced typescript concepts that Every Developer Should Know"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Top Advanced typescript concepts that Every Developer Should Know<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aTop Advanced typescript concepts that Every Developer Should Know\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>typescript \u662f\u4e00\u79cd\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\uff0c\u7531\u4e8e\u5176\u9644\u52a0\u7684\u7c7b\u578b\u5b89\u5168\u6027\uff0c\u901a\u5e38\u6bd4 javascript \u66f4\u53d7\u9752\u7750\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u5206\u4eab 10 \u4e2a\u6700\u91cd\u8981\u7684 typescript \u6982\u5ff5\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u60a8\u7684 typescript \u7f16\u7a0b\u6280\u80fd\u3002\u51c6\u5907\u597d\u4e86\u5417\uff1f\u5f00\u59cb\u5427\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733052833674c49a17d65e.jpg\" class=\"aligncenter\" title=\"Top Advanced typescript concepts that Every Developer Should Know\u63d2\u56fe\" alt=\"Top Advanced typescript concepts that Every Developer Should Know\u63d2\u56fe\" \/><\/p>\n<p><strong>1.\u6cdb\u578b<\/strong>\uff1a\u4f7f\u7528\u6cdb\u578b\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u53ef\u91cd\u7528\u7684\u7c7b\u578b\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u5904\u7406\u4eca\u5929\u548c\u660e\u5929\u7684\u6570\u636e\u3002<br \/> \u6cdb\u578b\u793a\u4f8b\uff1a<br \/> \u6211\u4eec\u53ef\u80fd\u60f3\u8981 typescript \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\u5c06\u53c2\u6570\u4f5c\u4e3a\u67d0\u79cd\u7c7b\u578b\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u80fd\u60f3\u8981\u8fd4\u56de\u76f8\u540c\u7684\u7c7b\u578b\u3002<\/p>\n<pre>function func&lt;t&gt;(args:t):t{\n    return args;\n}\n<\/pre>\n<p><strong>2.\u5177\u6709\u7c7b\u578b\u7ea6\u675f\u7684\u6cdb\u578b<\/strong>\uff1a\u73b0\u5728\u8ba9\u6211\u4eec\u901a\u8fc7\u5b9a\u4e49\u5b83\u53ea\u63a5\u53d7\u5b57\u7b26\u4e32\u548c\u6574\u6570\u6765\u9650\u5236\u7c7b\u578b t\uff1a<\/p>\n<pre>function func&lt;t extends string | number&gt;(value: t): t {\n    return value;\n}\n\nconst stringvalue = func(\"hello\"); \/\/ works, t is string\nconst numbervalue = func(42);      \/\/ works, t is number\n\n\/\/ const booleanvalue = func(true); \/\/ error: type 'boolean' is not assignable to type 'string | number'\n<\/pre>\n<p><strong>3.\u901a\u7528\u63a5\u53e3<\/strong>:<br \/> \u5f53\u60a8\u60f3\u8981\u4e3a\u4f7f\u7528\u5404\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3001\u7c7b\u6216\u51fd\u6570\u5b9a\u4e49\u5951\u7ea6\uff08\u5f62\u72b6\uff09\u65f6\uff0c\u63a5\u53e3\u6cdb\u578b\u975e\u5e38\u6709\u7528\u3002\u5b83\u4eec\u5141\u8bb8\u60a8\u5b9a\u4e49\u4e00\u4e2a\u84dd\u56fe\uff0c\u8be5\u84dd\u56fe\u53ef\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u540c\u65f6\u4fdd\u6301\u7ed3\u6784\u4e00\u81f4\u3002<\/p>\n<pre>\/\/ generic interface with type parameters t and u\ninterface repository&lt;t, u&gt; {\n    items: t[];           \/\/ array of items of type t\n    add(item: t): void;   \/\/ function to add an item of type t\n    getbyid(id: u): t | undefined; \/\/ function to get an item by id of type u\n}\n\n\/\/ implementing the repository interface for a user entity\ninterface user {\n    id: number;\n    name: string;\n}\n\nclass userrepository implements repository&lt;user, number&gt; {\n    items: user[] = [];\n\n    add(item: user): void {\n        this.items.push(item);\n    }\n\n     getbyid(idorname: number | string): user | undefined {\n        if (typeof idorname === 'string') {\n            \/\/ search by name if idorname is a string\n            console.log('searching by name:', idorname);\n            return this.items.find(user =&gt; user.name === idorname);\n        } else if (typeof idorname === 'number') {\n            \/\/ search by id if idorname is a number\n            console.log('searching by id:', idorname);\n            return this.items.find(user =&gt; user.id === idorname);\n        }\n        return undefined; \/\/ return undefined if no match found\n    }\n}\n\n\/\/ usage\nconst userrepo = new userrepository();\nuserrepo.add({ id: 1, name: \"alice\" });\nuserrepo.add({ id: 2, name: \"bob\" });\n\nconst user1 = userrepo.getbyid(1);\nconst user2 = userrepo.getbyid(\"bob\");\nconsole.log(user1); \/\/ output: { id: 1, name: \"alice\" }\nconsole.log(user2); \/\/ output: { id: 2, name: \"bob\" }\n\n<\/pre>\n<p><strong>4.\u6cdb\u578b\u7c7b\uff1a<\/strong>\uff1a\u5f53\u60a8\u5e0c\u671b\u7c7b\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u90fd\u9075\u5faa\u6cdb\u578b\u53c2\u6570\u6307\u5b9a\u7684\u7c7b\u578b\u65f6\uff0c\u8bf7\u4f7f\u7528\u6b64\u9009\u9879\u3002\u8fd9\u5141\u8bb8\u7075\u6d3b\u6027\uff0c\u540c\u65f6\u786e\u4fdd\u7c7b\u7684\u6bcf\u4e2a\u5c5e\u6027\u90fd\u4e0e\u4f20\u9012\u7ed9\u7c7b\u7684\u7c7b\u578b\u5339\u914d\u3002<\/p>\n<pre>interface user {\n    id: number;\n    name: string;\n    age: number;\n}\n\nclass userdetails&lt;t extends user&gt; {\n    id: t['id'];\n    name: t['name'];\n    age: t['age'];\n\n    constructor(user: t) {\n        this.id = user.id;\n        this.name = user.name;\n        this.age = user.age;\n    }\n\n    \/\/ method to get user details\n    getuserdetails(): string {\n        return `user: ${this.name}, id: ${this.id}, age: ${this.age}`;\n    }\n\n    \/\/ method to update user name\n    updatename(newname: string): void {\n        this.name = newname;\n    }\n\n    \/\/ method to update user age\n    updateage(newage: number): void {\n        this.age = newage;\n    }\n}\n\n\/\/ using the userdetails class with a user type\nconst user: user = { id: 1, name: \"alice\", age: 30 };\nconst userdetails = new userdetails(user);\n\nconsole.log(userdetails.getuserdetails());  \/\/ output: \"user: alice, id: 1, age: 30\"\n\n\/\/ updating user details\nuserdetails.updatename(\"bob\");\nuserdetails.updateage(35);\n\nconsole.log(userdetails.getuserdetails());  \/\/ output: \"user: bob, id: 1, age: 35\"\nconsole.log(new userdetails(\"30\"));  \/\/ error: \"this will throw error\" \n<\/pre>\n<p><strong>5.\u5c06\u7c7b\u578b\u53c2\u6570\u7ea6\u675f\u4e3a\u4f20\u9012\u7684\u7c7b\u578b<\/strong>\uff1a\u6709\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u53c2\u6570\u7c7b\u578b\u4f9d\u8d56\u4e8e\u5176\u4ed6\u4e00\u4e9b\u4f20\u9012\u7684\u53c2\u6570\u3002\u542c\u8d77\u6765\u5f88\u6df7\u4e71\uff0c\u8ba9\u6211\u4eec\u770b\u4e0b\u9762\u7684\u793a\u4f8b\u3002<\/p>\n<pre>function getproperty&lt;type&gt;(obj: type, key: keyof type) {\n  return obj[key];\n}\n\nlet x = { a: 1, b: 2, c: 3 };\ngetproperty(x, \"a\");  \/\/ valid\ngetproperty(x, \"d\");  \/\/ error: argument of type '\"d\"' is not assignable to parameter of type '\"a\" | \"b\" | \"c\"'.\n\n<\/pre>\n<p><strong>6.\u6761\u4ef6\u7c7b\u578b<\/strong>\uff1a\u901a\u5e38\uff0c\u6211\u4eec\u5e0c\u671b\u6211\u4eec\u7684\u7c7b\u578b\u662f\u4e00\u79cd\u7c7b\u578b\u6216\u53e6\u4e00\u79cd\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528\u6761\u4ef6\u7c7b\u578b\u3002<br \/> \u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u662f\uff1a<\/p>\n<pre>function func(param:number|boolean){\nreturn param;\n}\nconsole.log(func(2)) \/\/output: 2 will be printed\nconsole.log(func(\"true\")) \/\/error: boolean cannot be passed as argument\n\n<\/pre>\n<p>\u6709\u70b9\u590d\u6742\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>type hasproperty&lt;t, k extends keyof t&gt; = k extends \"age\" ? \"has age\" : \"has name\";\n\ninterface user {\n  name: string;\n  age: number;\n}\n\nlet test1: hasproperty&lt;user, \"age\"&gt;;  \/\/ \"has age\"\nlet test2: hasproperty&lt;user, \"name\"&gt;; \/\/ \"has name\"\nlet test3: hasproperty&lt;user, \"email\"&gt;; \/\/ error: type '\"email\"' is not assignable to parameter of type '\"age\" | \"name\"'.\n\n<\/pre>\n<p><strong>6.\u4ea4\u96c6\u7c7b\u578b\uff1a<\/strong>\u5f53\u6211\u4eec\u60f3\u8981\u5c06\u591a\u79cd\u7c7b\u578b\u5408\u5e76\u4e3a\u4e00\u4e2a\u7c7b\u578b\u65f6\uff0c\u8fd9\u4e9b\u7c7b\u578b\u975e\u5e38\u6709\u7528\uff0c\u5141\u8bb8\u7279\u5b9a\u7c7b\u578b\u7ee7\u627f\u5404\u79cd\u5176\u4ed6\u7c7b\u578b\u7684\u5c5e\u6027\u548c\u884c\u4e3a\u3002<br \/> \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u6709\u8da3\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>\/\/ defining the types for each area of well-being\n\ninterface mentalwellness {\n  mindfulnesspractice: boolean;\n  stresslevel: number; \/\/ scale of 1 to 10\n}\n\ninterface physicalwellness {\n  exercisefrequency: string; \/\/ e.g., \"daily\", \"weekly\"\n  sleepduration: number; \/\/ in hours\n}\n\ninterface productivity {\n  taskscompleted: number;\n  focuslevel: number; \/\/ scale of 1 to 10\n}\n\n\/\/ combining all three areas into a single type using intersection types\ntype healthybody = mentalwellness &amp; physicalwellness &amp; productivity;\n\n\/\/ example of a person with a balanced healthy body\nconst person: healthybody = {\n  mindfulnesspractice: true,\n  stresslevel: 4,\n  exercisefrequency: \"daily\",\n  sleepduration: 7,\n  taskscompleted: 15,\n  focuslevel: 8\n};\n\n\/\/ displaying the information\nconsole.log(person);\n\n<\/pre>\n<p><strong>7.infer \u5173\u952e\u5b57\uff1a<\/strong>\u5f53\u6211\u4eec\u60f3\u8981\u6709\u6761\u4ef6\u5730\u786e\u5b9a\u7279\u5b9a\u7c7b\u578b\u65f6\uff0cinfer \u5173\u952e\u5b57\u5f88\u6709\u7528\uff0c\u5e76\u4e14\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4ece\u8be5\u7c7b\u578b\u4e2d\u63d0\u53d6\u5b50\u7c7b\u578b\u3002<br \/> \u8fd9\u662f\u4e00\u822c\u8bed\u6cd5\uff1a<\/p>\n<pre>type conditionaltype&lt;t&gt; = t extends sometype ? inferredtype : othertype;\n<\/pre>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>type returntypeofpromise&lt;t&gt; = t extends promise&lt;infer u&gt; ? u : number;\n\ntype result = returntypeofpromise&lt;promise&lt;string&gt;&gt;;  \/\/ result is 'string'\ntype errorresult = returntypeofpromise&lt;number&gt;;      \/\/ errorresult is 'never'\n\nconst result: result = \"hello\";\nconsole.log(typeof result); \/\/ output: 'string'\n<\/pre>\n<p><strong>8.type variance<\/strong>\uff1a\u8fd9\u4e2a\u6982\u5ff5\u8ba8\u8bba\u4e86\u5b50\u7c7b\u578b\u548c\u7236\u7c7b\u578b\u5982\u4f55\u76f8\u4e92\u5173\u8054\u3002 <br \/> \u5b83\u4eec\u6709\u4e24\u79cd\u7c7b\u578b\uff1a<br \/><strong>\u534f\u65b9\u5dee\uff1a<\/strong>\u53ef\u4ee5\u5728\u9700\u8981\u7236\u7c7b\u578b\u7684\u5730\u65b9\u4f7f\u7528\u5b50\u7c7b\u578b\u3002<br \/> \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>class vehicle { }\nclass car extends vehicle { }\n\nfunction getcar(): car {\n  return new car();\n}\n\nfunction getvehicle(): vehicle {\n  return new vehicle();\n}\n\n\/\/ covariant assignment\nlet car: car = getcar();\nlet vehicle: vehicle = getvehicle(); \/\/ this works because car is a subtype of vehicle\n\n\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0ccar \u7ee7\u627f\u4e86 vehicle \u7c7b\u7684\u5c5e\u6027\uff0c\u56e0\u6b64\u5c06\u5176\u5206\u914d\u7ed9\u9700\u8981\u8d85\u7c7b\u578b\u7684\u5b50\u7c7b\u578b\u662f\u7edd\u5bf9\u6709\u6548\u7684\uff0c\u56e0\u4e3a\u5b50\u7c7b\u578b\u5c06\u5177\u6709\u8d85\u7c7b\u578b\u6240\u5177\u6709\u7684\u6240\u6709\u5c5e\u6027\u3002<br \/><strong>\u9006\u53d8\uff1a<\/strong>\u8fd9\u4e0e\u534f\u53d8\u76f8\u53cd\u3002\u6211\u4eec\u5728\u5b50\u7c7b\u578b\u5e94\u8be5\u51fa\u73b0\u7684\u5730\u65b9\u4f7f\u7528\u8d85\u7c7b\u578b\u3002<\/p>\n<pre>class vehicle {\n  startengine() {\n    console.log(\"vehicle engine starts\");\n  }\n}\n\nclass car extends vehicle {\n  honk() {\n    console.log(\"car honks\");\n  }\n}\n\nfunction processvehicle(vehicle: vehicle) {\n  vehicle.startengine(); \/\/ this works\n  \/\/ vehicle.honk(); \/\/ error: 'honk' does not exist on type 'vehicle'\n}\n\nfunction processcar(car: car) {\n  car.startengine(); \/\/ works because car extends vehicle\n  car.honk();        \/\/ works because 'car' has 'honk'\n}\n\nlet car: car = new car();\nprocessvehicle(car); \/\/ this works because of contravariance (car can be used as vehicle)\nprocesscar(car);     \/\/ this works as well because car is of type car\n\n\/\/ contravariance failure if you expect specific subtype behavior in the method\n\n\n<\/pre>\n<p>\u4f7f\u7528\u9006\u53d8\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c0f\u5fc3\u4e0d\u8981\u8bbf\u95ee\u7279\u5b9a\u4e8e\u5b50\u7c7b\u578b\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>9\u3002\u53cd\u601d\uff1a<\/strong> \u8fd9\u4e2a\u6982\u5ff5\u6d89\u53ca\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u867d\u7136 typescript \u4e3b\u8981\u5173\u6ce8\u7f16\u8bd1\u65f6\u7684\u7c7b\u578b\u68c0\u67e5\uff0c\u4f46\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5229\u7528 typescript \u8fd0\u7b97\u7b26\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u7c7b\u578b\u3002<br \/> typeof \u8fd0\u7b97\u7b26\uff1a\u6211\u4eec\u53ef\u4ee5\u5229\u7528 typeof \u8fd0\u7b97\u7b26\u5728\u8fd0\u884c\u65f6\u67e5\u627e\u53d8\u91cf\u7684\u7c7b\u578b<\/p>\n<pre>const num = 23;\nconsole.log(typeof num); \/\/ \"number\"\n\nconst flag = true;\nconsole.log(typeof flag); \/\/ \"boolean\"\n\n<\/pre>\n<p>instanceof \u8fd0\u7b97\u7b26\uff1ainstanceof \u8fd0\u7b97\u7b26\u53ef\u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u7c7b\u6216\u7279\u5b9a\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002<\/p>\n<pre>class vehicle {\n  model: string;\n  constructor(model: string) {\n    this.model = model;\n  }\n}\n\nconst benz = new vehicle(\"mercedes-benz\");\nconsole.log(benz instanceof vehicle); \/\/ true\n\n<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u7c7b\u578b\u3002<\/p>\n<p><strong>10.\u4f9d\u8d56\u6ce8\u5165\uff1a<\/strong>\u4f9d\u8d56\u6ce8\u5165\u662f\u4e00\u79cd\u6a21\u5f0f\uff0c\u5141\u8bb8\u60a8\u5c06\u4ee3\u7801\u5f15\u5165\u7ec4\u4ef6\u4e2d\uff0c\u800c\u65e0\u9700\u5728\u7ec4\u4ef6\u4e2d\u5b9e\u9645\u521b\u5efa\u6216\u7ba1\u7406\u5b83\u3002\u867d\u7136\u5b83\u770b\u8d77\u6765\u50cf\u662f\u4f7f\u7528\u5e93\uff0c\u4f46\u5b83\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u4e3a\u60a8\u4e0d\u9700\u8981\u901a\u8fc7 cdn \u6216 api \u5b89\u88c5\u6216\u5bfc\u5165\u5b83\u3002 <\/p>\n<p>\u4e4d\u4e00\u770b\uff0c\u5b83\u4f3c\u4e4e\u4e5f\u7c7b\u4f3c\u4e8e\u4f7f\u7528\u51fd\u6570\u6765\u5b9e\u73b0\u53ef\u91cd\u7528\u6027\uff0c\u56e0\u4e3a\u4e24\u8005\u90fd\u5141\u8bb8\u4ee3\u7801\u91cd\u7528\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u76f4\u63a5\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u51fd\u6570\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b83\u4eec\u4e4b\u95f4\u7684\u7d27\u5bc6\u8026\u5408\u3002\u8fd9\u610f\u5473\u7740\u51fd\u6570\u6216\u5176\u903b\u8f91\u7684\u4efb\u4f55\u53d8\u5316\u90fd\u53ef\u80fd\u5f71\u54cd\u5b83\u4f7f\u7528\u7684\u6bcf\u4e2a\u5730\u65b9\u3002<\/p>\n<p>\u4f9d\u8d56\u6ce8\u5165\u901a\u8fc7\u5c06\u4f9d\u8d56\u9879\u7684\u521b\u5efa\u4e0e\u4f7f\u7528\u5b83\u4eec\u7684\u7ec4\u4ef6\u89e3\u8026\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u6d4b\u8bd5\u3002<\/p>\n<p><strong>\u6ca1\u6709\u4f9d\u8d56\u6ce8\u5165\u7684\u793a\u4f8b<\/strong><\/p>\n<pre>\/\/ health-related service classes without interfaces\nclass mentalwellness {\n  getmentalwellnessadvice(): string {\n    return \"take time to meditate and relax your mind.\";\n  }\n}\n\nclass physicalwellness {\n  getphysicalwellnessadvice(): string {\n    return \"make sure to exercise daily for at least 30 minutes.\";\n  }\n}\n\n\/\/ healthadvice class directly creating instances of the services\nclass healthadvice {\n  private mentalwellnessservice: mentalwellness;\n  private physicalwellnessservice: physicalwellness;\n\n  \/\/ directly creating instances inside the class constructor\n  constructor() {\n    this.mentalwellnessservice = new mentalwellness();\n    this.physicalwellnessservice = new physicalwellness();\n  }\n\n  \/\/ method to get both mental and physical wellness advice\n  gethealthadvice(): string {\n    return `${this.mentalwellnessservice.getmentalwellnessadvice()} also, ${this.physicalwellnessservice.getphysicalwellnessadvice()}`;\n  }\n}\n\n\/\/ creating an instance of healthadvice, which itself creates instances of the services\nconst healthadvice = new healthadvice();\n\nconsole.log(healthadvice.gethealthadvice());\n\/\/ output: \"take time to meditate and relax your mind. also, make sure to exercise daily for at least 30 minutes.\"\n\n<\/pre>\n<p><strong>\u4f9d\u8d56\u6ce8\u5165\u793a\u4f8b<\/strong><\/p>\n<pre>\/\/ Health-related service interfaces with \"I\" prefix\ninterface IMentalWellnessService {\n  getMentalWellnessAdvice(): string;\n}\n\ninterface IPhysicalWellnessService {\n  getPhysicalWellnessAdvice(): string;\n}\n\n\/\/ Implementations of the services\nclass MentalWellness implements IMentalWellnessService {\n  getMentalWellnessAdvice(): string {\n    return \"Take time to meditate and relax your mind.\";\n  }\n}\n\nclass PhysicalWellness implements IPhysicalWellnessService {\n  getPhysicalWellnessAdvice(): string {\n    return \"Make sure to exercise daily for at least 30 minutes.\";\n  }\n}\n\n\/\/ HealthAdvice class that depends on services via interfaces\nclass HealthAdvice {\n  private mentalWellnessService: IMentalWellnessService;\n  private physicalWellnessService: IPhysicalWellnessService;\n\n  \/\/ Dependency injection via constructor\n  constructor(\n    mentalWellnessService: IMentalWellnessService,\n    physicalWellnessService: IPhysicalWellnessService\n  ) {\n    this.mentalWellnessService = mentalWellnessService;\n    this.physicalWellnessService = physicalWellnessService;\n  }\n\n  \/\/ Method to get both mental and physical wellness advice\n  getHealthAdvice(): string {\n    return `${this.mentalWellnessService.getMentalWellnessAdvice()} Also, ${this.physicalWellnessService.getPhysicalWellnessAdvice()}`;\n  }\n}\n\n\/\/ Dependency injection\nconst mentalWellness: IMentalWellnessService = new MentalWellness();\nconst physicalWellness: IPhysicalWellnessService = new PhysicalWellness();\n\n\/\/ Injecting services into the HealthAdvice class\nconst healthAdvice = new HealthAdvice(mentalWellness, physicalWellness);\n\nconsole.log(healthAdvice.getHealthAdvice());\n\/\/ Output: \"Take time to meditate and relax your mind. Also, Make sure to exercise daily for at least 30 minutes.\"\n\n\n<\/pre>\n<p>\u5728\u7d27\u5bc6\u8026\u5408\u7684\u573a\u666f\u4e2d\uff0c\u5982\u679c\u60a8\u4eca\u5929 mentalwellness \u7c7b\u4e2d\u6709\u4e00\u4e2astresslevel \u5c5e\u6027\uff0c\u5e76\u51b3\u5b9a\u660e\u5929\u5c06\u5176\u66f4\u6539\u4e3a\u5176\u4ed6\u5c5e\u6027\uff0c\u5219\u9700\u8981\u66f4\u65b0\u6240\u6709\u4f7f\u7528\u5b83\u7684\u5730\u65b9\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bb8\u591a\u91cd\u6784\u548c\u7ef4\u62a4\u6311\u6218\u3002 <\/p>\n<p>\u4f46\u662f\uff0c\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u548c\u63a5\u53e3\u7684\u4f7f\u7528\uff0c\u4f60\u53ef\u4ee5\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u9012\u4f9d\u8d56\u9879\uff08\u4f8b\u5982 mentalwellness \u670d\u52a1\uff09\uff0c\u5177\u4f53\u7684\u5b9e\u73b0\u7ec6\u8282\uff08\u4f8b\u5982stresslevel \u5c5e\u6027\uff09\u88ab\u62bd\u8c61\u5230\u63a5\u53e3\u540e\u9762\u3002\u8fd9\u610f\u5473\u7740\u53ea\u8981\u63a5\u53e3\u4fdd\u6301\u4e0d\u53d8\uff0c\u5bf9\u5c5e\u6027\u6216\u7c7b\u7684\u66f4\u6539\u4e0d\u9700\u8981\u4fee\u6539\u4f9d\u8d56\u7c7b\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u786e\u4fdd\u4ee3\u7801\u677e\u6563\u8026\u5408\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u4e14\u66f4\u6613\u4e8e\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u60a8\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6ce8\u5165\u6240\u9700\u7684\u5185\u5bb9\uff0c\u800c\u65e0\u9700\u7d27\u5bc6\u8026\u5408\u7ec4\u4ef6\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aTop Advanced typescript concepts that Every Developer Should Know\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u4e0a\u6d77\u7535\u8111\u56de\u6536\uff1a\u73af\u4fdd\u7406\u5ff5\u4e0e\u8d44\u6e90\u5229\u7528\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Top Advanced typescript concepts that Every Developer Should Know \u6536\u85cf \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aTop Advanced typescript concepts that Every Developer Should Know\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 typescript \u662f\u4e00\u79cd\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\uff0c\u7531\u4e8e\u5176\u9644\u52a0\u7684\u7c7b\u578b\u5b89\u5168\u6027\uff0c\u901a\u5e38\u6bd4 javascript \u66f4\u53d7\u9752\u7750\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u5206\u4eab 10 \u4e2a\u6700\u91cd\u8981\u7684 typescript \u6982\u5ff5\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u60a8\u7684 typescript \u7f16\u7a0b\u6280\u80fd\u3002\u51c6\u5907\u597d\u4e86\u5417\uff1f\u5f00\u59cb\u5427\u3002 1.\u6cdb\u578b\uff1a\u4f7f\u7528\u6cdb\u578b\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u53ef\u91cd\u7528\u7684\u7c7b\u578b\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u5904\u7406\u4eca\u5929\u548c\u660e\u5929\u7684\u6570\u636e\u3002 \u6cdb\u578b\u793a\u4f8b\uff1a \u6211\u4eec\u53ef\u80fd\u60f3\u8981 typescript \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\u5c06\u53c2\u6570\u4f5c\u4e3a\u67d0\u79cd\u7c7b\u578b\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u80fd\u60f3\u8981\u8fd4\u56de\u76f8\u540c\u7684\u7c7b\u578b\u3002 function func&lt;t&gt;(args:t):t{ return args; } 2.\u5177\u6709\u7c7b\u578b\u7ea6\u675f\u7684\u6cdb\u578b\uff1a\u73b0\u5728\u8ba9\u6211\u4eec\u901a\u8fc7\u5b9a\u4e49\u5b83\u53ea\u63a5\u53d7\u5b57\u7b26\u4e32\u548c\u6574\u6570\u6765\u9650\u5236\u7c7b\u578b t\uff1a function func&lt;t extends string | number&gt;(value: t): t { return value; } const stringvalue [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53540","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53540"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53540\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}