123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <title>TypeScript笔记 | 彪哥博客</title>
- <meta name="generator" content="VuePress 1.9.5">
- <link rel="icon" href="/blog/img/favicon.ico">
- <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
- <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
- <meta name="theme-color" content="#11a8cd">
-
- <link rel="preload" href="/blog/assets/css/0.styles.cf8cd190.css" as="style"><link rel="preload" href="/blog/assets/js/app.28edfb28.js" as="script"><link rel="preload" href="/blog/assets/js/2.395c0d18.js" as="script"><link rel="preload" href="/blog/assets/js/3.6748bd5c.js" as="script"><link rel="preload" href="/blog/assets/js/31.78e43a68.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cad3aa70.js"><link rel="prefetch" href="/blog/assets/js/100.08a8b2d8.js"><link rel="prefetch" href="/blog/assets/js/101.2aabb12c.js"><link rel="prefetch" href="/blog/assets/js/102.13f3cc4d.js"><link rel="prefetch" href="/blog/assets/js/103.c19aee03.js"><link rel="prefetch" href="/blog/assets/js/104.91a6aec1.js"><link rel="prefetch" href="/blog/assets/js/105.71de1aa4.js"><link rel="prefetch" href="/blog/assets/js/106.207422de.js"><link rel="prefetch" href="/blog/assets/js/107.bf754f60.js"><link rel="prefetch" href="/blog/assets/js/108.a43622c2.js"><link rel="prefetch" href="/blog/assets/js/109.3219d87b.js"><link rel="prefetch" href="/blog/assets/js/11.f2e9eca8.js"><link rel="prefetch" href="/blog/assets/js/110.f41596b0.js"><link rel="prefetch" href="/blog/assets/js/111.da99a105.js"><link rel="prefetch" href="/blog/assets/js/112.9dd75c6f.js"><link rel="prefetch" href="/blog/assets/js/113.9322f157.js"><link rel="prefetch" href="/blog/assets/js/114.c5150cc6.js"><link rel="prefetch" href="/blog/assets/js/115.f58b49f8.js"><link rel="prefetch" href="/blog/assets/js/116.8f7d9c6f.js"><link rel="prefetch" href="/blog/assets/js/117.3c970f48.js"><link rel="prefetch" href="/blog/assets/js/118.921e1d54.js"><link rel="prefetch" href="/blog/assets/js/119.0141defb.js"><link rel="prefetch" href="/blog/assets/js/12.98512c60.js"><link rel="prefetch" href="/blog/assets/js/120.de47a761.js"><link rel="prefetch" href="/blog/assets/js/121.a0b3693a.js"><link rel="prefetch" href="/blog/assets/js/122.6c7dd225.js"><link rel="prefetch" href="/blog/assets/js/123.dbff103c.js"><link rel="prefetch" href="/blog/assets/js/124.493776ef.js"><link rel="prefetch" href="/blog/assets/js/125.554c9fbf.js"><link rel="prefetch" href="/blog/assets/js/126.9d3b75dc.js"><link rel="prefetch" href="/blog/assets/js/127.aad20a7e.js"><link rel="prefetch" href="/blog/assets/js/128.6543adba.js"><link rel="prefetch" href="/blog/assets/js/129.d7c56b92.js"><link rel="prefetch" href="/blog/assets/js/13.a79fa0c7.js"><link rel="prefetch" href="/blog/assets/js/130.593d21f0.js"><link rel="prefetch" href="/blog/assets/js/131.4c90d8b8.js"><link rel="prefetch" href="/blog/assets/js/132.4ad12bdc.js"><link rel="prefetch" href="/blog/assets/js/133.6df121d8.js"><link rel="prefetch" href="/blog/assets/js/134.65e904d2.js"><link rel="prefetch" href="/blog/assets/js/135.47498729.js"><link rel="prefetch" href="/blog/assets/js/136.d99350df.js"><link rel="prefetch" href="/blog/assets/js/137.71e8757e.js"><link rel="prefetch" href="/blog/assets/js/138.75e29c6f.js"><link rel="prefetch" href="/blog/assets/js/139.766f20b7.js"><link rel="prefetch" href="/blog/assets/js/14.0fdf0c78.js"><link rel="prefetch" href="/blog/assets/js/140.0d9b8fbc.js"><link rel="prefetch" href="/blog/assets/js/141.128b6e26.js"><link rel="prefetch" href="/blog/assets/js/142.e004a584.js"><link rel="prefetch" href="/blog/assets/js/143.5a19a0b2.js"><link rel="prefetch" href="/blog/assets/js/144.5f397211.js"><link rel="prefetch" href="/blog/assets/js/145.3b994de3.js"><link rel="prefetch" href="/blog/assets/js/146.24441c89.js"><link rel="prefetch" href="/blog/assets/js/147.f7f1952e.js"><link rel="prefetch" href="/blog/assets/js/148.25581f95.js"><link rel="prefetch" href="/blog/assets/js/149.7a6ca4f7.js"><link rel="prefetch" href="/blog/assets/js/15.fce722b2.js"><link rel="prefetch" href="/blog/assets/js/150.3a1675b7.js"><link rel="prefetch" href="/blog/assets/js/151.250a4894.js"><link rel="prefetch" href="/blog/assets/js/152.fa700729.js"><link rel="prefetch" href="/blog/assets/js/153.cc421f44.js"><link rel="prefetch" href="/blog/assets/js/154.a04c0164.js"><link rel="prefetch" href="/blog/assets/js/155.b5c0abcd.js"><link rel="prefetch" href="/blog/assets/js/156.2ba750fc.js"><link rel="prefetch" href="/blog/assets/js/157.99ee745c.js"><link rel="prefetch" href="/blog/assets/js/158.09b15f2b.js"><link rel="prefetch" href="/blog/assets/js/159.3fd855e1.js"><link rel="prefetch" href="/blog/assets/js/16.7cf1b239.js"><link rel="prefetch" href="/blog/assets/js/160.307b6a2e.js"><link rel="prefetch" href="/blog/assets/js/161.97acae68.js"><link rel="prefetch" href="/blog/assets/js/162.86431056.js"><link rel="prefetch" href="/blog/assets/js/163.f1bfa2fe.js"><link rel="prefetch" href="/blog/assets/js/164.105b48fa.js"><link rel="prefetch" href="/blog/assets/js/165.700f83da.js"><link rel="prefetch" href="/blog/assets/js/166.8de21024.js"><link rel="prefetch" href="/blog/assets/js/167.2b670c3d.js"><link rel="prefetch" href="/blog/assets/js/168.330ac31c.js"><link rel="prefetch" href="/blog/assets/js/169.f8233c38.js"><link rel="prefetch" href="/blog/assets/js/17.93f492a1.js"><link rel="prefetch" href="/blog/assets/js/170.7c8b0366.js"><link rel="prefetch" href="/blog/assets/js/171.c3155533.js"><link rel="prefetch" href="/blog/assets/js/172.b659d767.js"><link rel="prefetch" href="/blog/assets/js/173.62c681db.js"><link rel="prefetch" href="/blog/assets/js/174.5c66f092.js"><link rel="prefetch" href="/blog/assets/js/175.d41dd28b.js"><link rel="prefetch" href="/blog/assets/js/176.e60d7f0a.js"><link rel="prefetch" href="/blog/assets/js/177.10de95b1.js"><link rel="prefetch" href="/blog/assets/js/178.f301674d.js"><link rel="prefetch" href="/blog/assets/js/179.4d54a4ff.js"><link rel="prefetch" href="/blog/assets/js/18.f17de23a.js"><link rel="prefetch" href="/blog/assets/js/180.955aa8ec.js"><link rel="prefetch" href="/blog/assets/js/181.a53e32e0.js"><link rel="prefetch" href="/blog/assets/js/182.38687994.js"><link rel="prefetch" href="/blog/assets/js/183.544fef00.js"><link rel="prefetch" href="/blog/assets/js/184.711e54a6.js"><link rel="prefetch" href="/blog/assets/js/185.20075148.js"><link rel="prefetch" href="/blog/assets/js/186.08c67f20.js"><link rel="prefetch" href="/blog/assets/js/187.7ca2d0c6.js"><link rel="prefetch" href="/blog/assets/js/188.cd167879.js"><link rel="prefetch" href="/blog/assets/js/189.e8e2eb21.js"><link rel="prefetch" href="/blog/assets/js/19.6b963460.js"><link rel="prefetch" href="/blog/assets/js/190.8b557318.js"><link rel="prefetch" href="/blog/assets/js/191.3e6fdc4f.js"><link rel="prefetch" href="/blog/assets/js/192.99d6e1d8.js"><link rel="prefetch" href="/blog/assets/js/193.4ced091e.js"><link rel="prefetch" href="/blog/assets/js/194.4b375e2e.js"><link rel="prefetch" href="/blog/assets/js/195.003e3d67.js"><link rel="prefetch" href="/blog/assets/js/196.7a3f55e5.js"><link rel="prefetch" href="/blog/assets/js/197.30d4c5b4.js"><link rel="prefetch" href="/blog/assets/js/198.628c2c1a.js"><link rel="prefetch" href="/blog/assets/js/199.89c4e586.js"><link rel="prefetch" href="/blog/assets/js/20.839dae41.js"><link rel="prefetch" href="/blog/assets/js/200.1d9f2ef9.js"><link rel="prefetch" href="/blog/assets/js/201.5ba078d9.js"><link rel="prefetch" href="/blog/assets/js/202.b49b23f4.js"><link rel="prefetch" href="/blog/assets/js/203.d88a03d9.js"><link rel="prefetch" href="/blog/assets/js/204.fb928277.js"><link rel="prefetch" href="/blog/assets/js/205.432c3d8d.js"><link rel="prefetch" href="/blog/assets/js/206.ed726599.js"><link rel="prefetch" href="/blog/assets/js/207.9ac30d7b.js"><link rel="prefetch" href="/blog/assets/js/208.c5e77adc.js"><link rel="prefetch" href="/blog/assets/js/209.d396aad5.js"><link rel="prefetch" href="/blog/assets/js/21.bd21bd29.js"><link rel="prefetch" href="/blog/assets/js/210.02e4ee2f.js"><link rel="prefetch" href="/blog/assets/js/211.0f8a9304.js"><link rel="prefetch" href="/blog/assets/js/212.9220dd3d.js"><link rel="prefetch" href="/blog/assets/js/213.89521ebd.js"><link rel="prefetch" href="/blog/assets/js/214.cd5ec468.js"><link rel="prefetch" href="/blog/assets/js/215.0084d772.js"><link rel="prefetch" href="/blog/assets/js/216.a72becb1.js"><link rel="prefetch" href="/blog/assets/js/217.71a7f3aa.js"><link rel="prefetch" href="/blog/assets/js/218.9793e19a.js"><link rel="prefetch" href="/blog/assets/js/219.c89175e9.js"><link rel="prefetch" href="/blog/assets/js/22.e6bcf65d.js"><link rel="prefetch" href="/blog/assets/js/220.d496e411.js"><link rel="prefetch" href="/blog/assets/js/221.07a9338a.js"><link rel="prefetch" href="/blog/assets/js/222.22bf261f.js"><link rel="prefetch" href="/blog/assets/js/223.b9c6a055.js"><link rel="prefetch" href="/blog/assets/js/224.364b4b11.js"><link rel="prefetch" href="/blog/assets/js/225.adec6660.js"><link rel="prefetch" href="/blog/assets/js/226.78192713.js"><link rel="prefetch" href="/blog/assets/js/227.0c1ec0e5.js"><link rel="prefetch" href="/blog/assets/js/228.a94ad835.js"><link rel="prefetch" href="/blog/assets/js/229.51820867.js"><link rel="prefetch" href="/blog/assets/js/23.7bb82fc7.js"><link rel="prefetch" href="/blog/assets/js/230.6eb1d9bf.js"><link rel="prefetch" href="/blog/assets/js/231.55d62061.js"><link rel="prefetch" href="/blog/assets/js/232.f938a3a8.js"><link rel="prefetch" href="/blog/assets/js/233.2c81f1b3.js"><link rel="prefetch" href="/blog/assets/js/234.4dbf5d59.js"><link rel="prefetch" href="/blog/assets/js/235.f78274d9.js"><link rel="prefetch" href="/blog/assets/js/236.a2b61bec.js"><link rel="prefetch" href="/blog/assets/js/237.6ae31c88.js"><link rel="prefetch" href="/blog/assets/js/238.2f5c56ae.js"><link rel="prefetch" href="/blog/assets/js/239.506a4e9f.js"><link rel="prefetch" href="/blog/assets/js/24.2c9c4ee6.js"><link rel="prefetch" href="/blog/assets/js/25.b4de33d1.js"><link rel="prefetch" href="/blog/assets/js/26.0bb98ba9.js"><link rel="prefetch" href="/blog/assets/js/27.df98327e.js"><link rel="prefetch" href="/blog/assets/js/28.31289bac.js"><link rel="prefetch" href="/blog/assets/js/29.45af5621.js"><link rel="prefetch" href="/blog/assets/js/30.d5c08e66.js"><link rel="prefetch" href="/blog/assets/js/32.66c0e717.js"><link rel="prefetch" href="/blog/assets/js/33.0efe6b11.js"><link rel="prefetch" href="/blog/assets/js/34.cb1866c1.js"><link rel="prefetch" href="/blog/assets/js/35.dca9b927.js"><link rel="prefetch" href="/blog/assets/js/36.fb4476c3.js"><link rel="prefetch" href="/blog/assets/js/37.09dfc1c5.js"><link rel="prefetch" href="/blog/assets/js/38.6624bf02.js"><link rel="prefetch" href="/blog/assets/js/39.df6c26ac.js"><link rel="prefetch" href="/blog/assets/js/4.44654b1a.js"><link rel="prefetch" href="/blog/assets/js/40.80101c19.js"><link rel="prefetch" href="/blog/assets/js/41.2b5e8c27.js"><link rel="prefetch" href="/blog/assets/js/42.c6ded3fe.js"><link rel="prefetch" href="/blog/assets/js/43.6d9424d6.js"><link rel="prefetch" href="/blog/assets/js/44.835e4b5c.js"><link rel="prefetch" href="/blog/assets/js/45.e7bb6f6a.js"><link rel="prefetch" href="/blog/assets/js/46.1b9286bf.js"><link rel="prefetch" href="/blog/assets/js/47.8d66ca97.js"><link rel="prefetch" href="/blog/assets/js/48.3c1102e1.js"><link rel="prefetch" href="/blog/assets/js/49.e17a3436.js"><link rel="prefetch" href="/blog/assets/js/5.88de390f.js"><link rel="prefetch" href="/blog/assets/js/50.6750f186.js"><link rel="prefetch" href="/blog/assets/js/51.9f93af9f.js"><link rel="prefetch" href="/blog/assets/js/52.f3ef3b5e.js"><link rel="prefetch" href="/blog/assets/js/53.a6bacd25.js"><link rel="prefetch" href="/blog/assets/js/54.dbb7c9ab.js"><link rel="prefetch" href="/blog/assets/js/55.2562d0c8.js"><link rel="prefetch" href="/blog/assets/js/56.14ea4931.js"><link rel="prefetch" href="/blog/assets/js/57.a2fad780.js"><link rel="prefetch" href="/blog/assets/js/58.8165b971.js"><link rel="prefetch" href="/blog/assets/js/59.556cab0d.js"><link rel="prefetch" href="/blog/assets/js/6.277038ca.js"><link rel="prefetch" href="/blog/assets/js/60.f048aa7c.js"><link rel="prefetch" href="/blog/assets/js/61.bdb307a8.js"><link rel="prefetch" href="/blog/assets/js/62.37a94f10.js"><link rel="prefetch" href="/blog/assets/js/63.74811780.js"><link rel="prefetch" href="/blog/assets/js/64.d3429318.js"><link rel="prefetch" href="/blog/assets/js/65.acfdd371.js"><link rel="prefetch" href="/blog/assets/js/66.cb805d9b.js"><link rel="prefetch" href="/blog/assets/js/67.39f85baa.js"><link rel="prefetch" href="/blog/assets/js/68.a1902a2a.js"><link rel="prefetch" href="/blog/assets/js/69.923fdd78.js"><link rel="prefetch" href="/blog/assets/js/7.474f8f8d.js"><link rel="prefetch" href="/blog/assets/js/70.1f3e978d.js"><link rel="prefetch" href="/blog/assets/js/71.13cd9358.js"><link rel="prefetch" href="/blog/assets/js/72.739b22a8.js"><link rel="prefetch" href="/blog/assets/js/73.95f69ae2.js"><link rel="prefetch" href="/blog/assets/js/74.b6624f6a.js"><link rel="prefetch" href="/blog/assets/js/75.31b56e72.js"><link rel="prefetch" href="/blog/assets/js/76.b9c0ce40.js"><link rel="prefetch" href="/blog/assets/js/77.46f6e413.js"><link rel="prefetch" href="/blog/assets/js/78.aebd00ee.js"><link rel="prefetch" href="/blog/assets/js/79.1b784d15.js"><link rel="prefetch" href="/blog/assets/js/8.9428e7ee.js"><link rel="prefetch" href="/blog/assets/js/80.1f550d53.js"><link rel="prefetch" href="/blog/assets/js/81.101cc131.js"><link rel="prefetch" href="/blog/assets/js/82.077c8298.js"><link rel="prefetch" href="/blog/assets/js/83.2e375d11.js"><link rel="prefetch" href="/blog/assets/js/84.38102a34.js"><link rel="prefetch" href="/blog/assets/js/85.24532d6a.js"><link rel="prefetch" href="/blog/assets/js/86.1dabbf00.js"><link rel="prefetch" href="/blog/assets/js/87.763da0f2.js"><link rel="prefetch" href="/blog/assets/js/88.ff6e5f7c.js"><link rel="prefetch" href="/blog/assets/js/89.187e5e16.js"><link rel="prefetch" href="/blog/assets/js/9.da143545.js"><link rel="prefetch" href="/blog/assets/js/90.3c8cff94.js"><link rel="prefetch" href="/blog/assets/js/91.a50bd44d.js"><link rel="prefetch" href="/blog/assets/js/92.5484868f.js"><link rel="prefetch" href="/blog/assets/js/93.c8ee75e3.js"><link rel="prefetch" href="/blog/assets/js/94.b18a3e9b.js"><link rel="prefetch" href="/blog/assets/js/95.cddef6ae.js"><link rel="prefetch" href="/blog/assets/js/96.80e5a938.js"><link rel="prefetch" href="/blog/assets/js/97.1f5e5197.js"><link rel="prefetch" href="/blog/assets/js/98.e3a275c8.js"><link rel="prefetch" href="/blog/assets/js/99.d33bf89e.js">
- <link rel="stylesheet" href="/blog/assets/css/0.styles.cf8cd190.css">
- </head>
- <body class="theme-mode-light">
- <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="彪哥博客" class="logo"> <span class="site-name can-hide">彪哥博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
- 个人游戏网站
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" aria-current="page" class="nav-link router-link-exact-active router-link-active">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
- GitHub
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://fastly.jsdelivr.net/gh/xugaoyi/image_store/blog/20200103123203.jpg"> <div class="blogger-info"><h3>彪哥</h3> <span>爱好前端</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
- 个人游戏网站
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" aria-current="page" class="nav-link router-link-exact-active router-link-active">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
- GitHub
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript文章</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>学习笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/note/javascript/" class="sidebar-link">《JavaScript教程》笔记</a></li><li><a href="/blog/note/js/" class="sidebar-link">《JavaScript高级程序设计》笔记</a></li><li><a href="/blog/note/es6/" class="sidebar-link">《ES6 教程》笔记</a></li><li><a href="/blog/note/vue/" class="sidebar-link">《Vue》笔记</a></li><li><a href="/blog/note/react/" class="sidebar-link">《React》笔记</a></li><li><a href="/blog/note/typescript-axios/" class="sidebar-link">《TypeScript 从零实现 axios》</a></li><li><a href="/blog/note/git/" class="sidebar-link">《Git》学习笔记</a></li><li><a href="/blog/pages/51afd6/" aria-current="page" class="active sidebar-link">TypeScript笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#类型注解" class="sidebar-link">类型注解</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#有哪些基础类型注解" class="sidebar-link">有哪些基础类型注解?</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#什么是类型注解和类型推断、类型断言" class="sidebar-link">什么是类型注解和类型推断、类型断言?</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#类型断言" class="sidebar-link">类型断言</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#对象的类型" class="sidebar-link">对象的类型</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#函数的类型注解" class="sidebar-link">函数的类型注解</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#函数中的this类型" class="sidebar-link">函数中的this类型</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#函数重载-函数参数间的组合约定" class="sidebar-link">函数重载 (函数参数间的组合约定)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#数组的类型注解" class="sidebar-link">数组的类型注解</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#元组" class="sidebar-link">元组</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#接口" class="sidebar-link">接口</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#es6-class类中应用ts" class="sidebar-link">ES6 class类中应用TS</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#class类的修饰器" class="sidebar-link">class类的修饰器</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#class类的构造函数中使用类型校验" class="sidebar-link">class类的构造函数中使用类型校验</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#class类的getter、setter和static" class="sidebar-link">class类的getter、setter和static</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#class类的只读属性" class="sidebar-link">class类的只读属性</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#抽象类" class="sidebar-link">抽象类</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#联合类型和类型保护" class="sidebar-link">联合类型和类型保护</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#枚举" class="sidebar-link">枚举</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#泛型" class="sidebar-link">泛型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#函数中的泛型使用" class="sidebar-link">函数中的泛型使用</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/51afd6/#class类中使用泛型" class="sidebar-link">class类中使用泛型</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#类型保护" class="sidebar-link">类型保护</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#typeof" class="sidebar-link">typeof</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#instanceof" class="sidebar-link">instanceof</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#in" class="sidebar-link">in</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#字面量类型保护" class="sidebar-link">字面量类型保护</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#自定义类型保护" class="sidebar-link">自定义类型保护</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#类型操作" class="sidebar-link">类型操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#typeof-2" class="sidebar-link">typeof</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#keyof" class="sidebar-link">keyof</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/51afd6/#in-2" class="sidebar-link">in</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/51afd6/#配置文件tsconfig-json" class="sidebar-link">配置文件tsconfig.json</a></li></ul></li><li><a href="/blog/note/wx-miniprogram/" class="sidebar-link">小程序笔记</a></li><li><a href="/blog/pages/4643cd/" class="sidebar-link">JS设计模式总结笔记</a></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06970110><div class="articleInfo" data-v-06970110><ul class="breadcrumbs" data-v-06970110><li data-v-06970110><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-06970110></a></li> <li data-v-06970110><a href="/blog/web/#前端" data-v-06970110>前端</a></li><li data-v-06970110><a href="/blog/web/#学习笔记" data-v-06970110>学习笔记</a></li></ul> <div class="info" data-v-06970110><div title="作者" class="author iconfont icon-touxiang" data-v-06970110><a href="https://github.com/heBody" target="_blank" title="作者" class="beLink" data-v-06970110>heBody</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06970110><a href="javascript:;" data-v-06970110>2020-10-08</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABKFJREFUSA3tVl1oFVcQnrMbrak3QUgkya1akpJYcrUtIqW1JvFBE9LiQ5v6JmJpolbMg32rVrhgoYK0QiMY6i9Y6EMaW5D+xFJaTYItIuK2Kr3+BJNwkxBj05sQY3b3nM6cs2dv9t7NT/vQJw/sndk5M/PNzJkzewGerP+pAmy+ON8lLzUJgA8ZYxYIYZmGYRnctDaWvJJAmTtfP1pvXsBCCPP8QFcCaRkZYACgDZFO4stNIcBCajEOlmmC9XpJ9bAGCaPaPmzPl32dvLSVu3BWCTQs0XQQ6g0DYgwLIoAZbBCdW/i+781o1VVlm/410mw4h06Y7bIPHNyWDyL4FHkX03Q8SrzNhZTZriieckWt7cL6MM85YcLpsi/7O9/iXFT6MswI0DmmpkSaJ0qLxFIm3+i1THHB3zmBH3PYx9CcykcLOeQVVa7QtdxTgQgEleX2AjHYfwA+2ddV77ruGoJUbhGDI09YSNXyMpUt5ylOzxgbUmtOp7NmbNt8v3arjTBfYELmLUV+M+nSawNNAUqpT3ClJWg5I3BLT+cGW/DXNGCa6tx1aakCGEigArTn4TDIPdrXXYKCZNrHLMCOEPvHBlLQ99s9eHB7EB6NTki73CVPQ2F5MSx/uRQixfmq7rK0wYD8w8E905bnPDfwoWs/rfv93NWN/ZfvwsLIU7A09gxECyISeGJkHAau98L97tuw7NXnoPyNF8FcYGLGKsOs0mN3OEyec9esGW/ZEl945dTP34wlR2FZVQWU1q0Cw8Tr7p+hgLLNL0FPxx/Q35mA8aEUrH6nCgwEl0tn7wUiZYJnNRh6DK4UH/k0lfyrsBKdPVv/AriGIQcEDQZ65LBAGe2Rzui9Ybjz7XUppz1/uKBbyVPGkN3ZAeC6hr0x7Nr38N5+EqkoOm17xpoqR9ohQF55ERSvr4Dkr3chNfC3DMzGJlNBElW8w9nsGQvhNGIzDkXzCg8cLK951xHsFBlTJspJNi3ZFIMF2AeDV3q8DNOB+YHi6QTrChDIWDBRi5U5f+ZMfJLu3ccrqxtdxk4SKH336LFxSmkqefwU5T8fhdSdQf9IVKD6aNiwI/hnmcAZ91isYMJIaCUCx9W098+LgruikeTqzqqxKPUwqJyCPJiyemVVZBOijDGjD38Os0jOiSPL1z3SPjXNANbiNPXAdzTfukjjuknNBbyz3nwgTd3AVFqUJ5hpHlq9MveLnWwttUfoygBmvVjuikxND3znrhsELnZk7k+OjIGxeNEkomyLVta0xxn+HZhjBc4YZ/AFjHjz9u3xRZl2BN4aq9nFwWh16IrQ1aHHEd3j1+4/dB9OtH4e29A2H1DyHQRmOSfQZ1Fy7MHBTGB6J/Djq6p3OxyO2cB+4Car7v/o3GXgfAkj23+x9ID1Teoamo/SXcbvSf2PX7Vc8DdCmE1vN9di+32P9/5YR3vLnhCVGUWBjEkr3yh4H8v9CzmsbdhzOKzsJKM90iFdaTMjRPhGVsakRvOaRidljo6H6G7j+ctrJpsP+4COhDIl0La2+FS4+5mlocBaXY5QnGZysIBYoeSsl5qQzrSj/cgNrfuEzlWBfwA+EjrZyWUvpAAAAABJRU5ErkJggg==">TypeScript笔记<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="typescript-学习笔记"><a href="#typescript-学习笔记" class="header-anchor">#</a> TypeScript 学习笔记</h1> <p>TypeScript 的类型校验是给程序员看的,在编译后不会存在TS代码。</p> <h2 id="类型注解"><a href="#类型注解" class="header-anchor">#</a> 类型注解</h2> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span>person<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token keyword">void</span><span class="token punctuation">{</span> <span class="token comment">// 参数类型是字符串,没有返回值</span>
- <span class="token comment">///...</span>
- <span class="token punctuation">}</span>
- <span class="token function">fn</span><span class="token punctuation">(</span><span class="token string">'str'</span><span class="token punctuation">)</span> <span class="token comment">// 如传递参数非字符串,vscode编辑器中或在编译时将给出错误提示</span>
- <span class="token keyword">const</span> test<span class="token operator">:</span><span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">1</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="有哪些基础类型注解"><a href="#有哪些基础类型注解" class="header-anchor">#</a> 有哪些基础类型注解?</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 基础类型</span>
- <span class="token operator">:</span><span class="token builtin">string</span>
- <span class="token operator">:</span><span class="token builtin">number</span>
- <span class="token operator">:</span><span class="token builtin">boolean</span>
- <span class="token operator">:</span><span class="token keyword">null</span> <span class="token comment">// 只能是null值</span>
- <span class="token operator">:</span><span class="token keyword">undefined</span> <span class="token comment">// 只能是undefined值</span>
- <span class="token operator">:</span><span class="token builtin">symbol</span>
- <span class="token comment">// 引用类型</span>
- <span class="token operator">:</span>object <span class="token comment">// 不常用,多使用接口来给对象进行类型注解</span>
- <span class="token comment">// 其他</span>
- <span class="token operator">:</span><span class="token builtin">any</span> <span class="token comment">// 任意类型</span>
- <span class="token operator">:</span><span class="token keyword">void</span> <span class="token comment">// 空,用于函数的无返回值的注解</span>
- <span class="token operator">:</span><span class="token builtin">never</span> <span class="token comment">// 用于标注函数代码永远执行不完(如抛出错误的函数,死循环函数)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h3 id="什么是类型注解和类型推断、类型断言"><a href="#什么是类型注解和类型推断、类型断言" class="header-anchor">#</a> 什么是类型注解和类型推断、类型断言?</h3> <p><strong>类型注解</strong> 是显式的标注类型</p> <p><strong>类型推断</strong> 是编辑器根据值来自动推断出类型 (编辑器中鼠标移到变量会显示类型的提示)。</p> <p><strong>类型断言</strong> 是告诉编译器,“相信我,它就是这个类型”。</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 类型注解就是显式的写出类型</span>
- <span class="token keyword">const</span> myNumber<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">123</span>
- <span class="token comment">// 类型推断是根据值来推断类型 (鼠标移到变量会显示类型(或直接显示值)的提示)</span>
- <span class="token keyword">const</span> myString <span class="token operator">=</span> <span class="token string">'test'</span>
- <span class="token comment">// 类型断言(开发者明确知道是什么类型)</span>
- <span class="token keyword">const</span> someValue<span class="token operator">:</span><span class="token builtin">any</span> <span class="token operator">=</span> <span class="token string">'abc'</span>
- <span class="token keyword">const</span> strLength<span class="token operator">:</span><span class="token builtin">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span>someValue <span class="token keyword">as</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length <span class="token comment">// 断言someValue是字符串类型</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="类型断言"><a href="#类型断言" class="header-anchor">#</a> 类型断言</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">const</span> el <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'#img'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">(</span>el <span class="token keyword">as</span> HTMLImageElement<span class="token punctuation">)</span><span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token string">'xx'</span> <span class="token comment">// 断言el就是图片元素类型</span>
- <span class="token comment">// 对象内的类型断言</span>
- <span class="token keyword">type</span> <span class="token class-name">TestObj</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token builtin">string</span>
- <span class="token punctuation">}</span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
- test<span class="token operator">:</span> <span class="token operator"><</span>TestObj<span class="token operator">></span><span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token string">'aaa'</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 或者</span>
- <span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
- test<span class="token operator">:</span> <span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token string">'aaa'</span>
- <span class="token punctuation">}</span> <span class="token keyword">as</span> TestObj <span class="token punctuation">,</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="对象的类型"><a href="#对象的类型" class="header-anchor">#</a> 对象的类型</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 对象字面量的类型检查</span>
- <span class="token keyword">const</span> xiaojiejie<span class="token operator">:</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token builtin">number</span>
- <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'小红'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">18</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 标注:object</span>
- <span class="token keyword">const</span> obj<span class="token operator">:</span>object <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
- <span class="token comment">// 使用接口</span>
- <span class="token keyword">interface</span> <span class="token class-name">Person</span><span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span>
- age<span class="token operator">:</span> <span class="token builtin">number</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> xjj<span class="token operator">:</span> Person <span class="token operator">=</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'xh'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">18</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// class类的类型检查</span>
- <span class="token keyword">class</span> <span class="token class-name">Parson</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
- <span class="token keyword">const</span> xiaobai<span class="token operator">:</span> Parson <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Parson</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token comment">// 函数和返回值的类型检查</span>
- <span class="token keyword">const</span> <span class="token function-variable function">fn</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=></span> <span class="token function-variable function">string</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token string">'123'</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h3 id="函数的类型注解"><a href="#函数的类型注解" class="header-anchor">#</a> 函数的类型注解</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 函数返回值的类型注解 fn(): number {}</span>
- <span class="token keyword">function</span> <span class="token function">getTotal2</span><span class="token punctuation">(</span>one<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> two<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> one <span class="token operator">+</span> two
- <span class="token punctuation">}</span>
- <span class="token function">getTotal2</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
- <span class="token comment">// 无返回值: void</span>
- <span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 永远执行不玩的函数</span>
- <span class="token keyword">function</span> <span class="token function">setTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token builtin">never</span> <span class="token punctuation">{</span>
- <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 参数是对象时的注解 (也可以用接口)</span>
- <span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">{</span> one<span class="token punctuation">,</span> two <span class="token punctuation">}</span><span class="token operator">:</span> <span class="token punctuation">{</span>one<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> two<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> one <span class="token operator">+</span> two
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> total <span class="token operator">=</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">{</span>one<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> two<span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
- <span class="token comment">// 类型别名方式定义函数类型</span>
- <span class="token keyword">type</span> <span class="token class-name">Callback</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token builtin">string</span>
- <span class="token keyword">let</span> fn<span class="token operator">:</span> <span class="token function-variable function">Callback</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token string">''</span>
- <span class="token comment">// 接口方式定义函数类型</span>
- <span class="token keyword">interface</span> <span class="token class-name">ICallBack</span> <span class="token punctuation">{</span>
- <span class="token punctuation">(</span>a<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">let</span> fn1<span class="token operator">:</span> <span class="token function-variable function">ICallBack</span> <span class="token operator">=</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token string">''</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><h4 id="函数中的this类型"><a href="#函数中的this类型" class="header-anchor">#</a> 函数中的this类型</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name">TestObj</span> <span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span>
- <span class="token function-variable function">fn</span><span class="token operator">:</span> <span class="token punctuation">(</span>x<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">void</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 普通函数的this</span>
- <span class="token keyword">let</span> obj<span class="token operator">:</span> TestObj <span class="token operator">=</span> <span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
- <span class="token function">fn</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">:</span> TestObj<span class="token punctuation">,</span> x<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 注意这里的this非参数,只是给this类型注解。x还是第一个参数</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>a
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 箭头函数的this (this是固定指向当前声明函数所在的作用域)</span>
- <span class="token keyword">let</span> obj2<span class="token operator">:</span> TestObj <span class="token operator">=</span> <span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
- <span class="token function">fn</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">:</span> TestObj<span class="token punctuation">,</span> x<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>a
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h4 id="函数重载-函数参数间的组合约定"><a href="#函数重载-函数参数间的组合约定" class="header-anchor">#</a> 函数重载 (函数参数间的组合约定)</h4> <p>原生js中并没有真正的函数重载,重名函数会被覆盖,但ts中可以有。</p> <blockquote><p><a href="https://www.cnblogs.com/yugege/p/5539020.html" target="_blank" rel="noopener noreferrer">浅谈JavaScript函数重载<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <p>ts支持函数重载,一般用于针对传入不同的参数或参数数量以及返回值之间的类型约定。</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 第一套约定(attr是'display'时,value必须是 'block' | 'nonde')</span>
- <span class="token keyword">function</span> <span class="token function">showOrHide</span><span class="token punctuation">(</span>ele<span class="token operator">:</span> HTMLElement<span class="token punctuation">,</span> attr<span class="token operator">:</span> <span class="token string">'display'</span><span class="token punctuation">,</span> value<span class="token operator">:</span> <span class="token string">'block'</span> <span class="token operator">|</span> <span class="token string">'nonde'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// 第二套约定(attr是'opacity'时,value必须是number)</span>
- <span class="token keyword">function</span> <span class="token function">showOrHide</span><span class="token punctuation">(</span>ele<span class="token operator">:</span> HTMLElement<span class="token punctuation">,</span> attr<span class="token operator">:</span> <span class="token string">'opacity'</span><span class="token punctuation">,</span> value<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// 函数的实现</span>
- <span class="token keyword">function</span> <span class="token function">showOrHide</span><span class="token punctuation">(</span>ele<span class="token operator">:</span> HTMLElement<span class="token punctuation">,</span> attr<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">,</span> value<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// ...</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// interface也可以声明函数重载</span>
- <span class="token keyword">interface</span> <span class="token class-name">Fn</span><span class="token punctuation">{</span>
- <span class="token punctuation">(</span>name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token function">string</span>
- <span class="token punctuation">(</span>name<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">number</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> fn<span class="token operator">:</span> <span class="token function-variable function">Fn</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>更多 <a href="https://zhuanlan.zhihu.com/p/496792140" target="_blank" rel="noopener noreferrer">ts函数重载知识<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="数组的类型注解"><a href="#数组的类型注解" class="header-anchor">#</a> 数组的类型注解</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">const</span> numberArr<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
- <span class="token keyword">const</span> stringArr<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span>
- <span class="token keyword">const</span> undefinedArr<span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token keyword">undefined</span><span class="token punctuation">]</span>
- <span class="token keyword">const</span> arr<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token builtin">number</span> <span class="token operator">|</span> <span class="token builtin">string</span> <span class="token operator">|</span> <span class="token builtin">boolean</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]</span> <span class="token comment">// 用到联合类型</span>
- <span class="token comment">// 类型别名 type alias</span>
- <span class="token keyword">type</span> <span class="token class-name">lady</span> <span class="token operator">=</span> <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span> age<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token punctuation">}</span>
- <span class="token keyword">const</span> xiaojj<span class="token operator">:</span> lady<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
- <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'xiaojj'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">90</span>
- <span class="token punctuation">}</span><span class="token punctuation">,</span>
- <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'j'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">30</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">]</span>
- <span class="token comment">// 或用类的方式</span>
- <span class="token keyword">class</span> <span class="token class-name">Ady2</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> xiaojj2<span class="token operator">:</span> Ady2<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
- <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'xiaojj'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">90</span>
- <span class="token punctuation">}</span><span class="token punctuation">,</span>
- <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'j'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">30</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h2 id="元组"><a href="#元组" class="header-anchor">#</a> 元组</h2> <p>元组,可以理解为:<strong>已知元素数量和类型的数组</strong></p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 联合类型</span>
- <span class="token keyword">const</span> xjj<span class="token operator">:</span><span class="token punctuation">(</span><span class="token builtin">string</span> <span class="token operator">|</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">]</span> <span class="token comment">// 规定整个数组当中可以有string或number</span>
- <span class="token comment">// 元组注解 注意这里的注解只有一个中括号</span>
- <span class="token keyword">const</span> xjj1<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">]</span> <span class="token comment">// 规定了数组每个元素对应位置的类型</span>
- <span class="token comment">// Note: 在开发中元祖的使用在相对少</span>
- <span class="token comment">// CSV的方式定义数据格式; (二维数组时需要多加一个中括号)</span>
- <span class="token keyword">const</span> xjj2<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
- <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
- <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">]</span>
- <span class="token punctuation">]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="接口"><a href="#接口" class="header-anchor">#</a> 接口</h2> <p>接口,可以理解为对象属性的类型描述。和<code>类型别名</code>类似,不同的是 接口必须是一个对象,而别名可以直接是一个类型,如<code>type Girl = string</code></p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name">Girl</span> <span class="token punctuation">{</span> <span class="token comment">// 接口 (理解:对象属性的类型描述)</span>
- <span class="token keyword">readonly</span> name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span> <span class="token comment">// 只读属性 (定义之后不能再修改)</span>
- age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
- waistline<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span> <span class="token comment">// 加个问号表示是可选值</span>
- <span class="token punctuation">[</span>propname<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">]</span><span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">;</span> <span class="token comment">// 表示可以有不限制属性名的属性,但属性名需要是字符串,值可以是任意类型</span>
- <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span> <span class="token comment">// 函数类型,返回值是string (如无返回值时是 void)。 say()也可以加入参数类型检查,如say(p:number)</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 和类型别名类似,不同的是 接口必须是一个对象,而别名可以直接是一个类型,如 type Girl = string</span>
- <span class="token comment">// 接口的继承</span>
- <span class="token keyword">interface</span> <span class="token class-name">Teacher</span> <span class="token keyword">extends</span> <span class="token class-name">Girl</span> <span class="token punctuation">{</span>
- <span class="token function">teach</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> girl <span class="token operator">=</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'大脚'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">,</span>
- sex<span class="token operator">:</span> <span class="token string">'女'</span><span class="token punctuation">,</span>
- <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'欢迎光临'</span>
- <span class="token punctuation">}</span><span class="token punctuation">,</span>
- <span class="token function">teach</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'教'</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> <span class="token function-variable function">screenResume</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> bust<span class="token punctuation">,</span> sex <span class="token punctuation">}</span><span class="token operator">:</span> Girl<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> bust<span class="token punctuation">,</span> sex<span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> <span class="token function-variable function">getResume</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> bust<span class="token punctuation">,</span> teach<span class="token punctuation">}</span><span class="token operator">:</span> Teacher<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
- <span class="token function">teach</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> bust<span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token function">screenResume</span><span class="token punctuation">(</span>girl<span class="token punctuation">)</span>
- <span class="token function">getResume</span><span class="token punctuation">(</span>girl<span class="token punctuation">)</span>
- <span class="token comment">// 接口在class类中的使用</span>
- <span class="token keyword">class</span> <span class="token class-name">xiaojjj</span> <span class="token keyword">implements</span> <span class="token class-name">Girl</span> <span class="token punctuation">{</span>
- name <span class="token operator">=</span> <span class="token string">"xiaojj"</span>
- age <span class="token operator">=</span> <span class="token number">18</span>
- bust <span class="token operator">=</span> <span class="token number">98</span>
- sex <span class="token operator">=</span> <span class="token string">'女'</span>
- <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'欢迎光临'</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br></div></div><p><strong>可索引的类型</strong></p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name">Arr</span> <span class="token punctuation">{</span>
- <span class="token punctuation">[</span>index<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">]</span><span class="token operator">:</span><span class="token builtin">string</span> <span class="token comment">// 表示通过索引访问数据时返回的类型是string</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> myArr<span class="token operator">:</span>Arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'1'</span><span class="token punctuation">,</span><span class="token string">'2'</span><span class="token punctuation">,</span><span class="token string">'3'</span><span class="token punctuation">]</span>
- <span class="token keyword">const</span> myStr<span class="token operator">:</span><span class="token builtin">string</span> <span class="token operator">=</span> myArr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="es6-class类中应用ts"><a href="#es6-class类中应用ts" class="header-anchor">#</a> ES6 class类中应用TS</h2> <h3 id="class类的修饰器"><a href="#class类的修饰器" class="header-anchor">#</a> class类的修饰器</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 类的修饰器:</span>
- <span class="token comment">// public (公共) 允许内外部使用</span>
- <span class="token comment">// protected (受保护的) 允许在内部和继承内使用</span>
- <span class="token comment">// private (私有) 允许在内部使用,但不能在继承中使用</span>
- <span class="token comment">// 类的内部和外部,{}内属于内部,外面是外部</span>
- <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
- <span class="token comment">// public 内外都可以使用,可省略,不写时默认public。 protected只允许内部使用</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token comment">// 这里的string注释是TS使用的</span>
- <span class="token keyword">private</span> age<span class="token operator">:</span> <span class="token number">18</span>
- <span class="token keyword">public</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">+</span> <span class="token string">'say hello'</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">Teacher2</span> <span class="token keyword">extends</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
- <span class="token keyword">public</span> <span class="token function">sayBye</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">' say bye'</span><span class="token punctuation">)</span> <span class="token comment">// protected 可以在继承中使用到</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- person<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'test'</span> <span class="token comment">// 在类的外部定义</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h3 id="class类的构造函数中使用类型校验"><a href="#class类的构造函数中使用类型校验" class="header-anchor">#</a> class类的构造函数中使用类型校验</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">class</span> <span class="token class-name">Person2</span> <span class="token punctuation">{</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token keyword">public</span> name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">Teacher3</span> <span class="token keyword">extends</span> <span class="token class-name">Person2</span><span class="token punctuation">{</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token keyword">public</span> age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token string">'test-name'</span><span class="token punctuation">)</span> <span class="token comment">// 这里传的值是给Person2的构造函数。即使父类没有构造函数,子类的构造函数内也要写super()</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> teacher3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Teacher3</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>teacher3<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>teacher3<span class="token punctuation">.</span>age<span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="class类的getter、setter和static"><a href="#class类的getter、setter和static" class="header-anchor">#</a> class类的getter、setter和static</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">class</span> <span class="token class-name">Xjj</span> <span class="token punctuation">{</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token keyword">private</span> _age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
- <span class="token keyword">get</span> <span class="token function">age</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>_age <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span> <span class="token comment">// 访问器属性,以属性的形式访问age,并可以对属性进行包装</span>
- <span class="token keyword">set</span> <span class="token function">age</span><span class="token punctuation">(</span>age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>_age <span class="token operator">=</span> age<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> dj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Xjj</span><span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- dj<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">25</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>dj<span class="token punctuation">.</span>age<span class="token punctuation">)</span>
- <span class="token comment">// 静态属性 static。 静态属性指不需要通过实例化,直接通过Girl.prop的方式就可以访问到属性</span>
- <span class="token keyword">class</span> <span class="token class-name">Girl</span> <span class="token punctuation">{</span>
- <span class="token keyword">static</span> <span class="token function">sayLove</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'I love you'</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// const girl = new Girl()</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Girl<span class="token punctuation">.</span><span class="token function">sayLove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h3 id="class类的只读属性"><a href="#class类的只读属性" class="header-anchor">#</a> class类的只读属性</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 只读属性</span>
- <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
- <span class="token keyword">public</span> <span class="token keyword">readonly</span> _name<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token comment">// 只读属性</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span>name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>_name <span class="token operator">=</span> name<span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">'testName'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// person._name = '222'; // 不能修改只读属性</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>_name<span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="抽象类"><a href="#抽象类" class="header-anchor">#</a> 抽象类</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token operator">/</span> 抽象类
- <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Girls</span> <span class="token punctuation">{</span>
- <span class="token keyword">abstract</span> <span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 注意这里只是定义抽象方法,而不具有方法的实现</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">Waiter</span> <span class="token keyword">extends</span> <span class="token class-name">Girls</span><span class="token punctuation">{</span> <span class="token comment">// 继承了抽象类之后要 实现抽象类内的成员</span>
- <span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'大爷1'</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">BaseTeacher</span> <span class="token keyword">extends</span> <span class="token class-name">Girls</span><span class="token punctuation">{</span>
- <span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'大爷2'</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">SeniorTeacher</span> <span class="token keyword">extends</span> <span class="token class-name">Girls</span><span class="token punctuation">{</span>
- <span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'大爷3'</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// abstract关键字标注为抽象类(没有具体实现,不能用new实例化。子类继承该抽象类必须实现相应方法,用于规范子类)</span>
- <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Component<span class="token operator"><</span><span class="token constant">T1</span><span class="token punctuation">,</span> <span class="token constant">T2</span><span class="token operator">></span></span> <span class="token punctuation">{</span>
- props<span class="token operator">:</span> <span class="token constant">T1</span><span class="token punctuation">;</span>
- state<span class="token operator">:</span> <span class="token constant">T2</span><span class="token punctuation">;</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span>props<span class="token operator">:</span> <span class="token constant">T1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>props <span class="token operator">=</span> props
- <span class="token punctuation">}</span>
- <span class="token comment">// 用abstract标注为抽象类的方法,不能有具体实现</span>
- <span class="token keyword">abstract</span> <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 规范类的props和state</span>
- <span class="token keyword">interface</span> <span class="token class-name">Props</span> <span class="token punctuation">{</span>
- val<span class="token operator">:</span> <span class="token builtin">number</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">interface</span> <span class="token class-name">State</span> <span class="token punctuation">{</span>
- x<span class="token operator">:</span> <span class="token builtin">number</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 规范类内部的方法</span>
- <span class="token keyword">interface</span> <span class="token class-name">Log</span> <span class="token punctuation">{</span>
- <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">interface</span> <span class="token class-name">Save</span> <span class="token punctuation">{</span>
- <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// <Props, State>通过泛型传入类型. implements 关键字使当前类必须要履行 Log,Storage 接口内定于的契约</span>
- <span class="token keyword">class</span> <span class="token class-name">MyComponent</span> <span class="token keyword">extends</span> <span class="token class-name">Component<span class="token operator"><</span>Props<span class="token punctuation">,</span> State<span class="token operator">></span></span> <span class="token keyword">implements</span> <span class="token class-name">Log</span><span class="token punctuation">,</span> Save <span class="token punctuation">{</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span>props<span class="token operator">:</span> Props<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">super</span><span class="token punctuation">(</span>props<span class="token punctuation">)</span>
- <span class="token keyword">this</span><span class="token punctuation">.</span>state <span class="token operator">=</span> <span class="token punctuation">{</span>
- x<span class="token operator">:</span> <span class="token number">1</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// this.props.val</span>
- <span class="token comment">// this.state.x</span>
- <span class="token keyword">return</span> <span class="token string">'<MyComponent>'</span>
- <span class="token punctuation">}</span>
- <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">''</span>
- <span class="token punctuation">}</span>
- <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> myComponent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyComponent</span><span class="token punctuation">(</span><span class="token punctuation">{</span> val<span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
- myComponent<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token comment">/**
- * 类中的接口使用implements关键字
- * 1. 如果一个类implements了一个接口,那么就必须实现该接口中定义的契约
- * 2. 多个接口使用逗号分隔
- * 3. implements与extends可以同时存在
- */</span>
- <span class="token comment">/**
- * TS中类和接口知识点
- * 1. 抽象类在编译后会产生实体代码,接口不会
- * 2. TS只支持继承,即一个子类之能有一个父类
- * 3. 接口不能有实现,抽象类可以有属性的实现,没有方法实现
- */</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br></div></div><h2 id="联合类型和类型保护"><a href="#联合类型和类型保护" class="header-anchor">#</a> 联合类型和类型保护</h2> <p><strong>联合类型</strong> 指某个参数可以是多种类型。</p> <p><strong>类型保护</strong> 指参数属于某个类型才有相应的操作。</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name">Waiter</span> <span class="token punctuation">{</span>
- anjiao<span class="token operator">:</span> <span class="token builtin">boolean</span>
- <span class="token function-variable function">say</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">interface</span> <span class="token class-name">Teacher</span> <span class="token punctuation">{</span>
- anjiao<span class="token operator">:</span> <span class="token builtin">boolean</span>
- <span class="token function-variable function">skill</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">function</span> <span class="token function">judgeWho</span><span class="token punctuation">(</span>animal<span class="token operator">:</span> <span class="token punctuation">(</span>Waiter <span class="token operator">|</span> Teacher<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 联合类型</span>
- <span class="token comment">// 第一种断言方法</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span>animal<span class="token punctuation">.</span>anjiao<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// (animal as Teacher) 的意思是:断言 animal 是 Teacher类型</span>
- <span class="token punctuation">(</span>animal <span class="token keyword">as</span> Teacher<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- <span class="token punctuation">(</span>animal <span class="token keyword">as</span> Waiter<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 第二种断言方法</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'skill'</span> <span class="token keyword">in</span> animal<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- animal<span class="token punctuation">.</span><span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- animal<span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// 第三种类型保护方法是使用typeof来判断 (代码省略)</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">NumberObj</span> <span class="token punctuation">{</span>
- count<span class="token operator">:</span> <span class="token builtin">number</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">function</span> <span class="token function">addObj</span><span class="token punctuation">(</span>first<span class="token operator">:</span> object <span class="token operator">|</span> NumberObj<span class="token punctuation">,</span> second<span class="token operator">:</span> object <span class="token operator">|</span> NumberObj<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 联合类型</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span>first <span class="token keyword">instanceof</span> <span class="token class-name">NumberObj</span> <span class="token operator">&&</span> second <span class="token keyword">instanceof</span> <span class="token class-name">NumberObj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 类型保护</span>
- <span class="token keyword">return</span> first<span class="token punctuation">.</span>count <span class="token operator">+</span> second<span class="token punctuation">.</span>count<span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><h2 id="枚举"><a href="#枚举" class="header-anchor">#</a> 枚举</h2> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// Enum枚举类型 (个人理解枚举:约定一组可选的常量。 使用常量名表示某个值的含义,增强可读性。)</span>
- <span class="token comment">// js写法</span>
- <span class="token comment">// const Status = {</span>
- <span class="token comment">// MASSAGE: 0,</span>
- <span class="token comment">// SPA: 1,</span>
- <span class="token comment">// DABAOJIAN: 2</span>
- <span class="token comment">// }</span>
- <span class="token comment">// ts写法</span>
- <span class="token keyword">enum</span> Status <span class="token punctuation">{</span>
- <span class="token constant">MASSAGE</span><span class="token punctuation">,</span> <span class="token comment">// 如果想从1开始,给MASSAGE = 1, 即可</span>
- <span class="token constant">SPA</span><span class="token punctuation">,</span>
- <span class="token constant">DABAOJIAN</span>
- <span class="token punctuation">}</span> <span class="token comment">// 默认赋值 0 、1、2</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Status<span class="token punctuation">.</span><span class="token constant">MASSAGE</span><span class="token punctuation">,</span> Status<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 0, MASSAGE 可以通过下标反查</span>
- <span class="token keyword">function</span> <span class="token function">getStatus</span><span class="token punctuation">(</span>status<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span>status <span class="token operator">===</span> Status<span class="token punctuation">.</span><span class="token constant">MASSAGE</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'massage'</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>status <span class="token operator">===</span> Status<span class="token punctuation">.</span><span class="token constant">SPA</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'spa'</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>status <span class="token operator">===</span> Status<span class="token punctuation">.</span><span class="token constant">DABAOJIAN</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token string">'dabaojian'</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token function">getStatus</span><span class="token punctuation">(</span>Status<span class="token punctuation">.</span><span class="token constant">SPA</span><span class="token punctuation">)</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h2 id="泛型"><a href="#泛型" class="header-anchor">#</a> 泛型</h2> <p>泛型,最简单的理解:泛指的类型。(类似函数中的形参与实参)</p> <h3 id="函数中的泛型使用"><a href="#函数中的泛型使用" class="header-anchor">#</a> 函数中的泛型使用</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code>
- <span class="token comment">// function join(first: string | number, second: string | number) {</span>
- <span class="token comment">// return `${first}${second}`</span>
- <span class="token comment">// }</span>
- <span class="token comment">// join('jspang', 1); // 如果我想第一个参数是字符串,第二个也必须是字符串,这么就用到泛型</span>
- <span class="token comment">// 泛型使用,如同定义形参,在调用时指定类型</span>
- <span class="token keyword">function</span> <span class="token generic-function"><span class="token function">join</span><span class="token generic class-name"><span class="token operator"><</span>JSPang<span class="token operator">></span></span></span><span class="token punctuation">(</span>first<span class="token operator">:</span> JSPang<span class="token punctuation">,</span> second<span class="token operator">:</span> JSPang<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>first<span class="token interpolation-punctuation punctuation">}</span></span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>second<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span>
- <span class="token punctuation">}</span>
- <span class="token generic-function"><span class="token function">join</span><span class="token generic class-name"><span class="token operator"><</span><span class="token builtin">string</span><span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token string">'jspang'</span><span class="token punctuation">,</span> <span class="token string">'123'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token generic-function"><span class="token function">join</span><span class="token generic class-name"><span class="token operator"><</span><span class="token builtin">number</span><span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// 泛型中数组的使用</span>
- <span class="token keyword">function</span> <span class="token generic-function"><span class="token function">myFun</span><span class="token generic class-name"><span class="token operator"><</span><span class="token constant">ANY</span><span class="token operator">></span></span></span><span class="token punctuation">(</span>params<span class="token operator">:</span><span class="token constant">ANY</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// ANY[] or Array<ANY></span>
- <span class="token keyword">return</span> params
- <span class="token punctuation">}</span>
- <span class="token generic-function"><span class="token function">myFun</span><span class="token generic class-name"><span class="token operator"><</span><span class="token builtin">string</span><span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
- <span class="token comment">// 两个类型参数的使用(工作中,常用T表示泛型)</span>
- <span class="token keyword">function</span> <span class="token generic-function"><span class="token function">join2</span><span class="token generic class-name"><span class="token operator"><</span><span class="token constant">T</span><span class="token punctuation">,</span><span class="token constant">P</span><span class="token operator">></span></span></span><span class="token punctuation">(</span>first<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">,</span> second<span class="token operator">:</span> <span class="token constant">P</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>first<span class="token interpolation-punctuation punctuation">}</span></span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>second<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span>
- <span class="token punctuation">}</span>
- <span class="token generic-function"><span class="token function">join2</span><span class="token generic class-name"><span class="token operator"><</span><span class="token builtin">string</span><span class="token punctuation">,</span><span class="token builtin">number</span><span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token string">'jspang'</span><span class="token punctuation">,</span> <span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token generic-function"><span class="token function">join2</span><span class="token generic class-name"><span class="token operator"><</span><span class="token builtin">number</span><span class="token punctuation">,</span> <span class="token builtin">string</span><span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token string">'22'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token function">join2</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token string">'22'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 泛型也支持类型推断 (鼠标移到函数名有提示)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h3 id="class类中使用泛型"><a href="#class类中使用泛型" class="header-anchor">#</a> class类中使用泛型</h3> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// class SelectGirl {</span>
- <span class="token comment">// constructor(private girls: string[] | number[]) { } // private 私有的参数,外部无法修改</span>
- <span class="token comment">// getGirl(index: number): string | number {</span>
- <span class="token comment">// return this.girls[index]</span>
- <span class="token comment">// }</span>
- <span class="token comment">// }</span>
- <span class="token comment">// 使用泛型</span>
- <span class="token keyword">class</span> <span class="token class-name">SelectGirl<span class="token operator"><</span><span class="token constant">T</span><span class="token operator">></span></span> <span class="token punctuation">{</span> <span class="token comment">// 泛型的约束: <T extends number | string></span>
- <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token keyword">private</span> girls<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment">// private 私有的参数,外部无法修改</span>
- <span class="token function">getGirl</span><span class="token punctuation">(</span>index<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token constant">T</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>girls<span class="token punctuation">[</span>index<span class="token punctuation">]</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// const selectGirl = new SelectGirl<string>(['大脚', 'xiaohong', 'xiaobai'])</span>
- <span class="token keyword">const</span> selectGirl <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SelectGirl<span class="token operator"><</span><span class="token builtin">number</span><span class="token operator">></span></span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">101</span><span class="token punctuation">,</span> <span class="token number">102</span><span class="token punctuation">,</span> <span class="token number">103</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>selectGirl<span class="token punctuation">.</span><span class="token function">getGirl</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
- <span class="token comment">// 泛型中的继承</span>
- <span class="token keyword">interface</span> <span class="token class-name">Girl</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">class</span> <span class="token class-name">SelectGirl2<span class="token operator"><</span><span class="token constant">T</span> <span class="token keyword">extends</span> Girl<span class="token operator">></span></span> <span class="token punctuation">{</span> <span class="token comment">// 泛型T中必须有一个name属性,继承自Girl接口</span>
- <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token keyword">private</span> girls<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment">// private 私有的参数,外部无法修改</span>
- <span class="token function">getGirl</span><span class="token punctuation">(</span>index<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>girls<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">.</span>name
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">const</span> selectGirl2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SelectGirl2</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
- <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">'大脚1'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
- <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">'大脚2'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
- <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">'大脚3'</span><span class="token punctuation">}</span>
- <span class="token punctuation">]</span><span class="token punctuation">)</span>
- <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>selectGirl2<span class="token punctuation">.</span><span class="token function">getGirl</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h2 id="类型保护"><a href="#类型保护" class="header-anchor">#</a> 类型保护</h2> <p>我们通常在 JavaScript 中通过判断来处理⼀些逻辑,在 TypeScript 中这种条件语句块还有另外⼀</p> <p>个特性:根据判断逻辑的结果,缩⼩类型范围(有点类似断⾔),这种特性称为 类型保护 ,触发条</p> <p>件:</p> <ul><li><p>逻辑条件语句块:if、else、elseif</p></li> <li><p>特定的⼀些关键字:typeof、instanceof、in</p></li></ul> <h4 id="typeof"><a href="#typeof" class="header-anchor">#</a> typeof</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span>a<span class="token operator">:</span> <span class="token builtin">string</span><span class="token operator">|</span><span class="token builtin">number</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// error,不能保证 a 就是字符串</span>
- a<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> a <span class="token operator">===</span> <span class="token string">'string'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// ok</span>
- a<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- <span class="token comment">// ok</span>
- a<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="instanceof"><a href="#instanceof" class="header-anchor">#</a> instanceof</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Date<span class="token operator">|</span><span class="token builtin">Array</span><span class="token operator"><</span><span class="token builtin">any</span><span class="token operator">></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span>a <span class="token keyword">instanceof</span> <span class="token class-name"><span class="token builtin">Array</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- a<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- a<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="in"><a href="#in" class="header-anchor">#</a> in</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name"><span class="token constant">IA</span></span> <span class="token punctuation">{</span>
- x<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- y<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- <span class="token keyword">interface</span> <span class="token class-name"><span class="token constant">IB</span></span> <span class="token punctuation">{</span>
- a<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- b<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- <span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span>arg<span class="token operator">:</span> <span class="token constant">IA</span> <span class="token operator">|</span> <span class="token constant">IB</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'x'</span> <span class="token keyword">in</span> arg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// ok</span>
- arg<span class="token punctuation">.</span>x<span class="token punctuation">;</span>
- <span class="token comment">// error</span>
- arg<span class="token punctuation">.</span>a<span class="token punctuation">;</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- <span class="token comment">// ok</span>
- arg<span class="token punctuation">.</span>a<span class="token punctuation">;</span>
- <span class="token comment">// error</span>
- 字⾯量类型保护
- 如果类型为字⾯量类型,那么还可以通过该字⾯量类型的字⾯值进⾏推断
- ⾃定义类型保护
- 有的时候,以上的⼀些⽅式并不能满⾜⼀些特殊情况,则可以⾃定义类型保护规则
- arg<span class="token punctuation">.</span>x<span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h4 id="字面量类型保护"><a href="#字面量类型保护" class="header-anchor">#</a> 字面量类型保护</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name"><span class="token constant">IA</span></span> <span class="token punctuation">{</span>
- type<span class="token operator">:</span> <span class="token string">'IA'</span><span class="token punctuation">;</span>
- x<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- y<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">interface</span> <span class="token class-name"><span class="token constant">IB</span></span> <span class="token punctuation">{</span>
- type<span class="token operator">:</span> <span class="token string">'IB'</span><span class="token punctuation">;</span>
- a<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- b<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span>arg<span class="token operator">:</span> <span class="token constant">IA</span> <span class="token operator">|</span> <span class="token constant">IB</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span>arg<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">'IA'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token comment">// ok</span>
- arg<span class="token punctuation">.</span>x<span class="token punctuation">;</span>
- <span class="token comment">// error</span>
- arg<span class="token punctuation">.</span>a<span class="token punctuation">;</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- <span class="token comment">// ok</span>
- arg<span class="token punctuation">.</span>a<span class="token punctuation">;</span>
- <span class="token comment">// error</span>
- arg<span class="token punctuation">.</span>x<span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h4 id="自定义类型保护"><a href="#自定义类型保护" class="header-anchor">#</a> 自定义类型保护</h4> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">function</span> <span class="token function">canEach</span><span class="token punctuation">(</span>data<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span><span class="token operator">:</span> data <span class="token keyword">is</span> Element<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">|</span>NodeList <span class="token punctuation">{</span>
- <span class="token keyword">return</span> data<span class="token punctuation">.</span>forEach <span class="token operator">!==</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">function</span> <span class="token function">fn2</span><span class="token punctuation">(</span>elements<span class="token operator">:</span> Element<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">|</span>NodeList<span class="token operator">|</span>Element<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">canEach</span><span class="token punctuation">(</span>elements<span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
- elements<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span>el<span class="token operator">:</span> Element<span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span>
- el<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">'box'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
- elements<span class="token punctuation">.</span>classList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">'box'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><code>data is Element[]|NodeList</code> 是⼀种类型谓词,格式为: <code>xx is XX</code> ,返回这种类型的函数就可以</p> <p>被 TypeScript 识别为类型保护</p> <h2 id="类型操作"><a href="#类型操作" class="header-anchor">#</a> 类型操作</h2> <h4 id="typeof-2"><a href="#typeof-2" class="header-anchor">#</a> typeof</h4> <p>获取数据的类型</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">'kkk'</span>
- <span class="token keyword">let</span> t <span class="token operator">=</span> <span class="token keyword">typeof</span> str <span class="token comment">// 使用typeof获取str的类型,返回string给变量t (原生js的typeof使用)</span>
- <span class="token keyword">type</span> <span class="token class-name">myType</span> <span class="token operator">=</span> <span class="token keyword">typeof</span> str <span class="token comment">// 使用typeof获取str的类型,返回string给类型myType (TS的typeof使用)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="keyof"><a href="#keyof" class="header-anchor">#</a> keyof</h4> <p>获取类型的所有<code>key</code>的集合</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 使用示例1</span>
- <span class="token keyword">interface</span> <span class="token class-name">Person</span><span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">type</span> <span class="token class-name">PersonKeys</span> <span class="token operator">=</span> <span class="token keyword">keyof</span> Person
- <span class="token comment">// 等同: type PersonKeys = 'name' | 'age'; // 注意是key的集合,不是值的集合</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token comment">// 使用示例2</span>
- <span class="token keyword">let</span> p1 <span class="token operator">=</span> <span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token string">'xx'</span><span class="token punctuation">,</span>
- age<span class="token operator">:</span> <span class="token number">28</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// keyof typeof p1 的作用,首先typeof将p1对象的类型取出,keyof再将类型的key集合取出,结果:'name' | 'age'</span>
- <span class="token keyword">function</span> <span class="token function">getPersonVal</span><span class="token punctuation">(</span>k<span class="token operator">:</span> <span class="token keyword">keyof</span> <span class="token keyword">typeof</span> p1<span class="token punctuation">)</span><span class="token punctuation">{</span>
- <span class="token keyword">return</span> p1<span class="token punctuation">[</span>k<span class="token punctuation">]</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="in-2"><a href="#in-2" class="header-anchor">#</a> in</h4> <p>内部使用<code>for...in</code>对类型进行遍历</p> <div class="language-typescript line-numbers-mode"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name">Person</span><span class="token punctuation">{</span>
- name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
- age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">type</span> <span class="token class-name">PersonKeys</span> <span class="token operator">=</span> <span class="token keyword">keyof</span> Person<span class="token punctuation">;</span> <span class="token comment">// 'name' | 'age'</span>
- <span class="token keyword">type</span> <span class="token class-name">NewPerson</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
- <span class="token punctuation">[</span>k <span class="token keyword">in</span> PersonKeys<span class="token punctuation">]</span><span class="token operator">:</span> <span class="token builtin">string</span>
- <span class="token comment">// 也可写成 [k in 'name' | 'age']: string </span>
- <span class="token comment">// 或 [k in keyof Person]: string</span>
- <span class="token punctuation">}</span> <span class="token comment">// {name: string; age: string;}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="配置文件tsconfig-json"><a href="#配置文件tsconfig-json" class="header-anchor">#</a> 配置文件tsconfig.json</h2> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token comment">// 此文件由命令 tsc -init 生成</span>
- <span class="token comment">// 直接运行 tsc 命令就会运用此配置文件</span>
- <span class="token comment">// 选项详解:https://www.tslang.cn/docs/handbook/compiler-options.html</span>
- <span class="token punctuation">{</span>
- <span class="token comment">// "include": ["demo15-1.ts"], // 要编译的指定文件,不配置此项时运行tsc默认编译全部</span>
- <span class="token comment">// "files": ["demo15-1.ts"], // 和include类似</span>
- <span class="token comment">// "exclude": ["demo15-3.ts"], // 要排除编译的指定文件</span>
- <span class="token property">"compilerOptions"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">// 编译选项</span>
- <span class="token comment">/* 基本选项 */</span>
- <span class="token comment">// "incremental": true, /* 启用增量编译 */</span>
- <span class="token property">"target"</span><span class="token operator">:</span> <span class="token string">"es5"</span><span class="token punctuation">,</span> <span class="token comment">/* 指定 ECMAScript 目标版本: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */</span>
- <span class="token property">"module"</span><span class="token operator">:</span> <span class="token string">"commonjs"</span><span class="token punctuation">,</span> <span class="token comment">/* 指定模块代码生成: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */</span>
- <span class="token comment">// "lib": [], /* 指定要包含在编译中的库文件. */</span>
- <span class="token comment">// "allowJs": true, /* 允许编译javascript文件*/</span>
- <span class="token comment">// "checkJs": true, /* 报告.js文件中的错误。 */</span>
- <span class="token comment">// "jsx": "preserve", /* 指定 JSX代码生成: 'preserve', 'react-native', or 'react'. */</span>
- <span class="token comment">// "declaration": true, /* 生成相应的 .d.ts文件 */</span>
- <span class="token comment">// "declarationMap": true, /* 为每个相应的 .d.ts文件生成一个sourcemap (编译后代码对源码的映射)*/</span>
- <span class="token property">"sourceMap"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">/* 生成源文件与输出文件的映射关系文件(.map)。*/</span>
- <span class="token comment">// "outFile": "./", /* 将输出文件合并为一个文件. */</span>
- <span class="token property">"outDir"</span><span class="token operator">:</span> <span class="token string">"./build"</span><span class="token punctuation">,</span> <span class="token comment">/* 输出的js文件目录。 */</span>
- <span class="token property">"rootDir"</span><span class="token operator">:</span> <span class="token string">"./src"</span><span class="token punctuation">,</span> <span class="token comment">/* ts源文件目录。 */</span>
- <span class="token comment">// "composite": true, /* 启用 项目编译 */</span>
- <span class="token comment">// "tsBuildInfoFile": "./", /* 指定用于存储增量编译信息的文件 */</span>
- <span class="token comment">// "removeComments": true, /* 不输出注释到编译结果.(删除所有注释,除了以 /!*开头的版权信息。) */</span>
- <span class="token comment">// "noEmit": true, /* 不发出输出.Do not emit outputs. */</span>
- <span class="token comment">// "importHelpers": true, /* 从“tslib”导入发出助手. Import emit helpers from 'tslib'. */</span>
- <span class="token comment">// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */</span>
- <span class="token comment">// "isolatedModules": true, /* 将每个文件作为单独的模块(与“ts.transpileModule”类似) */</span>
- <span class="token comment">/* 严格的类型检查选项 */</span>
- <span class="token property">"strict"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">/* 启用所有严格类型检查选项。 打开此选项后,下面这些选项就不需要单独设置*/</span>
- <span class="token comment">// "noImplicitAny": true, /* 对隐含的“any”类型的表达式和声明引发错误. (为false时允许any不用特意声明)*/</span>
- <span class="token comment">// "strictNullChecks": true, /* 启用严格的null检查. (为false时允许赋值为null)*/</span>
- <span class="token comment">// "strictFunctionTypes": true, /* 启用严格检查函数类型. */</span>
- <span class="token comment">// "strictBindCallApply": true, /* 启用函数上严格的“bind”、“call”和“apply”方法. */</span>
- <span class="token comment">// "strictPropertyInitialization": true, /* 启用 严格检查类中的属性初始化. */</span>
- <span class="token comment">// "noImplicitThis": true, /* 在隐含的“any”类型的“this”表达式上引发错误。 */</span>
- <span class="token comment">// "alwaysStrict": true, /* 以严格模式解析并为每个源文件发出“use strict”。 */</span>
- <span class="token comment">/* 附加检查。Additional Checks */</span>
- <span class="token comment">// "noUnusedLocals": true, /* 若有未使用的局部变量则抛错 */</span>
- <span class="token comment">// "noUnusedParameters": true, /* 若有未使用的参数则抛错。 */</span>
- <span class="token comment">// "noImplicitReturns": true, /* 不是函数的所有返回路径都有返回值时报错。*/</span>
- <span class="token comment">// "noFallthroughCasesInSwitch": true, /* 报告switch语句的fallthrough错误。(即,不允许switch的case语句贯穿) */</span>
- <span class="token comment">/* 模块解析选项 */</span>
- <span class="token comment">// "moduleResolution": "node", /* 决定如何处理模块:'node' (Node.js) or 'classic' (TypeScript pre-1.6). */</span>
- <span class="token comment">// "baseUrl": "./", /* 用于解析非绝对模块名称的基础目录。 */</span>
- <span class="token comment">// "paths": {}, /* 模块名到基于 baseUrl的路径映射的列表。 */</span>
- <span class="token comment">// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */</span>
- <span class="token comment">// "typeRoots": [], /* List of folders to include type definitions from. */</span>
- <span class="token comment">// "types": [], /* 要包含的类型声明文件名列表 */</span>
- <span class="token comment">// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */</span>
- <span class="token property">"esModuleInterop"</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token comment">/* 启用s emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */</span>
- <span class="token comment">// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */</span>
- <span class="token comment">// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */</span>
- <span class="token comment">/* Source Map Options */</span>
- <span class="token comment">// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */</span>
- <span class="token comment">// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */</span>
- <span class="token comment">// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */</span>
- <span class="token comment">// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */</span>
- <span class="token comment">/* Experimental Options */</span>
- <span class="token comment">// "experimentalDecorators": true, /* 启用s experimental support for ES7 decorators. */</span>
- <span class="token comment">// "emitDecoratorMetadata": true, /* 启用s experimental support for emitting type metadata for decorators. */</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br></div></div></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/heBody/blog/edit/master/docs/01.前端/40.学习笔记/35.TypeScript笔记.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=TypeScript" title="标签">#TypeScript</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/12/14, 19:36:42</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/note/git/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">《Git》学习笔记</div></a> <a href="/blog/note/wx-miniprogram/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">小程序笔记</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
- ←
- <a href="/blog/note/git/" class="prev">《Git》学习笔记</a></span> <span class="next"><a href="/blog/note/wx-miniprogram/">小程序笔记</a>→
- </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/922650/"><div>
- Git修改分支名
- <!----></div></a> <span class="date">08-11</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/55f894/"><div>
- CSS给table的tbody添加滚动条
- <!----></div></a> <span class="date">06-29</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/829589/"><div>
- 我做了一个手写春联小网页,祝大家虎年暴富
- <span class="title-tag">
- 原创
- </span></div></a> <span class="date">01-28</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章></a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:30363811@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/heBody" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
- Copyright © 2016-2022
- <span>Hesb | <a href="https://github.com/heBody/blob" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
- 跟随系统
- </li><li class="iconfont icon-rijianmoshi">
- 浅色模式
- </li><li class="iconfont icon-yejianmoshi">
- 深色模式
- </li><li class="iconfont icon-yuedu">
- 阅读模式
- </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
- <script src="/blog/assets/js/app.28edfb28.js" defer></script><script src="/blog/assets/js/2.395c0d18.js" defer></script><script src="/blog/assets/js/3.6748bd5c.js" defer></script><script src="/blog/assets/js/31.78e43a68.js" defer></script>
- </body>
- </html>
|