{"id":205,"date":"2026-07-14T09:26:29","date_gmt":"2026-07-14T01:26:29","guid":{"rendered":"https:\/\/yykcj.com\/?p=205"},"modified":"2026-07-14T09:26:29","modified_gmt":"2026-07-14T01:26:29","slug":"typescript-5-x-%e8%bf%9b%e9%98%b6%e7%94%a8%e6%b3%95%ef%bc%9a%e8%a3%85%e9%a5%b0%e5%99%a8%e3%80%81%e6%9d%a1%e4%bb%b6%e7%b1%bb%e5%9e%8b%e4%b8%8e%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=205","title":{"rendered":"TypeScript 5.x \u8fdb\u9636\u7528\u6cd5\uff1a\u88c5\u9970\u5668\u3001\u6761\u4ef6\u7c7b\u578b\u4e0e\u6027\u80fd\u4f18\u5316\u5b9e\u6218"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>TypeScript 5.x \u5e26\u6765\u4e86\u8bb8\u591a\u6fc0\u52a8\u4eba\u5fc3\u7684\u65b0\u7279\u6027\uff0c\u5c24\u5176\u662f\u88c5\u9970\u5668\u7684\u6807\u51c6\u5316\u3001\u6761\u4ef6\u7c7b\u578b\u7684\u589e\u5f3a\u4ee5\u53ca\u7c7b\u578b\u7cfb\u7edf\u6027\u80fd\u7684\u663e\u8457\u63d0\u5347\u3002\u7136\u800c\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u4ecd\u505c\u7559\u5728\u57fa\u7840\u7528\u6cd5\u4e0a\uff0c\u672a\u80fd\u5145\u5206\u5229\u7528\u8fd9\u4e9b\u7279\u6027\u6765\u63d0\u5347\u4ee3\u7801\u8d28\u91cf\u3002\u672c\u6587\u5c06\u5e26\u4f60\u6df1\u5165\u5b9e\u8df5\uff0c\u638c\u63e1 TypeScript 5.x \u7684\u8fdb\u9636\u7528\u6cd5\uff0c\u5305\u62ec\u88c5\u9970\u5668\u3001\u6761\u4ef6\u7c7b\u578b\u4ee5\u53ca\u6027\u80fd\u4f18\u5316\u6280\u5de7\u3002<\/p>\n<h2>\u4e00\u3001\u88c5\u9970\u5668\uff1a\u4ece\u5b9e\u9a8c\u5230\u6807\u51c6<\/h2>\n<p>TypeScript 5.x \u6b63\u5f0f\u5b9e\u73b0\u4e86 Stage 3 \u88c5\u9970\u5668\u63d0\u6848\uff0c\u4e0e\u4e4b\u524d\u7684\u5b9e\u9a8c\u6027\u88c5\u9970\u5668\u6709\u663e\u8457\u5dee\u5f02\u3002\u65b0\u88c5\u9970\u5668\u66f4\u7b80\u6d01\u3001\u7c7b\u578b\u5b89\u5168\uff0c\u5e76\u4e14\u4e0e JavaScript \u6807\u51c6\u5bf9\u9f50\u3002<\/p>\n<h3>1.1 \u542f\u7528\u65b0\u88c5\u9970\u5668<\/h3>\n<p>\u5728 <code>tsconfig.json<\/code> \u4e2d\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n    &quot;compilerOptions&quot;: {\n        &quot;target&quot;: &quot;ES2022&quot;,\n        &quot;experimentalDecorators&quot;: false \/\/ \u5173\u95ed\u65e7\u88c5\u9970\u5668\n    }\n}\n<\/code><\/pre>\n<h3>1.2 \u57fa\u672c\u7528\u6cd5<\/h3>\n<p>\u65b0\u88c5\u9970\u5668\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a<code>target<\/code>\uff08\u88ab\u88c5\u9970\u7684\u5143\u7d20\uff09\u548c <code>context<\/code>\uff08\u5305\u542b\u5143\u6570\u636e\u7684\u5bf9\u8c61\uff09\u3002<\/p>\n<pre><code class=\"language-typescript\">\nfunction logged&lt;T extends (...args: any[]) =&gt; any&gt;(\n    target: T,\n    context: ClassMethodDecoratorContext\n) {\n    return function (this: any, ...args: Parameters&lt;T&gt;) {\n        console.log(`Calling ${String(context.name)} with`, args);\n        return target.call(this, ...args);\n    };\n}\n\nclass Calculator {\n    @logged\n    add(a: number, b: number): number {\n        return a + b;\n    }\n}\n\nconst calc = new Calculator();\ncalc.add(2, 3); \/\/ \u8f93\u51fa\u65e5\u5fd7\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a\u65b0\u88c5\u9970\u5668\u4e0d\u80fd\u7528\u4e8e\u53c2\u6570\u88c5\u9970\u5668\uff1b\u5982\u679c\u9700\u8981\u53c2\u6570\u88c5\u9970\u5668\uff0c\u4ecd\u9700\u4f7f\u7528\u65e7\u88c5\u9970\u5668\uff08<code>experimentalDecorators: true<\/code>\uff09\uff0c\u4f46\u4e24\u8005\u4e0d\u53ef\u6df7\u7528\u3002<\/p>\n<h3>1.3 \u81ea\u5b9a\u4e49\u88c5\u9970\u5668\u5de5\u5382<\/h3>\n<p>\u88c5\u9970\u5668\u5de5\u5382\u8fd4\u56de\u4e00\u4e2a\u88c5\u9970\u5668\u51fd\u6570\uff0c\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-typescript\">\nfunction validate(min: number, max: number) {\n    return function (\n        target: any,\n        context: ClassAccessorDecoratorContext\n    ) {\n        return {\n            get(this: any) {\n                return target.get.call(this);\n            },\n            set(this: any, value: number) {\n                if (value &lt; min || value &gt; max) {\n                    throw new Error(`Value must be between ${min} and ${max}`);\n                }\n                target.set.call(this, value);\n            }\n        };\n    };\n}\n\nclass Temperature {\n    @validate(0, 100)\n    accessor celsius: number = 0;\n}\n\nconst temp = new Temperature();\ntemp.celsius = 50; \/\/ \u6b63\u5e38\ntemp.celsius = 150; \/\/ \u629b\u51fa\u9519\u8bef\n<\/code><\/pre>\n<h2>\u4e8c\u3001\u6761\u4ef6\u7c7b\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6a21\u5f0f<\/h2>\n<p>\u6761\u4ef6\u7c7b\u578b\u8ba9\u7c7b\u578b\u8ba1\u7b97\u53d8\u5f97\u7075\u6d3b\uff0cTypeScript 5.x \u8fdb\u4e00\u6b65\u589e\u5f3a\u4e86\u5176\u80fd\u529b\u3002<\/p>\n<h3>2.1 \u5206\u5e03\u5f0f\u6761\u4ef6\u7c7b\u578b<\/h3>\n<p>\u5f53\u6761\u4ef6\u7c7b\u578b\u7528\u4e8e\u6cdb\u578b\u4e14\u6cdb\u578b\u4e3a\u8054\u5408\u7c7b\u578b\u65f6\uff0c\u4f1a\u81ea\u52a8\u5206\u5e03\u5f0f\u6c42\u503c\uff1a<\/p>\n<pre><code class=\"language-typescript\">\ntype ToArray&lt;T&gt; = T extends unknown ? T[] : never;\ntype Result = ToArray&lt;string | number&gt;; \/\/ string[] | number[]\n<\/code><\/pre>\n<h3>2.2 \u6a21\u677f\u5b57\u9762\u91cf\u7c7b\u578b\u4e0e\u6761\u4ef6\u7c7b\u578b\u7ed3\u5408<\/h3>\n<pre><code class=\"language-typescript\">\ntype EventName&lt;T extends string&gt; = `${T}Changed`;\ntype Handler&lt;T extends string&gt; = (event: EventName&lt;T&gt;) =&gt; void;\n\nfunction createListener&lt;T extends string&gt;(event: T, handler: Handler&lt;T&gt;) {}\n\ncreateListener(&#x27;name&#x27;, (event) =&gt; {\n    \/\/ event \u7c7b\u578b\u4e3a &quot;nameChanged&quot;\n});\n<\/code><\/pre>\n<h3>2.3 infer \u5173\u952e\u5b57\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<p><code>infer<\/code> \u7528\u4e8e\u5728\u6761\u4ef6\u7c7b\u578b\u4e2d\u63a8\u65ad\u7c7b\u578b\u53d8\u91cf\uff1a<\/p>\n<pre><code class=\"language-typescript\">\ntype ReturnTypeOf&lt;T&gt; = T extends (...args: any[]) =&gt; infer R ? R : never;\n\nfunction greet() { return &#x27;hello&#x27;; }\ntype GreetReturn = ReturnTypeOf&lt;typeof greet&gt;; \/\/ string\n<\/code><\/pre>\n<p>\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a\u63d0\u53d6 Promise \u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-typescript\">\ntype Unwrap&lt;T&gt; = T extends Promise&lt;infer U&gt; ? Unwrap&lt;U&gt; : T;\ntype Result = Unwrap&lt;Promise&lt;Promise&lt;string&gt;&gt;&gt;; \/\/ string\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>infer<\/code> \u53ea\u80fd\u5728\u6761\u4ef6\u7c7b\u578b\u7684 <code>extends<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u3002<\/p>\n<h2>\u4e09\u3001\u6027\u80fd\u4f18\u5316\uff1a\u8ba9\u7c7b\u578b\u68c0\u67e5\u98de\u8d77\u6765<\/h2>\n<p>TypeScript 5.x \u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u63d0\u5347\u4e86\u7c7b\u578b\u68c0\u67e5\u6027\u80fd\u3002<\/p>\n<h3>3.1 \u4f7f\u7528 <code>--isolatedModules<\/code> \u548c <code>--verbatimModuleSyntax<\/code><\/h3>\n<p>\u8fd9\u4e24\u4e2a\u9009\u9879\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u7f16\u8bd1\u65f6\u7684\u7c7b\u578b\u68c0\u67e5\u5de5\u4f5c\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n    &quot;compilerOptions&quot;: {\n        &quot;isolatedModules&quot;: true,\n        &quot;verbatimModuleSyntax&quot;: true\n    }\n}\n<\/code><\/pre>\n<ul>\n<li><code>isolatedModules<\/code>\uff1a\u786e\u4fdd\u6bcf\u4e2a\u6587\u4ef6\u53ef\u4ee5\u72ec\u7acb\u7f16\u8bd1\uff0c\u907f\u514d\u8de8\u6587\u4ef6\u7c7b\u578b\u63a8\u65ad\u3002<\/li>\n<li><code>verbatimModuleSyntax<\/code>\uff1a\u4fdd\u7559\u6a21\u5757\u8bed\u6cd5\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7c7b\u578b\u64e6\u9664\u3002<\/li>\n<\/ul>\n<h3>3.2 \u907f\u514d\u5927\u578b\u8054\u5408\u7c7b\u578b<\/h3>\n<p>\u5927\u578b\u8054\u5408\u7c7b\u578b\uff08\u8d85\u8fc7 50 \u4e2a\u6210\u5458\uff09\u4f1a\u4e25\u91cd\u5f71\u54cd\u6027\u80fd\u3002\u4f7f\u7528\u63a5\u53e3\u6216\u7c7b\u578b\u6620\u5c04\u66ff\u4ee3\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ \u4e0d\u63a8\u8350\ntype Status = &#x27;a&#x27; | &#x27;b&#x27; | ... | &#x27;z&#x27;; \/\/ 26\u4e2a\n\n\/\/ \u63a8\u8350\ninterface StatusMap {\n    a: any;\n    b: any;\n    \/\/ ...\n}\ntype Status = keyof StatusMap;\n<\/code><\/pre>\n<h3>3.3 \u4f7f\u7528 <code>@ts-expect-error<\/code> \u66ff\u4ee3 <code>@ts-ignore<\/code><\/h3>\n<p><code>@ts-ignore<\/code> \u4f1a\u8df3\u8fc7\u7c7b\u578b\u68c0\u67e5\uff0c\u4f46\u53ef\u80fd\u9690\u85cf\u9519\u8bef\u3002<code>@ts-expect-error<\/code> \u66f4\u5b89\u5168\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ @ts-expect-error - \u4e0b\u4e00\u884c\u9884\u671f\u6709\u9519\u8bef\nconst x: number = &#x27;string&#x27;; \/\/ \u9519\u8bef\u88ab\u9884\u671f\uff0c\u4e0d\u4f1a\u62a5\u9519\n<\/code><\/pre>\n<h3>3.4 \u5229\u7528 <code>satisfies<\/code> \u64cd\u4f5c\u7b26<\/h3>\n<p><code>satisfies<\/code> \u5728 4.9 \u5f15\u5165\uff0c5.x \u589e\u5f3a\uff0c\u7528\u4e8e\u9a8c\u8bc1\u7c7b\u578b\u4f46\u4e0d\u6539\u53d8\u63a8\u65ad\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-typescript\">\nconst palette = {\n    red: [255, 0, 0],\n    green: &#x27;#00ff00&#x27;,\n} satisfies Record&lt;string, string | number[]&gt;;\n\n\/\/ palette.red \u63a8\u65ad\u4e3a number[]\uff0c\u800c\u975e string | number[]\npalette.red.map(x =&gt; x); \/\/ \u6b63\u786e\n<\/code><\/pre>\n<h2>\u56db\u3001\u5b9e\u6218\uff1a\u6784\u5efa\u7c7b\u578b\u5b89\u5168\u7684 API \u5ba2\u6237\u7aef<\/h2>\n<p>\u7ed3\u5408\u4e0a\u8ff0\u7279\u6027\uff0c\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u7c7b\u578b\u5b89\u5168\u7684 HTTP \u5ba2\u6237\u7aef\u3002<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ \u5b9a\u4e49 API \u7aef\u70b9\u7c7b\u578b\ntype Endpoint = {\n    &#x27;\/users&#x27;: { method: &#x27;GET&#x27;; response: User[] };\n    &#x27;\/users\/:id&#x27;: { method: &#x27;GET&#x27;; response: User };\n    &#x27;\/users&#x27;: { method: &#x27;POST&#x27;; body: Omit&lt;User, &#x27;id&#x27;&gt;; response: User };\n};\n\n\/\/ \u6761\u4ef6\u7c7b\u578b\u63d0\u53d6\u54cd\u5e94\u7c7b\u578b\ntype ResponseOf&lt;T extends keyof Endpoint&gt; = Endpoint[T] extends { response: infer R } ? R : never;\n\n\/\/ \u8bf7\u6c42\u51fd\u6570\nasync function request&lt;T extends keyof Endpoint&gt;(\n    url: T,\n    options?: { method?: Endpoint[T][&#x27;method&#x27;]; body?: any }\n): Promise&lt;ResponseOf&lt;T&gt;&gt; {\n    const response = await fetch(url, options);\n    return response.json();\n}\n\n\/\/ \u4f7f\u7528\nconst users = await request(&#x27;\/users&#x27;); \/\/ \u7c7b\u578b\u4e3a User[]\nconst user = await request(&#x27;\/users\/:id&#x27;, { method: &#x27;GET&#x27; }); \/\/ \u7c7b\u578b\u4e3a User\n<\/code><\/pre>\n<h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<p>TypeScript 5.x \u7684\u88c5\u9970\u5668\u3001\u6761\u4ef6\u7c7b\u578b\u548c\u6027\u80fd\u4f18\u5316\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5de5\u5177\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b9e\u8df5\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u65b0\u88c5\u9970\u5668\u7f16\u5199\u5143\u7f16\u7a0b\u4ee3\u7801<\/li>\n<li>\u5229\u7528\u6761\u4ef6\u7c7b\u578b\u8fdb\u884c\u590d\u6742\u7684\u7c7b\u578b\u8ba1\u7b97<\/li>\n<li>\u4f18\u5316\u9879\u76ee\u914d\u7f6e\u4ee5\u63d0\u5347\u7c7b\u578b\u68c0\u67e5\u901f\u5ea6<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b65\uff0c\u53ef\u4ee5\u63a2\u7d22 TypeScript 5.x \u7684\u6a21\u5757\u89e3\u6790\u7b56\u7565\uff08<code>moduleResolution: 'bundler'<\/code>\uff09\u4ee5\u53ca\u8fed\u4ee3\u5668\u8f85\u52a9\u65b9\u6cd5\uff08<code>Iterator<\/code> \u7c7b\u578b\uff09\u7b49\u7279\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-5-0\/\">TypeScript 5.x \u53d1\u5e03\u8bf4\u660e<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/tc39\/proposal-decorators\">Decorators \u63d0\u6848<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/microsoft\/TypeScript\/wiki\/Performance\">TypeScript \u6027\u80fd\u6307\u5357<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u89e3\u6790 TypeScript 5.x \u7684\u88c5\u9970\u5668\u65b0\u7279\u6027\u3001\u6761\u4ef6\u7c7b\u578b\u9ad8\u7ea7\u6a21\u5f0f\u4ee5\u53ca\u7c7b\u578b\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u5e2e\u52a9\u4e2d\u9ad8\u7ea7\u5f00\u53d1\u8005\u5199\u51fa\u66f4\u5b89\u5168\u3001\u66f4\u9ad8\u6548\u7684 TypeScript \u4ee3\u7801\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[40,6,130,131,129],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-typescript","tag-6","tag-130","tag-131","tag-129"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=205"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}