【区分】Typescript 中 interface 和 type
<blockquote><p>在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影。只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现。但最近总看到他们,就想深入的了解一下他们。</p>
</blockquote>
<h3 id="interface接口">interface:接口</h3>
<p>TypeScript 的核心原则之一是对值所具有的结构进行类型检查。 而接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。</p>
<pre><code>interface LabelledValue {
label: string;
}
function printLabel(labelledObj: LabelledValue) {
console.log(labelledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
</code></pre>
<p>接口就好比一个名字,用来描述上面例子里的要求。</p>
<p>接口具有的特性:</p>
<ul>
<li>可选属性</li>
</ul>
<pre><code>interface SquareConfig {
color?: string;
}
</code></pre>
<ul>
<li>只读属性</li>
</ul>
<pre><code>interface Point {
readonly x: number;
}
</code></pre>
<ul>
<li>多余属性检查,防止使用不属于接口的属性</li>
</ul>
<pre><code>interface Preson {
name: string;
age?: number;
}
let p1:Person = {name: '小明'} // 正确
let p2:Person = {name: '小明', age: 18, sex: '男'}; // 报错
// 绕过:多余属性不报错
// 方式1
let p = {name: '小明', age: 18, sex: '男'};
let p3 = p;
// 方式2
interface Preson {
name: string;
age?: number;
: any
}
let p4 = {name: '小明', age: 18, sex: '男'};
</code></pre>
<ul>
<li>函数类型</li>
</ul>
<pre><code>interface SearchFunc {
(source: string, subString: string): boolean;
}
</code></pre>
<ul>
<li>索引类型: 针对数组</li>
</ul>
<pre><code>interface StringArray {
: string;
}
let myArray: StringArray;
myArray = ["Bob", "Fred"];
</code></pre>
<ul>
<li>类类型
<ul>
<li>类实现接口</li>
</ul>
<pre><code>interface ClockInterface {
currentTime: Date;
setTime(d: Date);
}
class Clock implements ClockInterface {
currentTime: Date;
setTime(d: Date) {
this.currentTime = d;
}
constructor(h: number, m: number) { }
}
</code></pre>
<ul>
<li>接口继承接口,可多个</li>
</ul>
<pre><code>interface Shape {
color: string;
}
interface PenStroke {
penWidth: number;
}
interface Square extends Shape, PenStroke {
sideLength: number;
}
let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;
</code></pre>
</li>
</ul>
<h3 id="type类型别名">type:类型别名</h3>
<p>type 会给一个类型起个新名字。 type 有时和 interface 很像,但是可以作用于原始值(基本类型),联合类型,元组以及其它任何你需要手写的类型。</p>
<p>举例:</p>
<pre><code>type Name = string; // 基本类型
type NameResolver = () => string; // 函数
type NameOrResolver = Name | NameResolver; // 联合类型
function getName(n: NameOrResolver): Name {
if (typeof n === 'string') {
return n;
} else {
return n();
}
}
</code></pre>
<p>起别名不会新建一个类型 - 它创建了一个新 名字来引用那个类型。给基本类型起别名通常没什么用,尽管可以做为文档的一种形式使用。</p>
<p>同接口一样,类型别名也可以是泛型 - 我们可以添加类型参数并且在别名声明的右侧传入:</p>
<pre><code>type Container<T> = { value: T };
</code></pre>
<p>也可以使用类型别名来在属性里引用自己:</p>
<pre><code>type Tree<T> = {
value: T;
left: Tree<T>;
right: Tree<T>;
}
</code></pre>
<p>与交叉类型一起使用,我们可以创建出一些十分稀奇古怪的类型。</p>
<pre><code>type LinkedList<T> = T & { next: LinkedList<T> };
interface Person {
name: string;
}
var people: LinkedList<Person>;
var s = people.name;
var s = people.next.name;
var s = people.next.next.name;
var s = people.next.next.next.name;
</code></pre>
<p>然而,类型别名不能出现在声明右侧的任何地方。</p>
<pre><code>type Yikes = Array<Yikes>; // error
</code></pre>
<h3 id="interface-vs-type">interface vs type</h3>
<h4 id="1-objects--functions">1. Objects / Functions</h4>
<p>两者都可以用来描述对象或函数的类型,但是语法不同。</p>
<p>Interface</p>
<pre><code>interface Point {
x: number;
y: number;
}
interface SetPoint {
(x: number, y: number): void;
}
</code></pre>
<p>Type alias</p>
<pre><code>type Point = {
x: number;
y: number;
};
type SetPoint = (x: number, y: number) => void;
</code></pre>
<h4 id="2-other-types">2. Other Types</h4>
<p>与接口不同,类型别名还可以用于其他类型,如基本类型(原始值)、联合类型、元组。</p>
<pre><code>// primitive
type Name = string;
// object
type PartialPointX = { x: number; };
type PartialPointY = { y: number; };
// union
type PartialPoint = PartialPointX | PartialPointY;
// tuple
type Data = ;
// dom
let div = document.createElement('div');
type B = typeof div;
</code></pre>
<h4 id="3-extend">3. Extend</h4>
<p>两者都可以扩展,但是语法又有所不同。此外,请注意接口和类型别名不是互斥的。接口可以扩展类型别名,反之亦然。</p>
<p>Interface extends interface</p>
<pre><code>interface PartialPointX { x: number; }
interface Point extends PartialPointX { y: number; }
</code></pre>
<p>Type alias extends type alias</p>
<pre><code>type PartialPointX = { x: number; };
type Point = PartialPointX & { y: number; };
</code></pre>
<p>Interface extends type alias</p>
<pre><code>type PartialPointX = { x: number; };
interface Point extends PartialPointX { y: number; }
</code></pre>
<p>Type alias extends interface</p>
<pre><code>interface PartialPointX { x: number; }
type Point = PartialPointX & { y: number; };
</code></pre>
<h4 id="4-class-implements">4. class Implements</h4>
<p>类可以以相同的方式实现接口或类型别名。但是请注意,类和接口被认为是静态的。因此,它们不能实现/扩展命名联合类型的类型别名。</p>
<pre><code>interface Point {
x: number;
y: number;
}
class SomePoint implements Point {
x: 1;
y: 2;
}
type Point2 = {
x: number;
y: number;
};
class SomePoint2 implements Point2 {
x: 1;
y: 2;
}
type PartialPoint = { x: number; } | { y: number; };
// FIXME: can not implement a union type
class SomePartialPoint implements PartialPoint {
x: 1;
y: 2;
}
</code></pre>
<h4 id="5-extends-class">5. extends class</h4>
<p>类定义会创建两个东西:类的实例类型和一个构造函数。 因为类可以创建出类型,所以你能够在允许使用接口的地方使用类。</p>
<pre><code>class Point {
x: number;
y: number;
}
interface Point3d extends Point {
z: number;
}
</code></pre>
<h4 id="6-declaration-merging">6. Declaration merging</h4>
<p>与类型别名不同,接口可以定义多次,并将被视为单个接口(合并所有声明的成员)。</p>
<pre><code>// These two declarations become:
// interface Point { x: number; y: number; }
interface Point { x: number; }
interface Point { y: number; }
const point: Point = { x: 1, y: 2 };
</code></pre>
<h4 id="7-计算属性生成映射类型">7. 计算属性,生成映射类型</h4>
<p>type 能使用 in 关键字生成映射类型,但 interface 不行。</p>
<p>语法与索引签名的语法类型,内部使用了 for .. in。 具有三个部分:</p>
<ul>
<li>类型变量 K,它会依次绑定到每个属性。</li>
<li>字符串字面量联合的 Keys,它包含了要迭代的属性名的集合。</li>
<li>属性的结果类型。</li>
</ul>
<pre><code>type Keys = "firstname" | "surname"
type DudeType = {
: string
}
const test: DudeType = {
firstname: "Pawel",
surname: "Grzybek"
}
// 报错
//interface DudeType2 {
//: string
//}
</code></pre>
<h4 id="7-其他细节">7. 其他细节</h4>
<pre><code>export default interface Config {
name: string
}
// export default type Config1 = {
// name: string
// }
// 会报错
type Config2 = {
name: string
}
export default Config2
</code></pre>
<h4 id="总结">总结</h4>
<p>interface 和 type 很像,很多场景,两者都能使用。但也有细微的差别:</p>
<ul>
<li>类型:对象、函数两者都适用,但是 type 可以用于基础类型、联合类型、元祖。</li>
<li>同名合并:interface 支持,type 不支持。</li>
<li>计算属性:type 支持, interface 不支持。</li>
</ul>
<p>总的来说,公共的用 interface 实现,不能用 interface 实现的再用 type 实现。主要是一个项目最好保持一致。</p>
<p>参考:</p>
<ul>
<li>中文文档的接口</li>
<li>中文文档的高级类型</li>
<li>中文文档的声明合并</li>
<li>Typescript: Interfaces vs Types</li>
<li>TYPESCRIPT INTERFACE VS. TYPE</li>
</ul><br><br>
来源:https://www.cnblogs.com/EnSnail/p/11233592.html
頁:
[1]