typescript-基础类型
<p>ts代码有类型校验,必须指定类型</p><h1>一:JavaScript的基本类型</h1>
<p><span style="font-size: 16px">7种:(大小写敏感)</span></p>
<p><span style="font-size: 16px"> boolean: true 和false</span></p>
<p><span style="font-size: 16px"> null</span></p>
<p><span style="font-size: 16px"> undefined:变量未定义时的属性</span></p>
<p><span style="font-size: 16px"> number</span></p>
<p><span style="font-size: 16px"> string</span></p>
<p><span style="font-size: 16px"> symbol:(es6新添加的),它的实例是唯一且不可改变的</span></p>
<p> </p>
<h1>二:TypeScript的基本类型</h1>
<h2>1:数据类型</h2>
<p> </p>
<table class="reference">
<tbody>
<tr><th>数据类型</th><th>关键字</th><th>描述</th></tr>
<tr>
<td>任意类型</td>
<td>any</td>
<td>
<p>声明为 any 的变量可以赋予任意类型的值。</p>
<p>举例:</p>
<div> var oBox:any = document.getElementById('box');</div>
</td>
</tr>
<tr>
<td class="ts">数据类型</td>
<td class="ts">number</td>
<td>
<p>双精度 64 位浮点值。它可以用来表示整数和分数。</p>
<pre class="prettyprint prettyprinted"><span class="kwd">let<span class="pln"> binaryLiteral<span class="pun">:<span class="pln"> number <span class="pun">=<span class="pln"> <span class="lit">0b1010<span class="pun">;<span class="pln"> <span class="com">// 二进制<span class="pln">
<span class="kwd">let<span class="pln"> octalLiteral<span class="pun">:<span class="pln"> number <span class="pun">=<span class="pln"> <span class="lit">0o744<span class="pun">;<span class="pln"> <span class="com">// 八进制<span class="pln">
<span class="kwd">let<span class="pln"> decLiteral<span class="pun">:<span class="pln"> number <span class="pun">=<span class="pln"> <span class="lit">6<span class="pun">;<span class="pln"> <span class="com">// 十进制<span class="pln">
<span class="kwd">let<span class="pln"> hexLiteral<span class="pun">:<span class="pln"> number <span class="pun">=<span class="pln"> <span class="lit">0xf00d<span class="pun">;<span class="pln"> <span class="com">// 十六进制</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>字符串类型</td>
<td>string</td>
<td>
<p>一个字符系列,使用单引号(<span class="marked">')或双引号(<span class="marked">")来表示字符串类型。反引号(<span class="marked">`)来定义多行文本和内嵌表达式。</span></span></span></p>
<pre class="prettyprint prettyprinted"><span class="kwd">let<span class="pln"> name<span class="pun">:<span class="pln"> <span class="kwd">string<span class="pln"> <span class="pun">=<span class="pln"> <span class="str">"Runoob"<span class="pun">;<span class="pln">
<span class="kwd">let<span class="pln"> years<span class="pun">:<span class="pln"> number <span class="pun">=<span class="pln"> <span class="lit">5<span class="pun">;<span class="pln">
<span class="kwd">let<span class="pln"> words<span class="pun">:<span class="pln"> <span class="kwd">string<span class="pln"> <span class="pun">=<span class="pln"> <span class="str">`您好,今年是 ${ name } 发布 ${ years + 1} 周年`<span class="pun">;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>布尔类型</td>
<td>boolean</td>
<td>
<p>表示逻辑值:true 和 false。</p>
<pre class="prettyprint prettyprinted"><span class="kwd">let<span class="pln"> flag<span class="pun">:<span class="pln"> <span class="kwd">boolean<span class="pln"> <span class="pun">=<span class="pln"> <span class="kwd">true<span class="pun">;</span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>数组类型</td>
<td>无</td>
<td>
<p>声明变量为数组。</p>
<pre class="prettyprint prettyprinted"><span class="com">// 在元素类型后面加上[]<span class="pln">
<span class="kwd">let<span class="pln"> arr<span class="pun">:<span class="pln"> number<span class="pun">[]<span class="pln"> <span class="pun">=<span class="pln"> <span class="pun">[<span class="lit">1<span class="pun">,<span class="pln"> <span class="lit">2<span class="pun">];<span class="pln">
<span class="com">// 或者使用数组泛型<span class="pln">
<span class="kwd">let<span class="pln"> arr<span class="pun">:<span class="pln"> <span class="typ">Array<span class="str"><number><span class="pln"> <span class="pun">=<span class="pln"> <span class="pun">[<span class="lit">1<span class="pun">,<span class="pln"> <span class="lit">2<span class="pun">];<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<pre class="prettyprint prettyprinted"><span class="com">// 在元素类型后面加上[]<span class="pln">
<span class="kwd">let<span class="pln"> arr<span class="pun">:<span class="pln"> any<span class="pun">[]<span class="pln"> <span class="pun">=<span class="pln"> <span class="pun">[<span class="lit">1<span class="pun">,<span class="pln"> <span class="lit">'gg'<span class="pun">];</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<pre class="prettyprint prettyprinted"><span class="com"><span class="pln"><span class="kwd"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="lit"><span class="pun"><span class="pln"><span class="lit"><span class="pun"><span class="pln"><span class="com"><span class="pln"><span class="kwd"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="lit"><span class="pun"><span class="pln"><span class="lit"><span class="pun"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>元组</td>
<td>无</td>
<td>
<p>元组类型用来表示已知元素数量和类型的数组,各元素的类型不必相同,对应位置的类型需要相同。</p>
<pre class="prettyprint prettyprinted"><span class="kwd">let<span class="pln"> x<span class="pun">:<span class="pln"> <span class="pun">[<span class="kwd">string<span class="pun">,<span class="pln"> number<span class="pun">];<span class="pln">
x <span class="pun">=<span class="pln"> <span class="pun">[<span class="str">'Runoob'<span class="pun">,<span class="pln"> <span class="lit">1<span class="pun">];<span class="pln"> <span class="com">// 运行正常<span class="pln">
x <span class="pun">=<span class="pln"> <span class="pun">[<span class="lit">1<span class="pun">,<span class="pln"> <span class="str">'Runoob'<span class="pun">];<span class="pln"> <span class="com">// 报错<span class="pln">
console<span class="pun">.<span class="pln">log<span class="pun">(<span class="pln">x<span class="pun">[<span class="lit">0<span class="pun">]);<span class="pln"> <span class="com">// 输出 Runoob</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>枚举</td>
<td>enum</td>
<td>
<p>枚举类型用于定义数值集合。(定义更多的其他非数值类型的数据)</p>
<p>定义:</p>
<p> enum 枚举名称{</p>
<p> 标识符[=整型常数],</p>
<p> .....</p>
<p> }</p>
<p>举例:</p>
<p> enum flag {success=1,error=0};</p>
<pre class="prettyprint prettyprinted"><span class="kwd">enum<span class="pln"> <span class="typ">Color<span class="pln"> <span class="pun">{<span class="typ">Red<span class="pun">,<span class="pln"> <span class="typ">Green<span class="pun">,<span class="pln"> <span class="typ">Blue<span class="pun">};<span class="pln">
<span class="kwd">let<span class="pln"> c<span class="pun">:<span class="pln"> <span class="typ">Color<span class="pln"> <span class="pun">=<span class="pln"> <span class="typ">Color<span class="pun">.<span class="typ">Blue<span class="pun">;<span class="pln">
console<span class="pun">.<span class="pln">log<span class="pun">(<span class="pln">c<span class="pun">);<span class="pln"> <span class="com">// 输出 2,如果枚举类型没有赋值,那么输出的就是下标</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td class="ts">void</td>
<td class="ts">void</td>
<td>
<p>用于标识方法返回值的类型,表示该方法没有返回值。</p>
<pre class="prettyprint prettyprinted"><span class="kwd">function<span class="pln"> hello<span class="pun">():<span class="pln"> <span class="kwd">void<span class="pln"> <span class="pun">{<span class="pln">
alert<span class="pun">(<span class="str">"Hello Runoob"<span class="pun">);<span class="pln">
<span class="pun">}</span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>null</td>
<td>null</td>
<td>
<p>表示对象值缺失。</p>
</td>
</tr>
<tr>
<td>undefined</td>
<td>undefined</td>
<td>
<p>用于初始化变量为一个未定义的值;</p>
<p>举例:</p>
<p> 下面这样定义最好。 </p>
<pre class="prettyprint prettyprinted"><span class="kwd">let<span class="pln"> x<span class="pun">:<span class="pln"> number <span class="pun"><span style="background-color: rgba(255, 0, 0, 1)"><strong>|</strong></span><span class="pln"> <span class="kwd">null<span class="pln"> <span class="pun">|<span class="pln"> <span class="kwd">undefined<span class="pun">;<span class="pln">
x <span class="pun">=<span class="pln"> <span class="lit">1<span class="pun">;<span class="pln"> <span class="com">// 运行正确<span class="pln">
x <span class="pun">=<span class="pln"> <span class="kwd">undefined<span class="pun">;<span class="pln"> <span class="com">// 运行正确<span class="pln">
x <span class="pun">=<span class="pln"> <span class="kwd">null<span class="pun">;<span class="pln"> <span class="com">// 运行正确</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</td>
</tr>
<tr>
<td>never</td>
<td>never</td>
<td>
<p>never 是其它类型(包括 null 和 undefined)的子类型,代表从不会出现的值。这意味着声明为 never 类型的变量只能被 never 类型所赋值,在函数中它通常表现为抛出异常或无法执行到终止点(例如无线循环)</p>
<p>举例:</p>
<pre class="prettyprint prettyprinted"><span class="kwd">let<span class="pln"> x<span class="pun">:<span class="pln"> never<span class="pun">;<span class="pln"><span class="kwd"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="pln">
<span class="com">// 运行错误,数字类型不能转为 never 类型<span class="pln">
x <span class="pun">=<span class="pln"> <span class="lit">123<span class="pun">;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<p> </p>
</td>
</tr>
<tr>
<td> </td>
<td>Object </td>
<td>
<p> Object是一种非<code style="padding: 2px 4px; border-radius: 4px; color: rgba(191, 65, 74, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">number</code><span><span>,</span></span><code style="padding: 2px 4px; border-radius: 4px; color: rgba(191, 65, 74, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">string</code><span><span>,</span></span><code style="padding: 2px 4px; border-radius: 4px; color: rgba(191, 65, 74, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">boolean</code><span><span>,</span></span><code style="padding: 2px 4px; border-radius: 4px; color: rgba(191, 65, 74, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">symbol</code><span><span>,</span></span><code style="padding: 2px 4px; border-radius: 4px; color: rgba(191, 65, 74, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">null</code><span><span>,或</span></span><code style="padding: 2px 4px; border-radius: 4px; color: rgba(191, 65, 74, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 16px; font-style: normal; font-weight: 400; word-spacing: 0; white-space: normal; box-sizing: border-box; orphans: 2; widows: 2; background-color: rgba(255, 255, 255, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">undefined</code><span><span>的类型</span></span></p>
<p><span><span>举例:</span></span></p>
<p><span style="font-size: 14px"><span class="hljs-keyword" style="color: rgba(47, 79, 79, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: bold; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">declare<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"> <span class="hljs-function" style="color: rgba(47, 79, 79, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"><span class="hljs-keyword">function <span class="hljs-title">create(<span class="hljs-params">o: object | <span class="hljs-literal">null): <span class="hljs-title">void<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">;<br>create({ prop: <span class="hljs-number" style="color: rgba(47, 79, 79, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">0<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"> }); <span class="hljs-comment" style="color: rgba(115, 129, 145, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// OK<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"><br>create(<span class="hljs-literal" style="color: rgba(0, 72, 171, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">null<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">); <span class="hljs-comment" style="color: rgba(115, 129, 145, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// OK<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"><br><br>create(<span class="hljs-number" style="color: rgba(47, 79, 79, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">42<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">); <span class="hljs-comment" style="color: rgba(115, 129, 145, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// Error<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"><br>create(<span class="hljs-string" style="color: rgba(0, 72, 171, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">"string"<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">); <span class="hljs-comment" style="color: rgba(115, 129, 145, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// Error<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"><br>create(<span class="hljs-literal" style="color: rgba(0, 72, 171, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">false<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">); <span class="hljs-comment" style="color: rgba(115, 129, 145, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// Error<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"><br>create(<span class="hljs-literal" style="color: rgba(0, 72, 171, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">undefined<span style="color: rgba(47, 79, 79, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; float: none; display: inline !important; white-space: pre-wrap; orphans: 2; widows: 2; background-color: rgba(234, 238, 243, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">); <span class="hljs-comment" style="color: rgba(115, 129, 145, 1); text-transform: none; line-height: 1.6em; text-indent: 0; letter-spacing: normal; font-family: Menlo, Monaco, "Courier New", monospace; font-style: normal; font-weight: 400; word-spacing: 0; white-space: pre-wrap; box-sizing: border-box; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// Erro</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>r</span></p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<h2>2:类型断言(相当于转换数据类型)</h2>
<p>类型断言有两种形式。 其一是“尖括号”语法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> str = '1'
<span style="color: rgba(0, 0, 255, 1)">var</span> str2:number = <number> <any> str <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">str、str2 是 string 类型</span>
console.log(str2)</pre>
</div>
<p> </p>
<p>另一个为<code>as</code>语法:</p>
<div class="cnblogs_code">
<pre>let someValue: any = "this is a string"<span style="color: rgba(0, 0, 0, 1)">;
let strLength: number </span>= (someValue as string).length;</pre>
</div>
<p>两种形式是等价的。 至于使用哪个大多数情况下是凭个人喜好;然而,当你在TypeScript里使用JSX时,只有 <code>as</code>语法断言是被允许的。</p>
<p> </p>
<p> </p>
<h1>三:变量声明</h1>
<p> </p>
<p>TypeScript 变量的命名规则:</p>
<ul>
<li>
<p>变量名称可以包含数字和字母。</p>
</li>
<li>
<p>除了下划线 <span class="marked">_ 和美元 <span class="marked">$ 符号外,不能包含其他特殊字符,包括空格。</span></span></p>
</li>
<li>
<p>变量名不能以数字开头。</p>
</li>
</ul>
<p>变量使用前必须先声明,我们可以使用 var 来声明变量。</p>
<h2>1:var</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">声明变量的类型及初始值:
</span><span style="color: rgba(0, 0, 255, 1)">var</span> [变量名] : [类型] =<span style="color: rgba(0, 0, 0, 1)"> 值;
例如:
</span><span style="color: rgba(0, 0, 255, 1)">var</span> uname:string = "Runoob"<span style="color: rgba(0, 0, 0, 1)">;
声明变量的类型及但没有初始值,变量值会设置为 undefined:
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> [变量名] : [类型];
例如:
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> uname:string;
声明变量并初始值,但不设置类型类型,该变量可以是任意类型:
</span><span style="color: rgba(0, 0, 255, 1)">var</span> [变量名] =<span style="color: rgba(0, 0, 0, 1)"> 值;
例如:
</span><span style="color: rgba(0, 0, 255, 1)">var</span> uname = "Runoob"<span style="color: rgba(0, 0, 0, 1)">;
声明变量没有设置类型和初始值,类型可以是任意类型,默认初始值为 undefined:
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> [变量名];
例如:
</span><span style="color: rgba(0, 0, 255, 1)">var</span> uname;</pre>
</div>
<p> </p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> var 的变量提升的问题</span>
console.log(a);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">undefined ##如何看是否是提升变量---》局部作用域前后打印看看</span>
<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> a = 10<span style="color: rgba(0, 0, 0, 1)">;
}
console.log(a) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">10</span></pre>
</div>
<p> </p>
<h2>2:let (es6新增)</h2>
<p>let不存在变量提升,作用域是局部作用域</p>
<p> </p>
<h2>3:<code>const</code> 声明(es6新增)</h2>
<p><code>const</code> 声明是声明变量的另一种方式。</p>
<p>它们与<code>let</code>声明相似,但是就像它的名字所表达的,<strong><span style="text-decoration: underline">它们被赋值后不能再改变</span></strong>。 换句话说,它们拥有与 <code>let</code>相同的作用域规则,但是不能对它们重新赋值。</p>
<p>这很好理解,它们引用的值是<em>不可变的</em>。</p>
<div class="cnblogs_code">
<pre>const numLivesForCat = 9<span style="color: rgba(0, 0, 0, 1)">;
const kitty </span>=<span style="color: rgba(0, 0, 0, 1)"> {
name: </span>"Aurora"<span style="color: rgba(0, 0, 0, 1)">,
numLives: numLivesForCat,
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Error</span>
kitty =<span style="color: rgba(0, 0, 0, 1)"> {
name: </span>"Danielle"<span style="color: rgba(0, 0, 0, 1)">,
numLives: numLivesForCat
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> all "okay"</span>
kitty.name = "Rory"<span style="color: rgba(0, 0, 0, 1)">;
kitty.name </span>= "Kitty"<span style="color: rgba(0, 0, 0, 1)">;
kitty.name </span>= "Cat"<span style="color: rgba(0, 0, 0, 1)">;
kitty.numLives</span>--;</pre>
</div>
<p>除非你使用特殊的方法去避免,实际上<span style="text-decoration: underline"><strong><span style="color: rgba(255, 0, 0, 1); text-decoration: underline"><code>const</code>变量的内部状态是可修改的</span></strong></span>。 幸运的是,TypeScript允许你将对象的成员设置成只读的。</p>
<p> </p>
<h1>四:泛型</h1>
<h2>1:泛型函数---泛型的作用</h2>
<p>当我们需要给一个函数传入一个数字时(需要参数和返回值是同一类型),</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> hello(arg: number): number {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arg;
}</span></pre>
</div>
<p> </p>
<p>当我们需要给一个函数传入一个字符串时,</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> hello(arg: string): string {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arg;
}</span></pre>
</div>
<p> </p>
<p>这样是不是很麻烦呢?看下面写法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> hello(arg: any): any {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arg;
}</span></pre>
</div>
<p> </p>
<p>这样写的缺点:<span style="text-decoration: underline"><strong>any类型不能明确地表达参数与返回值必须是相同类型。因此泛型是能明确控制参数、返回值数据类型相同的方式</strong></span></p>
<p>泛型如下写法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span> hello<T><span style="color: rgba(0, 0, 0, 1)">(arg: T): T {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arg;
}</span></pre>
</div>
<p> </p>
<h2>2:泛型变量</h2>
<p><img src="https://img2018.cnblogs.com/blog/1451394/201906/1451394-20190628231205571-1130900653.png" alt=""></p>
<p>为什么上面的变量会报错呢?因为 T 只是捕捉传入的数据类型,并不能确定数据是number 还是 string还是什么。</p>
<p> <img src="https://img2018.cnblogs.com/blog/1451394/201906/1451394-20190628231521472-596030065.png" alt=""></p>
<p>像泛型数组就没有问题了</p>
<h1>五:枚举</h1>
<p> 为什么会有枚举呢?举个例子:后端开发约定订单的开始是0,未结账是1,运输中是2,运输完成是3,已收货是4.这样的纯数字会<strong><span style="text-decoration: underline">使得代码缺乏可读性</span></strong>。枚举就用于此场景。</p>
<h2>1:数字枚举</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">enum OrderStatus {
Start = 1,
Unpaid,
Shipping,
Shipped,
Complete,
}
</pre>
</div>
<p>就像上面我们就可以直接通过 OrderStatus.Start来代替原来的数字1,这样使得代码具备了可读性。</p>
<p>(上面Start = 1时,后面的枚举变量就是递增的)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">enum OrderStatus {
Start</span><span style="color: rgba(0, 0, 0, 1)">,
Unpaid,
Shipping,
Shipped,
Complete,
}</span></pre>
</div>
<p> </p>
<p> (上面Start就是0,后面的枚举变量就是递增的。我们虽然不在意后面的这些值,但这些值必须是确定的。)</p>
<h2>2:字符串枚举</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">enum OrderStatus {
Start = 'Start',
Unpaid = 'Unpaid',
Shipping = 'Shipping',
Shipped= 'Shipped',
Complete = 'Complete',
}
</pre>
</div>
<p>这样的可读性是不是更高了呢。 </p>
<p> </p>
<h1>六:symbol</h1>
<p> 自es6起,symbol 是类似于number、string的基本类型。</p>
<p>symbol的特点:symbol是唯一的,即使是使用同样的方式生成的两个symbol</p>
<p> </p>
<p> </p>
<h1>七:iterator 和 generator</h1>
<p> </p>
<p> </p>
<p> </p>
<h1> </h1>
<h1> </h1>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<pre><br><br><br></pre>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
或许你没有发现,你认真学习的样子总是最美的<br><br>
来源:https://www.cnblogs.com/lilz/p/10808139.html
頁:
[1]