ts-learnings/_book/04_interfaces.html
2019-04-04 16:11:16 +08:00

699 lines
64 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>接口 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="05_functions.html" />
<link rel="prev" href="03_classes.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="01_basic_data_types.html">
<a href="01_basic_data_types.html">
基本数据类型
</a>
</li>
<li class="chapter " data-level="1.3" data-path="02_variables_declaration.html">
<a href="02_variables_declaration.html">
变量声明
</a>
</li>
<li class="chapter " data-level="1.4" data-path="03_classes.html">
<a href="03_classes.html">
</a>
</li>
<li class="chapter active" data-level="1.5" data-path="04_interfaces.html">
<a href="04_interfaces.html">
接口
</a>
</li>
<li class="chapter " data-level="1.6" data-path="05_functions.html">
<a href="05_functions.html">
函数
</a>
</li>
<li class="chapter " data-level="1.7" data-path="06_generics.html">
<a href="06_generics.html">
泛型
</a>
</li>
<li class="chapter " data-level="1.8" data-path="07_enums.html">
<a href="07_enums.html">
枚举
</a>
</li>
<li class="chapter " data-level="1.9" data-path="08_type_inference.html">
<a href="08_type_inference.html">
类型推导
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >接口</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="&#x63A5;&#x53E3;&#xFF08;interfaces&#xFF09;">&#x63A5;&#x53E3;&#xFF08;Interfaces&#xFF09;</h1>
<h2 id="&#x7B80;&#x4ECB;">&#x7B80;&#x4ECB;</h2>
<p>TypeScript&#x8BED;&#x8A00;&#x7684;&#x6838;&#x5FC3;&#x539F;&#x5219;&#x4E4B;&#x4E00;&#xFF0C;&#x5C31;&#x662F;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x7740;&#x91CD;&#x4E8E;&#x503C;&#x6240;&#x5177;&#x6709;&#x7684; <em>&#x5F62;&#xFF08;shape&#xFF09;</em>&#xFF08;One of TypeScript&apos;s core principles is that type-checking focuses on the <em>shape</em> that values have&#xFF09;&#x3002;&#x8FD9;&#x6709;&#x65F6;&#x5019;&#x88AB;&#x79F0;&#x4E3A;&#x201C;<a href="https://zh.wikipedia.org/wiki/%E9%B8%AD%E5%AD%90%E7%B1%BB%E5%9E%8B" target="_blank">&#x9E2D;&#x5B50;&#x7C7B;&#x578B;&#xFF08;duck typing&#xFF09;</a>&#x201D; &#x6216; &#x201C;<a href="https://openhome.cc/Gossip/Scala/StructuralTyping.html" target="_blank">&#x7ED3;&#x6784;&#x5316;&#x5B50;&#x7C7B;&#x578B;&#xFF08;structural subtyping&#xFF09;</a>&#x201D;&#x3002;&#x5728;TypeScript&#x4E2D;&#xFF0C;&#x63A5;&#x53E3;&#x5145;&#x5F53;&#x4E86;&#x8FD9;&#x4E9B;&#x7C7B;&#x578B;&#x540D;&#x4E49;&#x4E0A;&#x7684;&#x89D2;&#x8272;&#xFF0C;&#x4E14;&#x662F;&#x4E00;&#x79CD;&#x5B9A;&#x4E49;&#x4EE3;&#x7801;&#x5185;&#x7684;&#x5408;&#x7EA6;&#xFF08;&#x7EA6;&#x5B9A;&#xFF09;&#xFF0C;&#x4EE5;&#x53CA;&#x4E0E;&#x9879;&#x76EE;&#x5916;&#x90E8;&#x4EE3;&#x7801;&#x7684;&#x5408;&#x7EA6;&#x7EA6;&#x5B9A;&#x7684;&#x5F3A;&#x5927;&#x65B9;&#x5F0F;&#xFF08;In TypeScript, interfaces fill the role of naming these types, and are a powerfull way of defining contracts within your code as well as contracts with code outside of your project&#xFF09;&#x3002;</p>
<h2 id="&#x63A5;&#x53E3;&#x521D;&#x6B65;&#xFF08;our-first-interface&#xFF09;">&#x63A5;&#x53E3;&#x521D;&#x6B65;&#xFF08;Our First Interface&#xFF09;</h2>
<p>&#x7406;&#x89E3;&#x63A5;&#x53E3;&#x7684;&#x6700;&#x5BB9;&#x6613;&#x65B9;&#x5F0F;&#xFF0C;&#x5C31;&#x662F;&#x4ECE;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x793A;&#x4F8B;&#x5F00;&#x59CB;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printLable</span> (<span class="hljs-params">labelledObj: { label: <span class="hljs-built_in">string</span> }</span>) </span>{
<span class="hljs-built_in">console</span>.log(labelledObj.label);
}
<span class="hljs-keyword">let</span> myObj = {size: <span class="hljs-number">10</span>, label: <span class="hljs-string">&quot;Size 10 Object&quot;</span>};
printLable(myObj);
</code></pre>
<p>&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x5BF9;<code>printLable</code>&#x7684;&#x8C03;&#x7528;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#x3002;&#x51FD;&#x6570;<code>printLable</code>&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x53C2;&#x6570;&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x8981;&#x6C42;&#x6240;&#x4F20;&#x5165;&#x7684;&#x5BF9;&#x8C61;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>label</code>&#x3001;&#x7C7B;&#x578B;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x7684;<code>myObj</code>&#x5B9E;&#x9645;&#x4E0A;&#x6709;&#x7740;&#x66F4;&#x591A;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x7F16;&#x8BD1;&#x5668;&#x5BF9;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x53EA;&#x68C0;&#x67E5;&#x5176; <em>&#x81F3;&#x5C11;</em> &#x6709;&#x7740;&#x5217;&#x51FA;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x4E14;&#x8981;&#x5339;&#x914D;&#x8981;&#x6C42;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5F53;&#x7136;&#x4E5F;&#x5B58;&#x5728;TypeScript&#x7F16;&#x8BD1;&#x5668;&#x4E0D;&#x90A3;&#x4E48;&#x5BBD;&#x5BB9;&#x7684;&#x60C5;&#x5F62;&#xFF0C;&#x8FD9;&#x4E00;&#x70B9;&#x5728;&#x540E;&#x9762;&#x4F1A;&#x8BB2;&#x5230;&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x7F16;&#x5199;&#x6B64;&#x793A;&#x4F8B;&#xFF0C;&#x8FD9;&#x6B21;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x6765;&#x63CF;&#x8FF0;&#x9700;&#x8981;&#x5177;&#x5907;<code>label</code>&#x5C5E;&#x6027;&#x8FD9;&#x4E00;&#x8981;&#x6C42;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> LabelledValue {
label: <span class="hljs-built_in">string</span>;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printLable</span> (<span class="hljs-params"> labelledObj: LabelledValue </span>) </span>{
<span class="hljs-built_in">console</span>.log(labelledObj.label);
}
<span class="hljs-keyword">let</span> myObj = { size: <span class="hljs-number">10</span>, label: <span class="hljs-string">&quot;Size 10 Object&quot;</span> };
printLable (myObj);
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;<code>LabelledValue</code>&#x63A5;&#x53E3;&#xFF0C;&#x662F;&#x4E00;&#x4E2A;&#x7ACB;&#x5373;&#x53EF;&#x7528;&#x4E8E;&#x63CF;&#x8FF0;&#x524D;&#x4E00;&#x793A;&#x4F8B;&#x4E2D;&#x7684;&#x8981;&#x6C42;&#x7684;&#x540D;&#x79F0;&#x3002;&#x5B83;&#x4ECD;&#x7136;&#x8868;&#x793A;&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>label</code>&#x3001;&#x7C7B;&#x578B;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x5E76;&#x6CA1;&#x6709;&#x50CF;&#x5728;&#x5176;&#x5B83;&#x8BED;&#x8A00;&#x4E2D;&#x4E00;&#x6837;&#xFF0C;&#x5FC5;&#x987B;&#x663E;&#x5F0F;&#x5730;&#x8BF4;&#x4F20;&#x9012;&#x7ED9;<code>printLable</code>&#x7684;&#x5BF9;&#x8C61;&#x5E94;&#x7528;&#x8BE5;&#x63A5;&#x53E3;&#x3002;&#x8FD9;&#x91CC;&#x53EA;&#x662F;&#x90A3;&#x4E2A; <em>&#x5F62;&#xFF08;shape&#xFF09;</em> &#x624D;&#x662F;&#x5173;&#x952E;&#x7684;&#x3002;&#x5982;&#x679C;&#x4F20;&#x9012;&#x7ED9;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x5BF9;&#x8C61;&#x6EE1;&#x8DB3;&#x4E86;&#x5217;&#x51FA;&#x7684;&#x8981;&#x6C42;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x662F;&#x5141;&#x8BB8;&#x7684;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6307;&#x51FA;&#x7684;&#x662F;&#xFF0C;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x4E0D;&#x8981;&#x6C42;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x4EE5;&#x4F55;&#x79CD;&#x987A;&#x5E8F;&#x8FDB;&#x5165;&#xFF0C;&#x53EA;&#x8981;&#x6709;&#x63A5;&#x53E3;&#x6240;&#x8981;&#x6C42;&#x7684;&#x5C5E;&#x6027;&#x53CA;&#x7C7B;&#x578B;&#x5373;&#x53EF;&#x3002;</p>
<h2 id="&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF08;optional-properties&#xFF09;">&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF08;Optional Properties&#xFF09;</h2>
<p>&#x63A5;&#x53E3;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x5728;&#x7279;&#x5B9A;&#x6761;&#x4EF6;&#x4E0B;&#x67D0;&#x4E9B;&#x5C5E;&#x6027;&#x5B58;&#x5728;&#xFF0C;&#x6216;&#x6839;&#x672C;&#x4E0D;&#x5B58;&#x5728;&#xFF08;Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all&#xFF09;&#x3002;&#x5728;&#x5EFA;&#x7ACB;&#x50CF;&#x662F;&#x90A3;&#x79CD;&#x5C06;&#x67D0;&#x4E2A;&#x4EC5;&#x6709;&#x5C11;&#x6570;&#x5C5E;&#x6027;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4F20;&#x9012;&#x7ED9;&#x67D0;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x201C;&#x9009;&#x9879;&#x5305;&#xFF08;option bags&#xFF09;&#x201D;&#x7684;&#x6A21;&#x5F0F;&#x65F6;&#xFF0C;&#x8FD9;&#x4E9B;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x7528;&#x5F97;&#x6BD4;&#x8F83;&#x666E;&#x904D;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x6B64;&#x79CD;&#x6A21;&#x5F0F;&#x7684;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> SquareConfig {
color?: <span class="hljs-built_in">string</span>;
width?: <span class="hljs-built_in">number</span>;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createSquare</span> (<span class="hljs-params"> config: SquareConfig </span>): </span>{color: <span class="hljs-built_in">string</span>; area: <span class="hljs-built_in">number</span>} {
<span class="hljs-keyword">let</span> newSquare = {color: <span class="hljs-string">&quot;white&quot;</span>, area: <span class="hljs-number">100</span>};
<span class="hljs-keyword">if</span> (config.color) {
newSquare.area = config.with * config.width;
}
<span class="hljs-keyword">return</span> newSquare;
}
<span class="hljs-keyword">let</span> mySquare = createSquare({color: <span class="hljs-string">&quot;black&quot;</span>});
</code></pre>
<p>&#x5E26;&#x6709;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5176;&#x5199;&#x6CD5;&#x4E0E;&#x5176;&#x5B83;&#x63A5;&#x53E3;&#x76F8;&#x4F3C;&#xFF0C;&#x53EA;&#x9700;&#x5728;&#x5404;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x7684;&#x58F0;&#x660E;&#x4E2D;&#xFF0C;&#x5728;&#x5C5E;&#x6027;&#x540D;&#x5B57;&#x7684;&#x672B;&#x5C3E;&#xFF0C;&#x4EE5;<code>?</code>&#x52A0;&#x4EE5;&#x8868;&#x793A;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x7684;&#x4F18;&#x52BF;&#x5728;&#x4E8E;&#xFF0C;&#x5728;&#x5BF9;&#x53EF;&#x80FD;&#x5B58;&#x5728;&#x7684;&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x63CF;&#x8FF0;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x963B;&#x6B62;&#x90A3;&#x4E9B;&#x4E0D;&#x662F;&#x8BE5;&#x63A5;&#x53E3;&#x7EC4;&#x6210;&#x90E8;&#x5206;&#x7684;&#x5C5E;&#x6027;&#x7684;&#x4F7F;&#x7528;&#x3002;&#x6BD4;&#x5982;&#x5728;&#x5C06;<code>createSquare</code>&#x4E2D;&#x7684;<code>color</code>&#x5C5E;&#x6027;&#x9519;&#x8BEF;&#x62FC;&#x5199;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5C31;&#x4F1A;&#x6536;&#x5230;&#x63D0;&#x9192;&#x7684;&#x9519;&#x8BEF;&#x6D88;&#x606F;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> SquareConfig {
color?: <span class="hljs-built_in">string</span>;
width?: <span class="hljs-built_in">number</span>;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createSquare</span> (<span class="hljs-params"> config: SquareConfig </span>): </span>{ color: <span class="hljs-built_in">string</span>; area: <span class="hljs-built_in">number</span> } {
<span class="hljs-keyword">let</span> newSquare = { color: <span class="hljs-string">&quot;white&quot;</span>, area: <span class="hljs-number">100</span> };
<span class="hljs-comment">//Property &apos;clor&apos; does not exist on type &apos;SquareConfig&apos;. Did you mean &apos;color&apos;? (2551) </span>
<span class="hljs-keyword">if</span> (config.color) {
newSquare.color = config.clor;
}
<span class="hljs-keyword">if</span> ( config.width ) {
newSquare.area = config.width * config.width;
}
<span class="hljs-keyword">return</span> newSquare;
}
<span class="hljs-keyword">let</span> mySquare = createSquare({color: <span class="hljs-string">&quot;black&quot;</span>});
</code></pre>
<h2 id="&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#xFF08;readonly-properties&#xFF09;">&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#xFF08;Readonly properties&#xFF09;</h2>
<p>&#x4E00;&#x4E9B;&#x5C5E;&#x6027;&#x53EA;&#x5E94;&#x5728;&#x5BF9;&#x8C61;&#x521A;&#x88AB;&#x521B;&#x5EFA;&#x65F6;&#x662F;&#x53EF;&#x4FEE;&#x6539;&#x7684;&#x3002;&#x90A3;&#x4E48;&#x53EF;&#x901A;&#x8FC7;&#x5C06;<code>readonly</code>&#x5173;&#x952E;&#x5B57;&#x653E;&#x5728;&#x8BE5;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#x524D;&#xFF0C;&#x5BF9;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x52A0;&#x4EE5;&#x6307;&#x5B9A;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Point {
readonly x: <span class="hljs-built_in">number</span>;
readonly y: <span class="hljs-built_in">number</span>;
}
</code></pre>
<p>&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6307;&#x6D3E;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6587;&#x5B57;&#xFF08;an object literal&#xFF09;&#xFF0C;&#x6784;&#x5EFA;&#x51FA;&#x4E00;&#x4E2A;<code>Point</code>&#x51FA;&#x6765;&#x3002;&#x5728;&#x8D4B;&#x503C;&#x8FC7;&#x540E;&#xFF0C;<code>x</code>&#x4E0E;<code>y</code>&#x5C31;&#x518D;&#x4E5F;&#x4E0D;&#x80FD;&#x4FEE;&#x6539;&#x4E86;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> p1: Point = { x: <span class="hljs-number">10</span>, y: <span class="hljs-number">20</span> };
p1.x = <span class="hljs-number">5</span>; <span class="hljs-comment">//Cannot assign to &apos;x&apos; because it is a constant or a read-only property. (2540)</span>
</code></pre>
<p>TypeScript &#x6709;&#x7740;&#x4E00;&#x4E2A;<code>ReadonlyArray&lt;T&gt;</code>&#x7C7B;&#x578B;&#xFF0C;&#x8BE5;&#x7C7B;&#x578B;&#x4E0E;<code>Array&lt;T&gt;</code>&#x4E00;&#x81F4;&#xFF0C;&#x53EA;&#x662F;&#x79FB;&#x9664;&#x4E86;&#x6240;&#x6709;&#x53D8;&#x5F02;&#x65B9;&#x6CD5;&#xFF08;with all mutating methods removed&#xFF09;&#xFF0C;&#x56E0;&#x6B64;&#x5411;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x5728;&#x67D0;&#x4E2A;&#x6570;&#x7EC4;&#x521B;&#x5EFA;&#x51FA;&#x540E;&#xFF0C;&#x4E0D;&#x4F1A;&#x88AB;&#x4FEE;&#x6539;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> a: <span class="hljs-built_in">number</span>[] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-keyword">let</span> ro: ReadonlyArray&lt;<span class="hljs-built_in">number</span>&gt; = a;
ro[<span class="hljs-number">0</span>] = <span class="hljs-number">12</span>; <span class="hljs-comment">//Index signature in type &apos;ReadonlyArray&lt;number&gt;&apos; only permits reading. (2542)</span>
ro.push(<span class="hljs-number">5</span>); <span class="hljs-comment">//Property &apos;push&apos; does not exist on type &apos;ReadonlyArray&lt;number&gt;&apos;. (2339) </span>
ro.length = <span class="hljs-number">100</span>;<span class="hljs-comment">//Cannot assign to &apos;length&apos; because it is a constant or a read-only property. (2540)</span>
a = ro;<span class="hljs-comment">//Type &apos;ReadonlyArray&lt;number&gt;&apos; is not assignable to type &apos;number[]&apos;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4E2D;&#x6700;&#x540E;&#x4E00;&#x884C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x5C06;&#x6574;&#x4E2A;<code>ReadonlyArray</code>&#x5F80;&#x56DE;&#x8D4B;&#x503C;&#x7ED9;&#x6B63;&#x5E38;&#x6570;&#x7EC4;&#xFF0C;&#x4E5F;&#x662F;&#x975E;&#x6CD5;&#x7684;&#x3002;&#x4F46;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#xFF08;a type assertion&#xFF09;&#xFF0C;&#x4EE5;&#x6D88;&#x9664;&#x6B64;&#x9519;&#x8BEF;&#xFF1A;</p>
<pre><code class="lang-typescript">a = ro as <span class="hljs-built_in">number</span>[];
</code></pre>
<h3 id="readonly-&#x4E0E;-const&#x7684;&#x533A;&#x522B;"><code>readonly</code> &#x4E0E; <code>const</code>&#x7684;&#x533A;&#x522B;</h3>
<p>&#x5BF9;&#x4E8E;&#x8981;&#x4F7F;&#x7528;<code>readonly</code>&#x6216;<code>const</code>&#xFF0C;&#x6700;&#x7B80;&#x5355;&#x7684;&#x529E;&#x6CD5;&#x5C31;&#x662F;&#x533A;&#x5206;&#x662F;&#x8981;&#x5728;&#x53D8;&#x91CF;&#x4E0A;&#xFF0C;&#x8FD8;&#x662F;&#x5C5E;&#x6027;&#x4E0A;&#x4F7F;&#x7528;&#x3002;&#x5BF9;&#x4E8E;&#x53D8;&#x91CF;&#xFF0C;&#x5F53;&#x7136;&#x5C31;&#x7528;<code>const</code>&#xFF0C;&#x5C5E;&#x6027;&#x5219;&#x7528;<code>readonly</code>&#x3002;</p>
<h2 id="&#x5173;&#x4E8E;&#x591A;&#x4F59;&#x5C5E;&#x6027;&#x68C0;&#x67E5;&#xFF08;excess-property-checks&#xFF09;">&#x5173;&#x4E8E;&#x591A;&#x4F59;&#x5C5E;&#x6027;&#x68C0;&#x67E5;&#xFF08;Excess Property Checks&#xFF09;</h2>
<p>&#x5728;&#x91C7;&#x7528;&#x4E86;&#x63A5;&#x53E3;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;TypeScript&#x4EE4;&#x5230;&#x53EF;&#x5C06;<code>{size: number; label: string;}</code>&#x4F20;&#x9012;&#x7ED9;&#x67D0;&#x4E9B;&#x4EC5;&#x671F;&#x671B;&#x4E00;&#x4E2A;<code>{label: string;}</code>&#x7684;&#x5730;&#x65B9;&#x3002;&#x540E;&#x9762;&#x8FD8;&#x4ECB;&#x7ECD;&#x4E86;&#x5173;&#x4E8E;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF0C;&#x4EE5;&#x53CA;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x5728;&#x540D;&#x4E3A;&#x201C;&#x9009;&#x9879;&#x5305;&#xFF08;option bags&#xFF09;&#x201D;&#x7684;&#x5730;&#x65B9;&#x5982;&#x4F55;&#x53D1;&#x6325;&#x4F5C;&#x7528;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x5982;&#x50CF;&#x5728;JavaScript&#x4E2D;&#x90A3;&#x6837;&#xFF0C;&#x5C06;&#x8FD9;&#x4E24;&#x4E2A;&#x7279;&#x6027;&#x5355;&#x7EAF;&#x5730;&#x7ED3;&#x5408;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x5C31;&#x8DB3;&#x4EE5;&#x6740;&#x6B7B;&#x4F60;&#x81EA;&#x5DF1;&#xFF0C;&#x4E0B;&#x9762;&#x5C31;&#x7528;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x4F7F;&#x7528;<code>createSquare</code>&#x6765;&#x8BF4;&#x660E;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> SquareConfig {
color?: <span class="hljs-built_in">string</span>;
width?: <span class="hljs-built_in">number</span>;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createSquare</span> (<span class="hljs-params"> config: SquareConfig </span>): </span>{ color: <span class="hljs-built_in">string</span>; area: <span class="hljs-built_in">number</span> } {
<span class="hljs-comment">// ...</span>
}
<span class="hljs-keyword">let</span> mySquare = createSquare ({ colour: <span class="hljs-string">&quot;red&quot;</span>, width: <span class="hljs-number">100</span> });
</code></pre>
<p>&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x7ED9;&#x4E88;<code>createSquare</code>&#x7684;&#x53C2;&#x6570;&#x88AB;&#x5199;&#x6210;&#x4E86;<code>colour</code>&#xFF0C;&#x800C;&#x4E0D;&#x662F;<code>color</code>&#x3002;&#x5728;&#x666E;&#x901A;&#x7684;JavaScript&#x4E2D;&#xFF0C;&#x8FD9;&#x7C7B;&#x9519;&#x8BEF;&#x5C06;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x8FD9;&#x4E2A;&#x8BDA;&#x5B9E;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x8BF4;&#x6CA1;&#x6709;&#x9519;&#x8BEF;&#x62FC;&#x5199;&#xFF0C;&#x56E0;&#x4E3A;<code>width</code>&#x5C5E;&#x6027;&#x662F;&#x517C;&#x5BB9;&#x7684;&#xFF0C;&#x6CA1;&#x6709;<code>color</code>&#x5C5E;&#x6027;&#x51FA;&#x73B0;&#xFF0C;&#x540C;&#x65F6;&#x8FD9;&#x91CC;&#x989D;&#x5916;&#x7684;<code>colour</code>&#x5C5E;&#x6027;&#x662F;&#x4E0D;&#x91CD;&#x8981;&#x7684;&#x3002;</p>
<p>&#x4E0D;&#x8FC7;&#xFF0C;TypeScript&#x4F1A;&#x8BA4;&#x4E3A;&#x5728;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4E2D;&#x5B58;&#x5728;&#x95EE;&#x9898;&#x3002;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x503C;&#x4F1A;&#x53D7;&#x5230;&#x7279;&#x522B;&#x5BF9;&#x5F85;&#xFF0C;&#x540C;&#x65F6;&#x5728;&#x5C06;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x503C;&#x8D4B;&#x4E88;&#x7ED9;&#x5176;&#x5B83;&#x53D8;&#x91CF;&#xFF0C;&#x6216;&#x8005;&#x5C06;&#x5B83;&#x4EEC;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x52A0;&#x4EE5;&#x4F20;&#x9012;&#x65F6;&#xFF0C;&#x800C;&#x6536;&#x5230; <em>&#x591A;&#x4F59;&#x5C5E;&#x6027;&#x68C0;&#x67E5;</em>&#x3002;&#x5982;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x503C;&#x6709;&#x7740;&#x4EFB;&#x4F55;&#x76EE;&#x6807;&#x5BF9;&#x8C61;&#x4E0D;&#x5177;&#x6709;&#x7684;&#x5C5E;&#x6027;&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x62A5;&#x51FA;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// Argument of type &apos;{ colour: string; width: number; }&apos; is not assignable to parameter of type &apos;SquareConfig&apos;.</span>
<span class="hljs-comment">// Object literal may only specify known properties, but &apos;colour&apos; does not exist in type &apos;SquareConfig&apos;. Did you mean to write &apos;color&apos;? (2345)</span>
<span class="hljs-keyword">let</span> mySquare = createSquare({colour: <span class="hljs-string">&quot;red&quot;</span>, width: <span class="hljs-number">100</span>});
</code></pre>
<p>&#x7ED5;&#x8FC7;&#x6B64;&#x7C7B;&#x68C0;&#x67E5;&#x5B9E;&#x9645;&#x4E0A;&#x76F8;&#x5F53;&#x7B80;&#x5355;&#x3002;&#x6700;&#x5BB9;&#x6613;&#x7684;&#x505A;&#x6CD5;&#x5C31;&#x662F;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#xFF08;a type assertion&#xFF09;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> mySquare = createSquare({width: <span class="hljs-number">100</span>, opacity: <span class="hljs-number">0.5</span>} as SquareConfig);
</code></pre>
<p>&#x4E0D;&#x8FC7;&#xFF0C;&#x5728;&#x786E;&#x5B9A;&#x5BF9;&#x8C61;&#x53EF;&#x80FD;&#x6709;&#x67D0;&#x4E9B;&#x5728;&#x7279;&#x522B;&#x60C5;&#x51B5;&#x4E0B;&#x4F1A;&#x7528;&#x5230;&#x989D;&#x5916;&#x5C5E;&#x6027;&#x65F6;&#xFF0C;&#x4E00;&#x79CD;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x662F;&#x4E3A;&#x5176;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#xFF08;a string index signature&#xFF09;&#x3002;&#x6BD4;&#x5982;&#x5728;&#x8FD9;&#x91CC;&#x7684;<code>SquareConfig</code>&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x6709;&#x7740;&#x4E0A;&#x9762;<code>color</code>&#x4E0E;<code>width</code>&#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x4E5F;&#x53EF;&#x4EE5;&#x5177;&#x6709;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x5176;&#x5B83;&#x5C5E;&#x6027;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x5C06;&#x5176;&#x5B9A;&#x4E49;&#x6210;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> SquareConfig {
color?: <span class="hljs-built_in">string</span>;
width?: <span class="hljs-built_in">number</span>;
[propName: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span>;
}
</code></pre>
<p>&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#x5728;&#x540E;&#x9762;&#x4F1A;&#x6D89;&#x53CA;&#xFF0C;&#x8FD9;&#x91CC;&#x8BF4;&#x7684;&#x662F;<code>SquareConfig</code>&#x53EF;&#x4EE5;&#x6709;&#x7740;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x800C;&#x53EA;&#x8981;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x4E0D;&#x662F;<code>color</code>&#x6216;<code>width</code>&#x5C31;&#x53EF;&#x4EE5;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x5E76;&#x4E0D;&#x91CD;&#x8981;&#x3002;</p>
<p>&#x7ED5;&#x8FC7;&#x8FD9;&#x4E9B;&#x68C0;&#x67E5;&#x7684;&#x4E00;&#x79CD;&#x7EC8;&#x6781;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x80FD;&#x6709;&#x70B9;&#x610F;&#x5916;&#xFF0C;&#x5C31;&#x662F;&#x5C06;&#x8BE5;&#x5BF9;&#x8C61;&#x8D4B;&#x503C;&#x7ED9;&#x53E6;&#x4E00;&#x53D8;&#x91CF;&#xFF1A;&#x56E0;&#x4E3A;<code>squareConfig</code>&#x4E0D;&#x4F1A;&#x53D7;&#x591A;&#x4F59;&#x5C5E;&#x6027;&#x68C0;&#x67E5;&#xFF0C;&#x56E0;&#x6B64;&#x7F16;&#x8BD1;&#x5668;&#x4E5F;&#x5C31;&#x4E0D;&#x4F1A;&#x7ED9;&#x51FA;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> squareConfig = { colour: <span class="hljs-string">&quot;red&quot;</span>, width: <span class="hljs-number">100</span> };
<span class="hljs-keyword">let</span> mySquare = createSquare(squareConfig);
</code></pre>
<p>&#x8BF7;&#x8BB0;&#x4F4F;&#x5BF9;&#x4E8E;&#x50CF;&#x662F;&#x4E0A;&#x9762;&#x7684;&#x7B80;&#x5355;&#x4EE3;&#x7801;&#xFF0C;&#x4E00;&#x822C;&#x4E0D;&#x5FC5;&#x5C1D;&#x8BD5;&#x201C;&#x7ED5;&#x8FC7;&#x201D;&#x8FD9;&#x4E9B;&#x68C0;&#x67E5;&#x3002;&#x800C;&#x5BF9;&#x4E8E;&#x66F4;&#x4E3A;&#x590D;&#x6742;&#x7684;&#x3001;&#x6709;&#x7740;&#x65B9;&#x6CD5;&#x5E76;&#x5B58;&#x6709;&#x72B6;&#x6001;&#x7684;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x503C;&#xFF08;complex object literals that have methods and hold state&#xFF09;&#xFF0C;&#x53EF;&#x80FD;&#x5C31;&#x8981;&#x7262;&#x8BB0;&#x8FD9;&#x4E9B;&#x6280;&#x5DE7;&#x4E86;&#xFF0C;&#x4F46;&#x5927;&#x591A;&#x6570;&#x7684;&#x591A;&#x4F59;&#x5C5E;&#x6027;&#x9519;&#x8BEF;&#xFF0C;&#x90FD;&#x662F;&#x771F;&#x5B9E;&#x5B58;&#x5728;&#x7684;bugs&#x3002;&#x90A3;&#x5C31;&#x610F;&#x5473;&#x7740;&#x5728;&#x4F7F;&#x7528;&#x8BF8;&#x5982;&#x9009;&#x9879;&#x5305;&#xFF08;option bags&#xFF09;&#x8FD9;&#x7C7B;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x800C;&#x51FA;&#x73B0;&#x591A;&#x4F59;&#x5C5E;&#x6027;&#x68C0;&#x67E5;&#x7C7B;&#x95EE;&#x9898;&#x65F6;&#xFF0C;&#x5C31;&#x5E94;&#x8BE5;&#x5BF9;&#x7C7B;&#x578B;&#x5B9A;&#x4E49;&#x52A0;&#x4EE5;&#x5BA1;&#x89C6;&#x3002;&#x5728;&#x6B64;&#x5B9E;&#x4F8B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x5141;&#x8BB8;&#x5C06;&#x67D0;&#x4E2A;&#x6709;&#x7740;<code>color</code>&#x6216;<code>colour</code>&#x5C5E;&#x6027;&#x7684;&#x5BF9;&#x8C61;&#x4F20;&#x9012;&#x7ED9;<code>createSquare</code>&#x65B9;&#x6CD5;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x8981;&#x4FEE;&#x6539;<code>SquareConfig</code>&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x6765;&#x53CD;&#x5E94;&#x51FA;&#x8FD9;&#x4E00;&#x70B9;&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x578B;&#xFF08;function-types&#xFF09;">&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x578B;&#xFF08;Function Types&#xFF09;</h2>
<p>&#x5BF9;&#x4E8E;&#x63CF;&#x8FF0;JavaScript&#x7684;&#x5BF9;&#x8C61;&#x6240;&#x80FD;&#x63A5;&#x53D7;&#x7684;&#x8303;&#x56F4;&#x5BBD;&#x5E7F;&#x7684;&#x5F62;&#xFF0C;&#x63A5;&#x53E3;&#x90FD;&#x662F;&#x53EF;&#x884C;&#x7684;&#xFF08;Interfaces are capable of describing the wide range of shapes that JavaScript objects can take&#xFF09;&#x3002;&#x9664;&#x4E86;&#x7528;&#x4E8E;&#x63CF;&#x8FF0;&#x5E26;&#x6709;&#x5C5E;&#x6027;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x63A5;&#x53E3;&#x8FD8;&#x53EF;&#x4EE5;&#x63CF;&#x8FF0;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x8981;&#x7528;&#x63A5;&#x53E3;&#x6765;&#x63CF;&#x8FF0;&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x8981;&#x7ED9;&#x4E88;&#x8BE5;&#x63A5;&#x53E3;&#x4E00;&#x4E2A;&#x8C03;&#x7528;&#x7B7E;&#x540D;&#xFF08;a call signature&#xFF09;&#x3002;&#x8FD9;&#x5C31;&#x50CF;&#x662F;&#x4E00;&#x4E2A;&#x4EC5;&#x6709;&#x7740;&#x53C2;&#x6570;&#x6E05;&#x5355;&#x4E0E;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x3002;&#x53C2;&#x6570;&#x6E05;&#x5355;&#x4E2D;&#x7684;&#x5404;&#x53C2;&#x6570;&#xFF0C;&#x90FD;&#x8981;&#x6C42;&#x540D;&#x79F0;&#x4E0E;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> SearchFunc {
(source: <span class="hljs-built_in">string</span>, subString: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">boolean</span>;
}
</code></pre>
<p>&#x4E00;&#x65E6;&#x5B9A;&#x4E49;&#x597D;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x50CF;&#x4F7F;&#x7528;&#x5176;&#x5B83;&#x63A5;&#x53E3;&#x4E00;&#x6837;&#xFF0C;&#x5BF9;&#x6B64;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x63A5;&#x53E3;&#xFF08;this function type interface&#xFF09;&#x8FDB;&#x884C;&#x4F7F;&#x7528;&#x4E86;&#x3002;&#x8FD9;&#x91CC;&#x5C55;&#x793A;&#x4E86;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x67D0;&#x79CD;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x628A;&#x540C;&#x4E00;&#x7C7B;&#x578B;&#x7684;&#x51FD;&#x6570;&#x503C;&#x8D4B;&#x4E88;&#x7ED9;&#x5B83;&#x7684;&#x8FC7;&#x7A0B;&#xFF08;create <em>a variable of a function type</em> and assign it <em>a function value</em> of the same type&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> mySearch: SearchFunc;
mySearch = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">source: <span class="hljs-built_in">string</span>; subString: <span class="hljs-built_in">string</span></span>) </span>{
<span class="hljs-keyword">let</span> result = source.search(subString);
<span class="hljs-keyword">return</span> result &gt; <span class="hljs-number">-1</span>;
}
</code></pre>
<p>&#x53C2;&#x6570;&#x540D;&#x79F0;&#x65E0;&#x9700;&#x5339;&#x914D;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5BF9;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x6B63;&#x786E;&#x7684;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x3002;&#x6BD4;&#x5982;&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x7F16;&#x5199;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> mySearch: SearchFunc;
mySearch = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">src: <span class="hljs-built_in">string</span>, sub: <span class="hljs-built_in">string</span></span>): <span class="hljs-title">boolean</span> </span>{
<span class="hljs-keyword">let</span> result = src.search(sub);
<span class="hljs-keyword">return</span> result &gt; <span class="hljs-number">-1</span>;
}
</code></pre>
<p>&#x51FD;&#x6570;&#x53C2;&#x6570;&#x4F1A;&#x9010;&#x4E00;&#x68C0;&#x67E5;&#xFF0C;&#x4EE5;&#x6BCF;&#x4E2A;&#x76F8;&#x5E94;&#x53C2;&#x6570;&#x4F4D;&#x7F6E;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E0E;&#x5BF9;&#x5E94;&#x7684;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#xFF08;Function parameters are checked one at a time, with the type in each corresponding parameter position checked against each other&#xFF09;&#x3002;&#x5982;&#x5B8C;&#x5168;&#x4E0D;&#x6253;&#x7B97;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#xFF0C;&#x90A3;&#x4E48;TypeScript&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x5C31;&#x53EF;&#x4EE5;&#x63A8;&#x65AD;&#x51FA;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x4E3A;&#x8BE5;&#x51FD;&#x6570;&#x503C;&#x662F;&#x76F4;&#x63A5;&#x8D4B;&#x4E88;&#x7ED9;<code>SearchFunc</code>&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x7684;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x8FD9;&#x91CC;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#xFF0C;&#x662F;&#x7531;&#x5176;&#x8FD4;&#x56DE;&#x503C;&#xFF08;&#x4E5F;&#x5C31;&#x662F;<code>false</code>&#x6216;<code>true</code>&#xFF09;&#x9690;&#x5F0F;&#x7ED9;&#x51FA;&#x7684;&#x3002;&#x52A0;&#x5165;&#x8BA9;&#x8BE5;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x6570;&#x5B57;&#x6216;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x90A3;&#x4E48;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#xFF08;the type-checker&#xFF09;&#x5C31;&#x4F1A;&#x53D1;&#x51FA;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x4E0E;<code>SearchFunc</code>&#x63A5;&#x53E3;&#x4E2D;&#x63CF;&#x8FF0;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x4E0D;&#x7B26;&#x7684;&#x8B66;&#x544A;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> mySearch: SearchFunc;
mySearch = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">src, sub</span>) </span>{
<span class="hljs-keyword">let</span> result = src.search(sub);
<span class="hljs-keyword">return</span> result &gt; <span class="hljs-number">-1</span>;
}
</code></pre>
<h2 id="&#x53EF;&#x7D22;&#x5F15;&#x7684;&#x7C7B;&#x578B;&#xFF08;indexable-types&#xFF09;">&#x53EF;&#x7D22;&#x5F15;&#x7684;&#x7C7B;&#x578B;&#xFF08;Indexable Types&#xFF09;</h2>
<p>&#x4E0E;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x6765;&#x63CF;&#x8FF0;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x7C7B;&#x4F3C;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x7C7B;&#x63CF;&#x8FF0;&#x90A3;&#x4E9B;&#x53EF;&#x4EE5;&#x7D22;&#x5F15;&#x7684;&#x7C7B;&#x578B;&#xFF08;types that we can &quot;index into&quot;&#xFF09;&#xFF0C;&#x6BD4;&#x5982;<code>a[10]</code>&#xFF0C;&#x6291;&#x6216;<code>ageMap[&quot;daniel&quot;]</code>&#x8FD9;&#x6837;&#x7684;&#x3002;&#x53EF;&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x63CF;&#x8FF0;&#x7528;&#x4E8E;&#x5728;&#x8BE5;&#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x8FDB;&#x884C;&#x7D22;&#x5F15;&#x7684;&#x7C7B;&#x578B;&#x7684; <em>&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#xFF08;index signature&#xFF09;</em>&#xFF0C;&#x4EE5;&#x53CA;&#x5728;&#x7D22;&#x5F15;&#x65F6;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x6765;&#x770B;&#x770B;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> StringArray {
[index: <span class="hljs-built_in">number</span>]: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">let</span> myArray: StringArray;
myArray = [<span class="hljs-string">&quot;Bob&quot;</span>, <span class="hljs-string">&quot;Fred&quot;</span>];
<span class="hljs-keyword">let</span> myStr: <span class="hljs-built_in">string</span> = myArray[<span class="hljs-number">0</span>];
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7684;<code>StringArray</code>&#x63A5;&#x53E3;&#x3002;&#x6B64;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x6307;&#x51FA;&#x5728;&#x67D0;&#x4E2A;<code>StringArray</code>&#x4EE5;&#x67D0;&#x4E2A;<code>number</code>&#x52A0;&#x4EE5;&#x7D22;&#x5F15;&#x65F6;&#xFF0C;&#x5B83;&#x5C06;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>string</code>&#x3002;</p>
<p>TypeScript&#x652F;&#x6301;&#x7684;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x6709;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x53CA;&#x6570;&#x5B57;&#x3002;&#x540C;&#x65F6;&#x652F;&#x6301;&#x8FD9;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x7D22;&#x5F15;&#x5668;&#x662F;&#x53EF;&#x80FD;&#x7684;&#xFF0C;&#x4F46;&#x4ECE;&#x67D0;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x7D22;&#x5F15;&#x5668;&#x6240;&#x8FD4;&#x56DE;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x662F;&#x4ECE;&#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x5668;&#x6240;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#xFF08;It is possible to support both types of indexers, but the type returned from a numeric indexer must be a subtype of the type returned from the string indexer&#xFF09;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5728;&#x4EE5;&#x67D0;&#x4E2A;<code>number</code>&#x8FDB;&#x884C;&#x7D22;&#x5F15;&#x65F6;&#xFF0C;JavaScript&#x5B9E;&#x9645;&#x4E0A;&#x4F1A;&#x5728;&#x5BF9;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x7D22;&#x5F15;&#x524D;&#xFF0C;&#x5C06;&#x5176;&#x8F6C;&#x6362;&#x6210;<code>string</code>&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5728;&#x4F7F;&#x7528;<code>100</code>&#xFF08;<code>number</code>&#xFF09;&#x6765;&#x8FDB;&#x884C;&#x7D22;&#x5F15;&#x65F6;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x4E0E;&#x4F7F;&#x7528;<code>&quot;100&quot;</code>&#xFF08;<code>string</code>&#xFF09;&#x6548;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x4E8C;&#x8005;&#x5C31;&#x9700;&#x8981;&#x4E00;&#x81F4;&#xFF08;That means that indexing with <code>100</code> (a <code>number</code>) is the same thing as indexing with <code>&quot;100&quot;</code> (a <code>stirng</code>), so the two need to be consistent&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Animal {
name: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">class</span> Dog extends Animal {
breed: <span class="hljs-built_in">string</span>;
}
<span class="hljs-comment">// Numeric index type &apos;Animal&apos; is not assignable to string index type &apos;Dog&apos;. (2413)</span>
<span class="hljs-keyword">interface</span> NotOkay {
[x: <span class="hljs-built_in">number</span>]: Animal;
[x: <span class="hljs-built_in">string</span>]: Dog;
}
</code></pre>
<p>&#x5C3D;&#x7BA1;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x662F;&#x63CF;&#x8FF0;&#x201C;&#x5B57;&#x5178;&#x201D;&#x6A21;&#x5F0F;&#x7684;&#x4E00;&#x79CD;&#x5F3A;&#x5927;&#x65B9;&#x5F0F;&#xFF0C;&#x4F46;&#x5B83;&#x4EEC;&#x540C;&#x65F6;&#x5F3A;&#x5236;&#x4E86;&#x4E0E;&#x5B83;&#x4EEC;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x5339;&#x914D;&#x7684;&#x5C5E;&#x6027;&#x503C;&#xFF08;While string index signatures are a powerful way to describe the &quot;dictionary&quot; pattern, they also enforce that all properties match their return type&#xFF09;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7D22;&#x5F15;&#x7533;&#x660E;&#x4E86;<code>obj.property</code>&#x540C;&#x65F6;&#x4E0E;<code>obj[&quot;property&quot;]</code>&#x53EF;&#x7528;&#x3002;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;<code>name</code>&#x7684;&#x7C7B;&#x578B;&#x4E0E;&#x8BE5;&#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x5668;&#x7684;&#x7C7B;&#x578B;&#x5E76;&#x4E0D;&#x5339;&#x914D;&#xFF0C;&#x90A3;&#x4E48;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x5C31;&#x4F1A;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-comment">//Property &apos;name&apos; of type &apos;string&apos; is not assignable to string index type &apos;number&apos;. (2411)</span>
<span class="hljs-keyword">interface</span> NumberDictionary {
[index: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">number</span>;
length: <span class="hljs-built_in">number</span>;
name: <span class="hljs-built_in">string</span>;
}
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x4E3A;&#x4E86;&#x963B;&#x6B62;&#x5BF9;&#x6307;&#x6570;&#x7684;&#x8D4B;&#x503C;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5C06;&#x8FD9;&#x4E9B;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7F6E;&#x4E3A;&#x53EA;&#x8BFB;&#xFF08;Finally, you can make index signatures readonly in order to prevent assignment to their indices&#xFF09;:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> ReadonlyStringArray {
readonly [index: <span class="hljs-built_in">number</span>]: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">let</span> myArray: ReadonlyStringArray = [<span class="hljs-string">&quot;Alice&quot;</span>, <span class="hljs-string">&quot;Bob&quot;</span>];
<span class="hljs-comment">//Index signature in type &apos;ReadonlyStringArray&apos; only permits reading. (2542)</span>
myArray[<span class="hljs-number">2</span>] = <span class="hljs-string">&quot;Mallory&quot;</span>;
</code></pre>
<p>&#x56E0;&#x4E3A;&#x6B64;&#x5904;&#x7684;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x80FD;&#x8BBE;&#x7F6E;<code>myArray[2]</code>&#x4E86;&#x3002;</p>
<h2 id="&#x7C7B;&#x7684;&#x7C7B;&#x578B;&#xFF08;class-types&#xFF09;">&#x7C7B;&#x7684;&#x7C7B;&#x578B;&#xFF08;Class Types&#xFF09;</h2>
<h3 id="&#x5E94;&#x7528;&#x67D0;&#x4E2A;&#x63A5;&#x53E3;&#xFF08;implementing-an-interface&#xFF09;">&#x5E94;&#x7528;&#x67D0;&#x4E2A;&#x63A5;&#x53E3;&#xFF08;Implementing an interface&#xFF09;</h3>
<p>&#x5728;&#x8BF8;&#x5982;C#&#x53CA;Java&#x8FD9;&#x6837;&#x7684;&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;&#x63A5;&#x53E3;&#x7684;&#x4E00;&#x79CD;&#x6700;&#x5E38;&#x7528;&#x65B9;&#x5F0F;&#xFF0C;&#x5C31;&#x662F;&#x663E;&#x5F0F;&#x5730;&#x5F3A;&#x8C03;&#x67D0;&#x4E2A;&#x7C7B;&#x6EE1;&#x8DB3;&#x4E00;&#x79CD;&#x7279;&#x5B9A;&#x7684;&#x5408;&#x7EA6;&#xFF0C;&#x90A3;&#x4E48;&#x5728;TypeScript&#x4E2D;&#xFF0C;&#x8FD9;&#x6837;&#x505A;&#x4E5F;&#x662F;&#x53EF;&#x80FD;&#x7684;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> ClockInterface {
currentTime: <span class="hljs-built_in">Date</span>;
}
<span class="hljs-keyword">class</span> Clock <span class="hljs-keyword">implements</span> ClockInterface {
currentTime: <span class="hljs-built_in">Date</span>;
<span class="hljs-keyword">constructor</span> (h: number, m: number) {}
}
</code></pre>
<p>&#x5728;&#x63A5;&#x53E3;&#x4E2D;&#x8FD8;&#x53EF;&#x4EE5;&#x5BF9;&#x5C06;&#x5728;&#x7C7B;&#x4E2D;&#x5E94;&#x7528;&#x5230;&#x7684;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x63CF;&#x8FF0;&#xFF0C;&#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x793A;&#x4F8B;&#x4E2D;&#x5BF9;<code>setTime</code>&#x6240;&#x505A;&#x7684;&#x90A3;&#x6837;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> ClockInterface {
currentTime: <span class="hljs-built_in">Date</span>;
setTime (d: <span class="hljs-built_in">Date</span>);
}
<span class="hljs-keyword">class</span> Clock <span class="hljs-keyword">implements</span> ClockInterface {
currentTime: <span class="hljs-built_in">Date</span>;
setTime (d: <span class="hljs-built_in">Date</span>) {
<span class="hljs-keyword">this</span>.currentTime = d;
}
<span class="hljs-keyword">constructor</span> (h: number, m: number) {}
}
</code></pre>
<p>&#x63A5;&#x53E3;&#x5BF9;&#x7C7B;&#x7684;&#x516C;&#x5171;&#x4FA7;&#x8FDB;&#x884C;&#x4E86;&#x63CF;&#x8FF0;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x540C;&#x65F6;&#x63CF;&#x8FF0;&#x516C;&#x5171;&#x53CA;&#x79C1;&#x6709;&#x4FA7;&#x3002;&#x8FD9;&#x5C31;&#x7981;&#x6B62;&#x5BF9;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x6765;&#x5BF9;&#x540C;&#x65F6;&#x6709;&#x7740;&#x7279;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x8BE5;&#x7C7B;&#x5B9E;&#x4F8B;&#x7684;&#x79C1;&#x6709;&#x9762;&#x7684;&#x7C7B;&#xFF0C;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#xFF08;Interfaces describe the public side of the class, rather than both the public and private side. This prohibits you from using them to check that a class also has particular types for the private side of the class instance&#xFF09;&#x3002;</p>
<h3 id="&#x7C7B;&#x7684;&#x9759;&#x6001;&#x4E0E;&#x5B9E;&#x4F8B;&#x4FA7;&#xFF08;difference-between-the-static-and-instance-sides-of-classes&#xFF09;">&#x7C7B;&#x7684;&#x9759;&#x6001;&#x4E0E;&#x5B9E;&#x4F8B;&#x4FA7;&#xFF08;Difference between the static and instance sides of classes&#xFF09;</h3>
<p>&#x5728;&#x4E0E;&#x7C7B;&#x4E00;&#x540C;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x662F;&#x65F6;&#xFF0C;&#x8BB0;&#x4F4F;&#x7C7B;&#x6709;&#x7740;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#xFF1A;&#x9759;&#x6001;&#x4FA7;&#x7684;&#x7C7B;&#x578B;&#x4E0E;&#x793A;&#x4F8B;&#x4FA7;&#x7684;&#x7C7B;&#x578B;&#xFF08;the type of the static side and the type of the instance side&#xFF09;&#xFF0C;&#x662F;&#x6709;&#x5E2E;&#x52A9;&#x7684;&#x3002;&#x6216;&#x8BB8;&#x5DF2;&#x7ECF;&#x6CE8;&#x610F;&#x5230;&#x5728;&#x4F7F;&#x7528;&#x6784;&#x5EFA;&#x7B7E;&#x540D;&#x6765;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x5E76;&#x5C1D;&#x8BD5;&#x5E94;&#x7528;&#x6B64;&#x63A5;&#x53E3;&#x6765;&#x5EFA;&#x7ACB;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C06;&#x62A5;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> ClockInterface {
<span class="hljs-keyword">new</span> (hour: <span class="hljs-built_in">number</span>, minute: <span class="hljs-built_in">number</span>);
}
<span class="hljs-keyword">class</span> Clock <span class="hljs-keyword">implements</span> ClockInterface {
currentTime: <span class="hljs-built_in">Date</span>;
<span class="hljs-keyword">constructor</span> (h: number, m: number) {}
}
</code></pre>
<p>&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5728;&#x67D0;&#x4E2A;&#x7C7B;&#x5E94;&#x7528;&#x67D0;&#x4E2A;&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x4EC5;&#x6709;&#x8BE5;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x4FA7;&#x88AB;&#x68C0;&#x67E5;&#x4E86;&#x3002;&#x56E0;&#x4E3A;&#x8BE5;&#x6784;&#x5EFA;&#x5668;&#x4F4D;&#x5904;&#x9759;&#x6001;&#x4FA7;&#xFF0C;&#x6240;&#x4EE5;&#x5176;&#x5E76;&#x4E0D;&#x5305;&#x542B;&#x5728;&#x6B64;&#x68C0;&#x67E5;&#x4E2D;&#x3002;</p>
<p>&#x90A3;&#x4E48;&#x5C31;&#x9700;&#x8981;&#x76F4;&#x63A5;&#x5728;&#x8BE5;&#x7C7B;&#x7684;&#x9759;&#x6001;&#x4FA7;&#x4E0A;&#x52A8;&#x624B;&#x4E86;&#x3002;&#x5728;&#x6B64;&#x5B9E;&#x4F8B;&#x4E2D;&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x63A5;&#x53E3;&#xFF1A;&#x7528;&#x4E8E;&#x6784;&#x5EFA;&#x5668;&#x7684;<code>ClockConstrutor</code>&#x4E0E;&#x7528;&#x4E8E;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x7684;<code>ClockInterface</code>&#x3002;&#x968F;&#x540E;&#x4E3A;&#x4FBF;&#x5229;&#x8D77;&#x89C1;&#xFF0C;&#x8FD9;&#x91CC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x6784;&#x5EFA;&#x5668;&#x51FD;&#x6570;<code>createClock</code>&#xFF0C;&#x4EE5;&#x521B;&#x5EFA;&#x51FA;&#x4F20;&#x9012;&#x7ED9;&#x5B83;&#x7684;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> ClockConstrutor {
<span class="hljs-keyword">new</span> (hour: <span class="hljs-built_in">number</span>, minute: <span class="hljs-built_in">number</span>): ClockInterface;
}
<span class="hljs-keyword">interface</span> ClockInterface {
tick();
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createClock</span> (<span class="hljs-params">ctor: ClockConstrutor, hour: <span class="hljs-built_in">number</span>, minute: <span class="hljs-built_in">number</span></span>): <span class="hljs-title">ClockInterface</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ctor (hour, minute);
}
<span class="hljs-keyword">class</span> DigitalClock <span class="hljs-keyword">implements</span> ClockInterface {
<span class="hljs-keyword">constructor</span> (h: number, m: number) {}
tick () {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;beep beep&quot;</span>);
}
}
<span class="hljs-keyword">class</span> AnalogClock <span class="hljs-keyword">implements</span> ClockInterface {
<span class="hljs-keyword">constructor</span> (h: number, m: number) {}
tick () {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;tick tock&quot;</span>);
}
}
<span class="hljs-keyword">let</span> digital = createClock (DigitalClock, <span class="hljs-number">12</span>, <span class="hljs-number">17</span>);
<span class="hljs-keyword">let</span> analog = createClock (AnalogClock, <span class="hljs-number">7</span>, <span class="hljs-number">32</span>);
</code></pre>
<p>&#x56E0;&#x4E3A;<code>createClock</code>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;<code>ClockConstrutor</code>, &#x90A3;&#x4E48;&#x5728;<code>createClock(AnalogClock, 7, 32)</code>&#x4E2D;&#xFF0C;&#x5B83;&#x5C31;&#x5BF9;<code>AnalogClock</code>&#x6709;&#x7740;&#x6B63;&#x786E;&#x7684;&#x6784;&#x5EFA;&#x7B7E;&#x540D;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#x3002;</p>
<h2 id="&#x6269;&#x5C55;&#x63A5;&#x53E3;&#xFF08;extending-interfaces&#xFF09;">&#x6269;&#x5C55;&#x63A5;&#x53E3;&#xFF08;Extending Interfaces&#xFF09;</h2>
<p>&#x4E0E;&#x7C7B;&#x4E00;&#x6837;&#xFF0C;&#x63A5;&#x53E3;&#x4E5F;&#x53EF;&#x4EE5;&#x76F8;&#x4E92;&#x6269;&#x5C55;&#x3002;&#x6B64;&#x7279;&#x6027;&#x4EE4;&#x5230;&#x5C06;&#x67D0;&#x63A5;&#x53E3;&#x7684;&#x6210;&#x5458;&#x62F7;&#x8D1D;&#x5230;&#x53E6;&#x4E00;&#x63A5;&#x53E3;&#x53EF;&#x884C;&#xFF0C;&#x8FD9;&#x5C31;&#x5728;&#x5C06;&#x63A5;&#x53E3;&#x5206;&#x79BB;&#x4E3A;&#x53EF;&#x91CD;&#x7528;&#x7EC4;&#x4EF6;&#x65F6;&#xFF0C;&#x63D0;&#x4F9B;&#x66F4;&#x591A;&#x7684;&#x7075;&#x6D3B;&#x6027;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Shape {
color: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">interface</span> Square <span class="hljs-keyword">extends</span> Shape {
sideLength: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">let</span> square = &lt;Square&gt; {};
square.color = <span class="hljs-string">&quot;blue&quot;</span>;
square.sideLength = <span class="hljs-number">10</span>;
</code></pre>
<p>&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x8FD8;&#x53EF;&#x4EE5;&#x5BF9;&#x591A;&#x4E2A;&#x63A5;&#x53E3;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#xFF0C;&#x4ECE;&#x800C;&#x521B;&#x5EFA;&#x51FA;&#x6240;&#x6709;&#x63A5;&#x53E3;&#x7684;&#x4E00;&#x4E2A;&#x8054;&#x5408;&#xFF08;a combination of all of the interfaces&#xFF09;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Shape {
color: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">interface</span> PenStroke {
penWidth: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">interface</span> Square <span class="hljs-keyword">extends</span> Shape, PenStroke {
sideLength: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">let</span> square = &lt;Square&gt; {};
square.color = <span class="hljs-string">&quot;blue&quot;</span>;
square.sideLength = <span class="hljs-number">10</span>;
square.penWidth = <span class="hljs-number">5.0</span>;
</code></pre>
<h2 id="&#x6DF7;&#x5408;&#x7C7B;&#x578B;&#xFF08;hybrid-types&#xFF09;">&#x6DF7;&#x5408;&#x7C7B;&#x578B;&#xFF08;Hybrid Types&#xFF09;</h2>
<p>&#x6B63;&#x5982;&#x65E9;&#x5148;&#x6240;&#x63D0;&#x5230;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x63A5;&#x53E3;&#x5177;&#x5907;&#x63CF;&#x8FF0;&#x5B58;&#x5728;&#x4E8E;&#x771F;&#x5B9E;&#x4E16;&#x754C;JavaScript&#x4E2D;&#x7684;&#x4E30;&#x5BCC;&#x7C7B;&#x578B;&#xFF08;As we mentioned earlier, interfaces can describe the rich types present in real world JavaScript&#xFF09;&#x3002;&#x7531;&#x4E8E;JavaScript&#x7684;&#x52A8;&#x6001;&#x4E14;&#x7075;&#x6D3B;&#x7684;&#x5929;&#x6027;&#xFF0C;&#x56E0;&#x6B64;&#x5076;&#x5C14;&#x4F1A;&#x9047;&#x5230;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x5C06;&#x4EE5;&#x7ED3;&#x5408;&#x4E0A;&#x8FF0;&#x5404;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x65B9;&#x5F0F;&#x8FD0;&#x4F5C;&#x7684;&#x60C5;&#x51B5;&#x3002;</p>
<p>&#x8FD9;&#x7C7B;&#x5B9E;&#x4F8B;&#x4E4B;&#x4E00;&#xFF0C;&#x5C31;&#x662F;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x540C;&#x65F6;&#x4EE5;&#x51FD;&#x6570;&#x4E0E;&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x5E26;&#x6709;&#x4E00;&#x4E9B;&#x5C5E;&#x6027;&#x65B9;&#x5F0F;&#x884C;&#x4E8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Counter {
(start: <span class="hljs-built_in">number</span>): <span class="hljs-built_in">string</span>;
interval: <span class="hljs-built_in">number</span>;
reset(): <span class="hljs-built_in">void</span>;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getCounter</span> (<span class="hljs-params"></span>): <span class="hljs-title">Counter</span> </span>{
<span class="hljs-keyword">let</span> counter = &lt;Counter&gt; <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">start: <span class="hljs-built_in">number</span></span>) </span>{};
counter.interval = <span class="hljs-number">123</span>;
counter.reset = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{};
<span class="hljs-keyword">return</span> counter;
}
<span class="hljs-keyword">let</span> c = getCounter();
c(<span class="hljs-number">10</span>);
c.reset();
c.interval = <span class="hljs-number">5.0</span>;
</code></pre>
<p>&#x5728;&#x4E0E;&#x7B2C;&#x4E09;&#x65B9;JavaScript&#xFF08;&#x6CE8;&#xFF1A;TypeScript, &#x4F60;&#xFF0C;&#x522B;&#x4EBA;&#x7684;&#x7A0B;&#x5E8F;&#xFF09;&#x4EA4;&#x4E92;&#x65F6;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4E0A;&#x9762;&#x8FD9;&#x6837;&#x7684;&#x6A21;&#x5F0F;&#xFF0C;&#x6765;&#x5145;&#x5206;&#x63CF;&#x8FF0;&#x7C7B;&#x578B;&#x7684;&#x5F62;&#x72B6;&#xFF08;When interacting with 3rd-party JavaScript, you may need to use patterns like the above to fully describe the shape of the type&#xFF09;&#x3002;</p>
<h2 id="&#x5BF9;&#x7C7B;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#x7684;&#x63A5;&#x53E3;&#xFF08;interface-extending-classes&#xFF09;">&#x5BF9;&#x7C7B;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#x7684;&#x63A5;&#x53E3;&#xFF08;Interface Extending Classes&#xFF09;</h2>
<p>&#x5F53;&#x67D0;&#x4E2A;&#x63A5;&#x53E3;&#x5BF9;&#x7C7B;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#x65F6;&#xFF0C;&#x5B83;&#x5C06;&#x7EE7;&#x627F;&#x8BE5;&#x7C7B;&#x7684;&#x6210;&#x5458;&#xFF0C;&#x5374;&#x4E0D;&#x7EE7;&#x627F;&#x8FD9;&#x4E9B;&#x6210;&#x5458;&#x7684;&#x5B9E;&#x73B0;&#xFF08;When an interface type extends a class type, it inherits the members of the class but not their implementations&#xFF09;&#x3002;&#x8FD9;&#x5C31;&#x5982;&#x540C;&#x63A5;&#x53E3;&#x5DF2;&#x7ECF;&#x5BF9;&#x8BE5;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x6210;&#x5458;&#x8FDB;&#x884C;&#x4E86;&#x58F0;&#x660E;&#xFF0C;&#x800C;&#x6CA1;&#x6709;&#x63D0;&#x4F9B;&#x5230;&#x5176;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x3002;&#x63A5;&#x53E3;&#x751A;&#x81F3;&#x4F1A;&#x7EE7;&#x627F;&#x5230;&#x67D0;&#x4E2A;&#x57FA;&#x7C7B;&#x7684;&#x79C1;&#x6709;&#x53CA;&#x53D7;&#x4FDD;&#x62A4;&#x6210;&#x5458;&#x3002;&#x90A3;&#x5C31;&#x610F;&#x5473;&#x7740;&#x5728;&#x521B;&#x5EFA;&#x67D0;&#x4E2A;&#x5BF9;&#x5E26;&#x6709;&#x79C1;&#x6709;&#x53CA;&#x4FDD;&#x62A4;&#x6210;&#x5458;&#x7684;&#x7C7B;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#x7684;&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x6240;&#x5EFA;&#x7ACB;&#x7684;&#x63A5;&#x53E3;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x53EA;&#x80FD;&#x88AB;&#x88AB;&#x6269;&#x5C55;&#x7684;&#x7C7B;&#x6240;&#x5176;&#x5B50;&#x7C7B;&#x6240;&#x5E94;&#x7528;&#xFF08;&#x5B9E;&#x73B0;&#xFF0C;It is as if the interface had declared all of the members of the class without providing an implementation. Interfaces inherit even the private and protected members of a base class. This means that when you create an interface that extends a class with private or protected members, that interface type can only be implemented by that class or a subclass of it&#xFF09;&#x3002;</p>
<p>&#x5728;&#x6709;&#x7740;&#x5927;&#x7684;&#x7EE7;&#x627F;&#x5C42;&#x6B21;&#x65F6;&#xFF0C;&#x6B64;&#x7279;&#x6027;&#x662F;&#x6709;&#x7528;&#x7684;&#xFF0C;&#x4F46;&#x9700;&#x8981;&#x6307;&#x51FA;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x53EA;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x6709;&#x7740;&#x4EC5;&#x5E26;&#x6709;&#x786E;&#x5B9A;&#x5C5E;&#x6027;&#x7684;&#x5B50;&#x7C7B;&#x65F6;&#x624D;&#x6709;&#x7528;&#xFF08;This is useful when you have a large inheritance hierarchy, but want to specify that your code works with only subclass that have certain properties&#xFF09;&#x3002;&#x8FD9;&#x4E9B;&#x5B50;&#x7C7B;&#x9664;&#x4E86;&#x7EE7;&#x627F;&#x81EA;&#x57FA;&#x7C7B;&#x5916;&#xFF0C;&#x4E0D;&#x5FC5;&#x662F;&#x6709;&#x5173;&#x8054;&#x7684;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Control {
<span class="hljs-keyword">private</span> state: <span class="hljs-built_in">any</span>;
}
<span class="hljs-keyword">interface</span> SelectableControl <span class="hljs-keyword">extends</span> Control {
select (): <span class="hljs-built_in">void</span>;
}
<span class="hljs-keyword">class</span> Button extends Control <span class="hljs-keyword">implements</span> SelectableControl {
select () {}
}
<span class="hljs-keyword">class</span> TextBox extends Control {}
<span class="hljs-comment">//Class &apos;Image&apos; incorrectly implements interface &apos;SelectableControl&apos;.</span>
<span class="hljs-comment">//Property &apos;state&apos; is missing in type &apos;Image&apos;. (2420)</span>
<span class="hljs-keyword">class</span> Image <span class="hljs-keyword">implements</span> SelectableControl {
select () {}
}
<span class="hljs-keyword">class</span> Location {}
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;<code>SelectableControl</code>&#x5305;&#x542B;&#x4E86;&#x6240;&#x6709;<code>Control</code>&#x7684;&#x6210;&#x5458;&#xFF0C;&#x5305;&#x62EC;&#x79C1;&#x6709;&#x7684;<code>state</code>&#x5C5E;&#x6027;&#x3002;&#x56E0;&#x4E3A;<code>state</code>&#x662F;&#x4E00;&#x4E2A;&#x79C1;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x56E0;&#x6B64;&#x5BF9;&#x4E8E;<code>Control</code>&#x7684;&#x540E;&#x4EE3;&#xFF0C;&#x5C31;&#x53EA;&#x53EF;&#x80FD;&#x53BB;&#x5E94;&#x7528;<code>SelectableControl</code>&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x4E86;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x53EA;&#x6709;<code>Control</code>&#x7684;&#x540E;&#x4EE3;&#xFF0C;&#x624D;&#x4F1A;&#x6709;&#x7740;&#x8FD9;&#x4E2A;&#x6E90;&#x81EA;&#x540C;&#x4E00;&#x58F0;&#x660E;&#x7684;<code>state</code>&#x79C1;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x79C1;&#x6709;&#x6210;&#x5458;&#x53EF;&#x7528;&#x7684;&#x4E00;&#x4E2A;&#x8981;&#x6C42;&#xFF08;Since <code>state</code> is a private member it is only possible for descendants of <code>Control</code> to implement <code>SelectableControl</code>. This is because only descendants of <code>Control</code> will have a <code>state</code> private member that originates in the same declaration, which is a requirement for private members to be compatible&#xFF09;&#x3002;</p>
<p>&#x5728;<code>Control</code>&#x8FD9;&#x4E2A;&#x7C7B;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7;<code>SelectableControl</code>&#x7684;&#x67D0;&#x4E2A;&#x5B9E;&#x4F8B;&#x53BB;&#x8BBF;&#x95EE;<code>state</code>&#x8FD9;&#x4E2A;&#x79C1;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x662F;&#x53EF;&#x80FD;&#x7684;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x67D0;&#x4E2A;<code>SelectableControl</code>&#x4E5F;&#x4F1A;&#x4E0E;&#x4E00;&#x4E2A;&#x5DF2;&#x77E5;&#x6709;&#x7740;<code>select</code>&#x65B9;&#x6CD5;&#x7684;<code>Control</code>&#x90A3;&#x6837;&#x884C;&#x4E8B;&#xFF08;Effectively, a <code>SelectableControl</code> acts like a <code>Control</code> that is known to have a <code>select</code> method&#xFF09;&#x3002;&#x8FD9;&#x91CC;&#x7684;<code>Button</code>&#x4E0E;<code>TextBox</code>&#x90FD;&#x662F;<code>SelectableControl</code>&#x7684;&#x5B50;&#x7C7B;&#x578B;&#xFF08;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x7EE7;&#x627F;&#x81EA;<code>Control</code>&#xFF0C;&#x5E76;&#x6709;&#x7740;<code>select</code>&#x65B9;&#x6CD5;&#xFF09;, &#x4F46;<code>Image</code>&#x4E0E;<code>Location</code>&#x5C31;&#x4E0D;&#x662F;&#x4E86;&#x3002;</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="03_classes.html" class="navigation navigation-prev " aria-label="Previous page: 类">
<i class="fa fa-angle-left"></i>
</a>
<a href="05_functions.html" class="navigation navigation-next " aria-label="Next page: 函数">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"接口","level":"1.5","depth":1,"next":{"title":"函数","level":"1.6","depth":1,"path":"05_functions.md","ref":"05_functions.md","articles":[]},"previous":{"title":"类","level":"1.4","depth":1,"path":"03_classes.md","ref":"03_classes.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"sharing":{"weibo":false,"all":["facebook","google","twitter","weibo","instapaper"],"google":false,"twitter":true,"vk":false,"instapaper":false,"facebook":true},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css","pdf":"styles/pdf.css","epub":"styles/epub.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css","pdf":"styles/pdf.css","epub":"styles/epub.css"}},"file":{"path":"04_interfaces.md","mtime":"2019-04-03T06:43:16.308Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-04-04T00:04:23.982Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>