{"id":4387,"date":"2024-07-29T11:10:24","date_gmt":"2024-07-29T03:10:24","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4387"},"modified":"2024-07-29T11:10:24","modified_gmt":"2024-07-29T03:10:24","slug":"typescript-%e8%bf%9b%e9%98%b6%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4387","title":{"rendered":"TypeScript \u8fdb\u9636\u6559\u7a0b"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u9ad8\u7ea7\u7c7b\u578b<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ea4\u53c9\u7c7b\u578b\uff08Intersection Types\uff09<\/h4>\n\n\n\n<p>\u4ea4\u53c9\u7c7b\u578b\u5c06\u591a\u4e2a\u7c7b\u578b\u5408\u5e76\u4e3a\u4e00\u4e2a\u7c7b\u578b\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u5c5e\u6027\u5408\u5e76\u5230\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>interface Person { name: string; } interface Contact { phone: string; } type Employee = Person &amp; Contact; let employee: Employee = { name: \"John Doe\", phone: \"123-456-7890\" };<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8054\u5408\u7c7b\u578b\uff08Union Types\uff09<\/h4>\n\n\n\n<p>\u8054\u5408\u7c7b\u578b\u5141\u8bb8\u4e00\u4e2a\u53d8\u91cf\u53ef\u4ee5\u662f\u591a\u79cd\u7c7b\u578b\u4e4b\u4e00\u3002\u5b83\u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u503c\u53ef\u4ee5\u662f\u51e0\u79cd\u7c7b\u578b\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>function printId(id: number | string) { console.log(\"Your ID is: \" + id); } printId(101); printId(\"202\");<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7c7b\u578b\u5b88\u536b\uff08Type Guards\uff09<\/h4>\n\n\n\n<p>\u7c7b\u578b\u5b88\u536b\u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u4e00\u4e2a\u503c\u7684\u7c7b\u578b\u3002\u5b83\u4eec\u5e38\u7528\u4e8e\u8054\u5408\u7c7b\u578b\u7684\u6761\u4ef6\u68c0\u67e5\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>function isString(value: any): value is string { return typeof value === \"string\"; } function example(value: number | string) { if (isString(value)) { console.log(value.toUpperCase()); \/\/ \u5728\u8fd9\u91cc value \u662f string } else { console.log(value.toFixed(2)); \/\/ \u5728\u8fd9\u91cc value \u662f number } }<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7c7b\u578b\u64cd\u4f5c<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u7c7b\u578b\u522b\u540d\uff08Type Aliases\uff09<\/h4>\n\n\n\n<p>\u7c7b\u578b\u522b\u540d\u7528\u4e8e\u4e3a\u4e00\u4e2a\u7c7b\u578b\u8d77\u4e00\u4e2a\u65b0\u7684\u540d\u5b57\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>type StringOrNumber = string | number; let value: StringOrNumber = \"Hello\"; value = 42;<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7c7b\u578b\u65ad\u8a00\uff08Type Assertions\uff09<\/h4>\n\n\n\n<p>\u7c7b\u578b\u65ad\u8a00\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u4e2a\u503c\u7684\u5177\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>let someValue: any = \"this is a string\"; let strLength: number = (someValue as string).length;<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7d22\u5f15\u7c7b\u578b\uff08Index Types\uff09<\/h4>\n\n\n\n<p>\u7d22\u5f15\u7c7b\u578b\u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u7684\u52a8\u6001\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>function pluck&lt;T, K extends keyof T&gt;(obj: T, keys: K[]): T[K][] { return keys.map(key =&gt; obj[key]); } let person = { name: \"John\", age: 30, city: \"New York\" }; let personName = pluck(person, [\"name\"]); \/\/ \u8fd4\u56de [\"John\"]<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6620\u5c04\u7c7b\u578b\uff08Mapped Types\uff09<\/h4>\n\n\n\n<p>\u6620\u5c04\u7c7b\u578b\u57fa\u4e8e\u65e7\u7c7b\u578b\u521b\u5efa\u65b0\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>type Readonly&lt;T&gt; = { readonly [P in keyof T]: T[P]; }; type Partial&lt;T&gt; = { [P in keyof T]?: T[P]; }; interface Person { name: string; age: number; } let readonlyPerson: Readonly&lt;Person&gt; = { name: \"John\", age: 30 }; \/\/ readonlyPerson.name = \"Jane\"; \/\/ Error: Cannot assign to 'name' because it is a read-only property<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6cdb\u578b\uff08Generics\uff09<\/h3>\n\n\n\n<p>\u6cdb\u578b\u4f7f\u5f97\u7ec4\u4ef6\u53ef\u4ee5\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\u3002\u8fd9\u6837\u7528\u6237\u53ef\u4ee5\u7528\u81ea\u5df1\u7684\u6570\u636e\u7c7b\u578b\u6765\u5b9a\u4e49\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>function identity&lt;T&gt;(arg: T): T { return arg; } let output1 = identity&lt;string&gt;(\"myString\"); let output2 = identity&lt;number&gt;(42);<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6cdb\u578b\u63a5\u53e3\u548c\u7c7b<\/h4>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>interface GenericIdentityFn&lt;T&gt; { (arg: T): T; } function identity&lt;T&gt;(arg: T): T { return arg; } let myIdentity: GenericIdentityFn&lt;number&gt; = identity; class GenericNumber&lt;T&gt; { zeroValue: T; add: (x: T, y: T) =&gt; T; } let myGenericNumber = new GenericNumber&lt;number&gt;(); myGenericNumber.zeroValue = 0; myGenericNumber.add = function(x, y) { return x + y; };<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9ad8\u7ea7\u7c7b\u7279\u6027<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u62bd\u8c61\u7c7b\uff08Abstract Classes\uff09<\/h4>\n\n\n\n<p>\u62bd\u8c61\u7c7b\u662f\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u7684\u7c7b\uff0c\u5b83\u4eec\u53ea\u80fd\u88ab\u7ee7\u627f\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>abstract class Animal { abstract makeSound(): void; move(): void { console.log(\"Moving...\"); } } class Dog extends Animal { makeSound() { console.log(\"Woof! Woof!\"); } } let dog = new Dog(); dog.makeSound(); dog.move();<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7c7b\u88c5\u9970\u5668\uff08Class Decorators\uff09<\/h4>\n\n\n\n<p>\u88c5\u9970\u5668\u662f\u7528\u4e8e\u4fee\u6539\u7c7b\u53ca\u5176\u6210\u5458\u884c\u4e3a\u7684\u7279\u6b8a\u7c7b\u578b\u7684\u58f0\u660e\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>function sealed(constructor: Function) { Object.seal(constructor); Object.seal(constructor.prototype); } @sealed class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return \"Hello, \" + this.greeting; } }<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u548c\u547d\u540d\u7a7a\u95f4<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u6a21\u5757\uff08Modules\uff09<\/h4>\n\n\n\n<p>TypeScript \u4f7f\u7528 ES6 \u6a21\u5757\u8bed\u6cd5\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>\/\/ math.ts export function add(x: number, y: number): number { return x + y; } \/\/ main.ts import { add } from '.\/math'; console.log(add(5, 3));<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u547d\u540d\u7a7a\u95f4\uff08Namespaces\uff09<\/h4>\n\n\n\n<p>\u547d\u540d\u7a7a\u95f4\u7528\u4e8e\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5e76\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>namespace Validation { export interface StringValidator { isAcceptable(s: string): boolean; } const lettersRegexp = \/^[A-Za-z]+$\/; export class LettersOnlyValidator implements StringValidator { isAcceptable(s: string) { return lettersRegexp.test(s); } } } let validator = new Validation.LettersOnlyValidator(); console.log(validator.isAcceptable(\"Hello\")); \/\/ true<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7c7b\u578b\u58f0\u660e\uff08Type Declarations\uff09<\/h3>\n\n\n\n<p>TypeScript \u53ef\u4ee5\u4e0e JavaScript \u5e93\u4e00\u8d77\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u58f0\u660e\u6587\u4ef6\uff08<code>.d.ts<\/code>\uff09\u6765\u63cf\u8ff0\u5e93\u7684 API\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>\/\/ lodash.d.ts declare module \"lodash\" { export function chunk&lt;T&gt;(array: T[], size: number): T[][]; } \/\/ main.ts import { chunk } from \"lodash\"; console.log(chunk([1, 2, 3, 4, 5], 2)); \/\/ [[1, 2], [3, 4], [5]]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9ad8\u7ea7\u7c7b\u578b\u58f0\u660e<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u6761\u4ef6\u7c7b\u578b\uff08Conditional Types\uff09<\/h4>\n\n\n\n<p>\u6761\u4ef6\u7c7b\u578b\u6839\u636e\u6761\u4ef6\u9009\u62e9\u4e00\u79cd\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>type TypeName&lt;T&gt; = T extends string ? \"string\" : T extends number ? \"number\" : T extends boolean ? \"boolean\" : T extends undefined ? \"undefined\" : T extends Function ? \"function\" : \"object\"; type T1 = TypeName&lt;string&gt;; \/\/ \"string\" type T2 = TypeName&lt;\"a\"&gt;; \/\/ \"string\" type T3 = TypeName&lt;true&gt;; \/\/ \"boolean\" type T4 = TypeName&lt;() =&gt; void&gt;; \/\/ \"function\" type T5 = TypeName&lt;{}&gt;; \/\/ \"object\"<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7c7b\u578b\u63a8\u65ad\uff08Type Inference\uff09<\/h4>\n\n\n\n<p>TypeScript \u80fd\u591f\u6839\u636e\u4e0a\u4e0b\u6587\u81ea\u52a8\u63a8\u65ad\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>let x = 3; \/\/ TypeScript \u63a8\u65ad x \u7684\u7c7b\u578b\u4e3a number<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u5206\u5272\u548c\u52a8\u6001\u5bfc\u5165<\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5bfc\u5165\u8bed\u6cd5\u6765\u6309\u9700\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>\/\/ math.ts export function add(x: number, y: number): number { return x + y; } \/\/ main.ts async function loadMath() { const math = await import('.\/math'); console.log(math.add(5, 3)); } loadMath();<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mixins\uff08\u6df7\u5408\uff09<\/h3>\n\n\n\n<p>Mixins \u662f\u4e00\u79cd\u5b9e\u73b0\u591a\u7ee7\u627f\u7684\u6a21\u5f0f\uff0cTypeScript \u652f\u6301\u901a\u8fc7\u7c7b\u548c\u63a5\u53e3\u5b9e\u73b0 Mixins\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>class Disposable { isDisposed: boolean = false; dispose() { this.isDisposed = true; } } class Activatable { isActive: boolean = false; activate() { this.isActive = true; } deactivate() { this.isActive = false; } } class SmartObject implements Disposable, Activatable { isDisposed: boolean = false; dispose: () =&gt; void; isActive: boolean = false; activate: () =&gt; void; deactivate: () =&gt; void; constructor() { setInterval(() =&gt; console.log(this.isActive + \" : \" + this.isDisposed), 500); } } applyMixins(SmartObject, [Disposable, Activatable]); let smartObj = new SmartObject(); setTimeout(() =&gt; smartObj.activate(), 1000); function applyMixins(derivedCtor: any, baseCtors: any[]) { baseCtors.forEach(baseCtor =&gt; { Object.getOwnPropertyNames(baseCtor.prototype).forEach(name =&gt; { derivedCtor.prototype[name] = baseCtor.prototype[name]; }); }); }<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fdb\u9636\u5de5\u5177\u7c7b\u578b<\/h3>\n\n\n\n<p>TypeScript \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u5de5\u5177\u7c7b\u578b\uff0c\u7528\u4e8e\u5e38\u89c1\u7684\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>typescript<\/p>\n\n\n\n<p>\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<p><code>type Partial&lt;T&gt; = { [P in keyof T]?: T[P]; }; type Readonly&lt;T&gt; = { readonly [P in keyof T]: T[P]; }; type Pick&lt;T, K extends keyof T&gt; = { [P in K]: T[P]; }; type Record&lt;K extends keyof any, T&gt; = { [P in K]: T; };<\/code><\/p>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a TypeScript \u7684\u8fdb\u9636\u6559\u7a0b\uff0c\u6db5\u76d6\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7279\u6027\u548c\u7528\u6cd5\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u5185\u5bb9\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528 TypeScript \u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u7ea7\u7c7b\u578b \u4ea4\u53c9\u7c7b\u578b\uff08Intersection Types\uff09 \u4ea4\u53c9\u7c7b\u578b\u5c06\u591a\u4e2a\u7c7b\u578b\u5408\u5e76\u4e3a\u4e00\u4e2a\u7c7b\u578b\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u4e0d\u540c\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[284],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4387"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4387"}],"version-history":[{"count":1,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4387\/revisions"}],"predecessor-version":[{"id":4388,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4387\/revisions\/4388"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}