123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804 |
- <!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.cf6c3304.css" as="style"><link rel="preload" href="/blog/assets/js/app.b419507c.js" as="script"><link rel="preload" href="/blog/assets/js/2.602aac03.js" as="script"><link rel="preload" href="/blog/assets/js/30.0ac25def.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.05bc42d6.js"><link rel="prefetch" href="/blog/assets/js/100.93782085.js"><link rel="prefetch" href="/blog/assets/js/101.49f33592.js"><link rel="prefetch" href="/blog/assets/js/102.b391aaca.js"><link rel="prefetch" href="/blog/assets/js/103.b5c297df.js"><link rel="prefetch" href="/blog/assets/js/104.1b8956b0.js"><link rel="prefetch" href="/blog/assets/js/105.4af5ed2a.js"><link rel="prefetch" href="/blog/assets/js/106.f5ff0141.js"><link rel="prefetch" href="/blog/assets/js/107.9b0935ed.js"><link rel="prefetch" href="/blog/assets/js/108.a3b3e687.js"><link rel="prefetch" href="/blog/assets/js/109.0faeea62.js"><link rel="prefetch" href="/blog/assets/js/11.803b5050.js"><link rel="prefetch" href="/blog/assets/js/110.05696e77.js"><link rel="prefetch" href="/blog/assets/js/111.33bbfeaf.js"><link rel="prefetch" href="/blog/assets/js/112.cc349461.js"><link rel="prefetch" href="/blog/assets/js/113.ee6a59a5.js"><link rel="prefetch" href="/blog/assets/js/114.837d6a71.js"><link rel="prefetch" href="/blog/assets/js/115.035a54c0.js"><link rel="prefetch" href="/blog/assets/js/116.bffbfdca.js"><link rel="prefetch" href="/blog/assets/js/117.effbf98c.js"><link rel="prefetch" href="/blog/assets/js/118.a2ce28a1.js"><link rel="prefetch" href="/blog/assets/js/119.da6e315d.js"><link rel="prefetch" href="/blog/assets/js/12.68f9aa15.js"><link rel="prefetch" href="/blog/assets/js/120.edc4f23e.js"><link rel="prefetch" href="/blog/assets/js/121.67388947.js"><link rel="prefetch" href="/blog/assets/js/122.0e892f37.js"><link rel="prefetch" href="/blog/assets/js/123.9c12e399.js"><link rel="prefetch" href="/blog/assets/js/124.e265d82a.js"><link rel="prefetch" href="/blog/assets/js/125.07ad81ea.js"><link rel="prefetch" href="/blog/assets/js/126.30be0256.js"><link rel="prefetch" href="/blog/assets/js/127.daf6f659.js"><link rel="prefetch" href="/blog/assets/js/128.c3aa5522.js"><link rel="prefetch" href="/blog/assets/js/129.60c2ac70.js"><link rel="prefetch" href="/blog/assets/js/13.b2f4726e.js"><link rel="prefetch" href="/blog/assets/js/130.283b36c2.js"><link rel="prefetch" href="/blog/assets/js/131.36fd29f9.js"><link rel="prefetch" href="/blog/assets/js/132.aa486fed.js"><link rel="prefetch" href="/blog/assets/js/133.5244b658.js"><link rel="prefetch" href="/blog/assets/js/134.5d546ce7.js"><link rel="prefetch" href="/blog/assets/js/135.efe5189e.js"><link rel="prefetch" href="/blog/assets/js/136.0e15e278.js"><link rel="prefetch" href="/blog/assets/js/137.9ed0177b.js"><link rel="prefetch" href="/blog/assets/js/138.0b8bc204.js"><link rel="prefetch" href="/blog/assets/js/139.cc005358.js"><link rel="prefetch" href="/blog/assets/js/14.8dc92978.js"><link rel="prefetch" href="/blog/assets/js/140.bc32a870.js"><link rel="prefetch" href="/blog/assets/js/141.acfd1af2.js"><link rel="prefetch" href="/blog/assets/js/142.4acb4dd6.js"><link rel="prefetch" href="/blog/assets/js/143.8182807e.js"><link rel="prefetch" href="/blog/assets/js/144.20a6c974.js"><link rel="prefetch" href="/blog/assets/js/145.f3a9911c.js"><link rel="prefetch" href="/blog/assets/js/146.3ed86675.js"><link rel="prefetch" href="/blog/assets/js/147.fa9b3898.js"><link rel="prefetch" href="/blog/assets/js/148.f498d3e2.js"><link rel="prefetch" href="/blog/assets/js/149.3fe441c6.js"><link rel="prefetch" href="/blog/assets/js/15.c32eb5e6.js"><link rel="prefetch" href="/blog/assets/js/150.6d73379d.js"><link rel="prefetch" href="/blog/assets/js/151.4916058e.js"><link rel="prefetch" href="/blog/assets/js/152.42f788ce.js"><link rel="prefetch" href="/blog/assets/js/153.57ac80e4.js"><link rel="prefetch" href="/blog/assets/js/154.46b3a2b3.js"><link rel="prefetch" href="/blog/assets/js/155.f16ed794.js"><link rel="prefetch" href="/blog/assets/js/156.7004ac8c.js"><link rel="prefetch" href="/blog/assets/js/157.54029a82.js"><link rel="prefetch" href="/blog/assets/js/158.d1a36e56.js"><link rel="prefetch" href="/blog/assets/js/159.e3aba89f.js"><link rel="prefetch" href="/blog/assets/js/16.ce96f674.js"><link rel="prefetch" href="/blog/assets/js/160.da511416.js"><link rel="prefetch" href="/blog/assets/js/161.e4fc79bf.js"><link rel="prefetch" href="/blog/assets/js/162.f214074e.js"><link rel="prefetch" href="/blog/assets/js/163.4bf1ce2d.js"><link rel="prefetch" href="/blog/assets/js/164.50c2a26a.js"><link rel="prefetch" href="/blog/assets/js/165.3fadd30d.js"><link rel="prefetch" href="/blog/assets/js/166.b46fceba.js"><link rel="prefetch" href="/blog/assets/js/167.6c6747d8.js"><link rel="prefetch" href="/blog/assets/js/168.87ccce63.js"><link rel="prefetch" href="/blog/assets/js/169.e1e1c58b.js"><link rel="prefetch" href="/blog/assets/js/17.a693fe7f.js"><link rel="prefetch" href="/blog/assets/js/170.755abdff.js"><link rel="prefetch" href="/blog/assets/js/171.a6b69067.js"><link rel="prefetch" href="/blog/assets/js/172.52c10d67.js"><link rel="prefetch" href="/blog/assets/js/173.568ec26d.js"><link rel="prefetch" href="/blog/assets/js/174.f6ff5c51.js"><link rel="prefetch" href="/blog/assets/js/175.f2d646a9.js"><link rel="prefetch" href="/blog/assets/js/176.2fd93f9c.js"><link rel="prefetch" href="/blog/assets/js/177.fc5683f6.js"><link rel="prefetch" href="/blog/assets/js/178.432e7e1f.js"><link rel="prefetch" href="/blog/assets/js/179.e63b01b0.js"><link rel="prefetch" href="/blog/assets/js/18.6df3c873.js"><link rel="prefetch" href="/blog/assets/js/180.7a136172.js"><link rel="prefetch" href="/blog/assets/js/181.6e78e56b.js"><link rel="prefetch" href="/blog/assets/js/182.73d2f94a.js"><link rel="prefetch" href="/blog/assets/js/183.dce8f42f.js"><link rel="prefetch" href="/blog/assets/js/184.94ec7685.js"><link rel="prefetch" href="/blog/assets/js/185.c3561080.js"><link rel="prefetch" href="/blog/assets/js/186.63b6c4b4.js"><link rel="prefetch" href="/blog/assets/js/187.b1ca4046.js"><link rel="prefetch" href="/blog/assets/js/188.6525cd10.js"><link rel="prefetch" href="/blog/assets/js/189.fdd57a16.js"><link rel="prefetch" href="/blog/assets/js/19.6ffd68ce.js"><link rel="prefetch" href="/blog/assets/js/190.8d9ac4f5.js"><link rel="prefetch" href="/blog/assets/js/191.f19beada.js"><link rel="prefetch" href="/blog/assets/js/192.bcd02302.js"><link rel="prefetch" href="/blog/assets/js/193.05afb721.js"><link rel="prefetch" href="/blog/assets/js/194.698e74d2.js"><link rel="prefetch" href="/blog/assets/js/195.70ee23b9.js"><link rel="prefetch" href="/blog/assets/js/196.14c12f32.js"><link rel="prefetch" href="/blog/assets/js/197.52e07cb7.js"><link rel="prefetch" href="/blog/assets/js/198.6312a976.js"><link rel="prefetch" href="/blog/assets/js/199.600b8211.js"><link rel="prefetch" href="/blog/assets/js/20.d994b814.js"><link rel="prefetch" href="/blog/assets/js/200.1658389e.js"><link rel="prefetch" href="/blog/assets/js/201.f2ecc5d4.js"><link rel="prefetch" href="/blog/assets/js/202.05a5bb1c.js"><link rel="prefetch" href="/blog/assets/js/203.893b83ad.js"><link rel="prefetch" href="/blog/assets/js/204.a90c3691.js"><link rel="prefetch" href="/blog/assets/js/205.d82fb971.js"><link rel="prefetch" href="/blog/assets/js/206.00e0b021.js"><link rel="prefetch" href="/blog/assets/js/207.a5dbb279.js"><link rel="prefetch" href="/blog/assets/js/208.d15bb006.js"><link rel="prefetch" href="/blog/assets/js/209.00d63e46.js"><link rel="prefetch" href="/blog/assets/js/21.2587b99c.js"><link rel="prefetch" href="/blog/assets/js/210.b256d7b8.js"><link rel="prefetch" href="/blog/assets/js/211.be264f59.js"><link rel="prefetch" href="/blog/assets/js/212.9956352d.js"><link rel="prefetch" href="/blog/assets/js/213.6144a981.js"><link rel="prefetch" href="/blog/assets/js/214.926fcb0b.js"><link rel="prefetch" href="/blog/assets/js/215.19148b26.js"><link rel="prefetch" href="/blog/assets/js/216.f56e94aa.js"><link rel="prefetch" href="/blog/assets/js/217.c5a75265.js"><link rel="prefetch" href="/blog/assets/js/218.1d08fa09.js"><link rel="prefetch" href="/blog/assets/js/219.11d14310.js"><link rel="prefetch" href="/blog/assets/js/22.cbb873e2.js"><link rel="prefetch" href="/blog/assets/js/220.31c10a26.js"><link rel="prefetch" href="/blog/assets/js/221.b95f8a68.js"><link rel="prefetch" href="/blog/assets/js/222.79b831b9.js"><link rel="prefetch" href="/blog/assets/js/223.23d684a8.js"><link rel="prefetch" href="/blog/assets/js/224.86b7a46d.js"><link rel="prefetch" href="/blog/assets/js/225.ba05abd3.js"><link rel="prefetch" href="/blog/assets/js/226.759b748e.js"><link rel="prefetch" href="/blog/assets/js/227.c26a7240.js"><link rel="prefetch" href="/blog/assets/js/228.de29829d.js"><link rel="prefetch" href="/blog/assets/js/229.83cf7341.js"><link rel="prefetch" href="/blog/assets/js/23.3392ec01.js"><link rel="prefetch" href="/blog/assets/js/230.d239ed00.js"><link rel="prefetch" href="/blog/assets/js/231.ddfd0758.js"><link rel="prefetch" href="/blog/assets/js/232.6b57acfb.js"><link rel="prefetch" href="/blog/assets/js/233.a104a750.js"><link rel="prefetch" href="/blog/assets/js/234.3d041b0e.js"><link rel="prefetch" href="/blog/assets/js/235.9e19c5d0.js"><link rel="prefetch" href="/blog/assets/js/236.9f300061.js"><link rel="prefetch" href="/blog/assets/js/237.9fb8d4bd.js"><link rel="prefetch" href="/blog/assets/js/238.ceec05fc.js"><link rel="prefetch" href="/blog/assets/js/24.736495d8.js"><link rel="prefetch" href="/blog/assets/js/25.8f5cf322.js"><link rel="prefetch" href="/blog/assets/js/26.67d8ecd5.js"><link rel="prefetch" href="/blog/assets/js/27.05800199.js"><link rel="prefetch" href="/blog/assets/js/28.72a4cebd.js"><link rel="prefetch" href="/blog/assets/js/29.f8d2f7c1.js"><link rel="prefetch" href="/blog/assets/js/3.aec45124.js"><link rel="prefetch" href="/blog/assets/js/31.4d385616.js"><link rel="prefetch" href="/blog/assets/js/32.ab6922e1.js"><link rel="prefetch" href="/blog/assets/js/33.22aedb73.js"><link rel="prefetch" href="/blog/assets/js/34.1176b400.js"><link rel="prefetch" href="/blog/assets/js/35.d4d05428.js"><link rel="prefetch" href="/blog/assets/js/36.6dd0dde1.js"><link rel="prefetch" href="/blog/assets/js/37.4af0c50a.js"><link rel="prefetch" href="/blog/assets/js/38.1a98317a.js"><link rel="prefetch" href="/blog/assets/js/39.8383d231.js"><link rel="prefetch" href="/blog/assets/js/4.6a499ed7.js"><link rel="prefetch" href="/blog/assets/js/40.ea1ea46b.js"><link rel="prefetch" href="/blog/assets/js/41.29846640.js"><link rel="prefetch" href="/blog/assets/js/42.9c906181.js"><link rel="prefetch" href="/blog/assets/js/43.344bd56a.js"><link rel="prefetch" href="/blog/assets/js/44.e23af589.js"><link rel="prefetch" href="/blog/assets/js/45.fa09e088.js"><link rel="prefetch" href="/blog/assets/js/46.ddf18f6d.js"><link rel="prefetch" href="/blog/assets/js/47.593a3a96.js"><link rel="prefetch" href="/blog/assets/js/48.fb21c024.js"><link rel="prefetch" href="/blog/assets/js/49.5bb86e0c.js"><link rel="prefetch" href="/blog/assets/js/5.48f0d794.js"><link rel="prefetch" href="/blog/assets/js/50.0fb55bed.js"><link rel="prefetch" href="/blog/assets/js/51.a38b4abd.js"><link rel="prefetch" href="/blog/assets/js/52.70f6922c.js"><link rel="prefetch" href="/blog/assets/js/53.c7e04159.js"><link rel="prefetch" href="/blog/assets/js/54.ff09ad12.js"><link rel="prefetch" href="/blog/assets/js/55.57d17d47.js"><link rel="prefetch" href="/blog/assets/js/56.a58b3cc3.js"><link rel="prefetch" href="/blog/assets/js/57.9de5f95b.js"><link rel="prefetch" href="/blog/assets/js/58.d15857bf.js"><link rel="prefetch" href="/blog/assets/js/59.ff3f0900.js"><link rel="prefetch" href="/blog/assets/js/6.1505523f.js"><link rel="prefetch" href="/blog/assets/js/60.bc9edb0a.js"><link rel="prefetch" href="/blog/assets/js/61.ec369f58.js"><link rel="prefetch" href="/blog/assets/js/62.75c1dd19.js"><link rel="prefetch" href="/blog/assets/js/63.a932b576.js"><link rel="prefetch" href="/blog/assets/js/64.d821c8d4.js"><link rel="prefetch" href="/blog/assets/js/65.c48ce70a.js"><link rel="prefetch" href="/blog/assets/js/66.6fc46ec0.js"><link rel="prefetch" href="/blog/assets/js/67.5a70400c.js"><link rel="prefetch" href="/blog/assets/js/68.6a172c1e.js"><link rel="prefetch" href="/blog/assets/js/69.d878fdd4.js"><link rel="prefetch" href="/blog/assets/js/7.b38bde58.js"><link rel="prefetch" href="/blog/assets/js/70.61fcea39.js"><link rel="prefetch" href="/blog/assets/js/71.b4f867d8.js"><link rel="prefetch" href="/blog/assets/js/72.d6eafa00.js"><link rel="prefetch" href="/blog/assets/js/73.7937e89b.js"><link rel="prefetch" href="/blog/assets/js/74.3ff087d7.js"><link rel="prefetch" href="/blog/assets/js/75.97ca5f60.js"><link rel="prefetch" href="/blog/assets/js/76.ece1fc97.js"><link rel="prefetch" href="/blog/assets/js/77.95cdf309.js"><link rel="prefetch" href="/blog/assets/js/78.da225d8a.js"><link rel="prefetch" href="/blog/assets/js/79.ae629400.js"><link rel="prefetch" href="/blog/assets/js/8.f710ef8c.js"><link rel="prefetch" href="/blog/assets/js/80.cf6d2811.js"><link rel="prefetch" href="/blog/assets/js/81.6c5fdca6.js"><link rel="prefetch" href="/blog/assets/js/82.245ed394.js"><link rel="prefetch" href="/blog/assets/js/83.4b549b0f.js"><link rel="prefetch" href="/blog/assets/js/84.17cebe69.js"><link rel="prefetch" href="/blog/assets/js/85.b1179a10.js"><link rel="prefetch" href="/blog/assets/js/86.ad526400.js"><link rel="prefetch" href="/blog/assets/js/87.2eef9fa1.js"><link rel="prefetch" href="/blog/assets/js/88.92f778ed.js"><link rel="prefetch" href="/blog/assets/js/89.efe59f09.js"><link rel="prefetch" href="/blog/assets/js/9.bf9d8b8d.js"><link rel="prefetch" href="/blog/assets/js/90.26e610ca.js"><link rel="prefetch" href="/blog/assets/js/91.f78cb514.js"><link rel="prefetch" href="/blog/assets/js/92.d7983929.js"><link rel="prefetch" href="/blog/assets/js/93.20272a60.js"><link rel="prefetch" href="/blog/assets/js/94.245150bc.js"><link rel="prefetch" href="/blog/assets/js/95.6f0e48c2.js"><link rel="prefetch" href="/blog/assets/js/96.02cf7f8b.js"><link rel="prefetch" href="/blog/assets/js/97.a9438771.js"><link rel="prefetch" href="/blog/assets/js/98.12762ccb.js"><link rel="prefetch" href="/blog/assets/js/99.ddbea38c.js">
- <link rel="stylesheet" href="/blog/assets/css/0.styles.cf6c3304.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-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/blog/web/#前端" data-v-06225672>前端</a></li><li data-v-06225672><a href="/blog/web/#学习笔记" data-v-06225672>学习笔记</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/heBody" target="_blank" title="作者" class="beLink" data-v-06225672>heBody</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>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><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div>
- Theme by
- <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a>
- | Copyright © 2019-2022
- <span>Evan Xu | <a href="https://github.com/heBody/blob/master/LICENSE" 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.b419507c.js" defer></script><script src="/blog/assets/js/2.602aac03.js" defer></script><script src="/blog/assets/js/30.0ac25def.js" defer></script>
- </body>
- </html>
|