接口(Interfaces)

One of TypeScript's core principles is that type-checking focuses on the 'shape' that values have. This is sometimes called "duck typing" or "structural subtyping". In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.

实在水平有限,不知道怎么翻译。

大概的意思应该是,typescript对类型检查是对"形状"来判断的。用函数举个例子说,就是只要参数列表被调用的函数所包含,就算是匹配了。

你在项目内外编写的代码只要符合接口定义的协议,就都能识别。

我们的第一个接口

我们通过一个简单的例子来看接口是如何工作的:

function printLabel(labelledObj: {label: string}) {
console.log(labelledObj.label);
} var myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

The type-checker checks the call to 'printLabel'. The 'printLabel' function has a single parameter that requires that the object passed in has a property called 'label' of type string. Notice that our object actually has more properties than this, but the compiler only checks to that at least the ones required are present and match the types required.

调用'printLabel'时,类型检查器进行检查。'printLabel'函数需要传递一个参数,这个参数是一个对象(labelledObj),并且有一个字符串的属性——"label"。

注意,我们的这个对象实际上可能不止这一个属性,但是编译器只会检查当前这个对象至少符合接口要求的类型,即包含一个label的字符串类型的属性。

我们再以同一个例子,这次使用一个接口来描述需要一个字符串类型的label属性:

interface LabelledValue {
label: string;
} function printLabel(labelledObj: LabelledValue) {
console.log(labelledObj.label);
} var myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

The interface 'LabelledValue' is a name we can now use to describe the requirement in the previous example. It still represents having a single property called 'label' that is of type string. Notice we didn't have to explicitly say that the object we pass to 'printLabel' implements this interface like we might have to in other languages. Here, it's only the shape that matters. If the object we pass to the function meets the requirements listed, then it's allowed.

'LabelledValue'是接口的名字,这个接口用来描述前面那个例子的要求。它表示的仍然是一个叫做label的字符串类型的属性。

注意,我们没有像其他语言那样,明确说这个对象实现了LabelledValue接口。在这里,重要的只是“形状”(shape)。

如果这个对象符合函数的要求,那么就是允许的。

It's worth pointing out that the type-checker does not require that these properties come in any sort of order, only that the properties the interface requires are present and have the required type.

值得指出的是:类型检查并不要求属性的顺序,只要这个属性符合接口的要求,并且类型匹配,那么就是允许的。

可选属性(Optional Properties)

Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional properties are popular when creating patterns like "option bags" where the user passes an object to a function that only has a couple properties filled in.

并不是所有的属性都是接口要求的。某些存在的条件不是必须的。当用户传递一个对象到一个只有具有一父类拥有的属性的函数时,这些可选属性很受欢迎。

下面是这个模式的例子:

interface SquareConfig {
color?: string;
width?: number;
} function createSquare(config: SquareConfig): {color: string; area: number} {
var newSquare = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
} var mySquare = createSquare({color: "black"});

Interfaces with optional properties are written similar to other interfaces, which each optional property denoted with a '?' as part of the property declaration.

有可选属性的接口在编码上与其他接口类似,每个可选属性在属性声明时用一个 '?'来表示。

The advantage of optional properties is that you can describe these possibly available properties while still also catching properties that you know are not expected to be available. For example, had we mistyped the name of the property we passed to 'createSquare', we would get an error message letting us know:

可选属性的优点是,您可以描述这些可能可用的属性,同时还可以检查您知道不需要使用的属性。例如假定传递给'createSquare'的属性名称拼写错误,则会得到下面错误消息:

interface SquareConfig {
color?: string;
width?: number;
} function createSquare(config: SquareConfig): {color: string; area: number} {
var newSquare = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.collor; // Type-checker can catch the mistyped name here
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
} var mySquare = createSquare({color: "black"});

TypeScript Handbook 2——接口1(翻译)的更多相关文章

  1. typescript handbook 学习笔记4

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...

  2. typescript handbook 学习笔记3

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...

  3. typescript中的接口

    说到接口:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据,也不关心 ...

  4. typescript handbook 学习笔记2

    概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...

  5. 【TypeScript】TypeScript 学习 2——接口

    在 TypeScript 中,接口是用作约束作用的,在编译成 JavaScript 的时候,所有的接口都会被擦除掉,因为 JavaScript 中并没有接口这一概念. 先看看一个简单的例子: func ...

  6. TypeScript完全解读(26课时)_4.TypeScript完全解读-接口

    4.TypeScript完全解读-接口 初始化tslint tslint --init:初始化完成后会生成tslint.json的文件 如果我们涉及到一些规则都会在这个rules里面进行配置 安装ts ...

  7. typescript属性类型接口

    /* typeScript中的接口 - 1.属性类接口 */ /* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定 ...

  8. 探索typescript的必经之路-----接口(interface)

    TypeScript定义接口 熟悉编程语言的同学都知道,接口(interface)的重要性不言而喻. 很多内容都会运用到接口.typescrip中的接口类似于java,同时还增加了更灵活的接口类型,包 ...

  9. TypeScript 高级类型 接口(interface)

    在代码的实现或者调用上能设定一定的限制和规范,就像契约一样.通常,我们把这种契约称为接口. TypeScript的核心原则之一是对值所具有的结构进行类型检查. 有时称为“鸭式辨型法”或“结构性子类型化 ...

随机推荐

  1. git clone --early EOF

    出现这个问题可能需要重新检查以下方面: 1. Android studio Git 的安装地址:  ..../Git/cmd/git.exe 记得在环境变量 --Path 中进行配置: ,..../G ...

  2. android JNI开发

    1.NDK简介 NDK(Native Development Kit)NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk.NDK集成了交叉 ...

  3. SPSS数据分析—最优尺度回归

    在之前介绍的线性回归模型中,有一个隐含的假设是自变量均为连续变量,但实际上自变量有时候是分类变量,类似于方差分析中的因素,这种分类自变量在回归分析中,也默认作为连续变量使用,这就会产生一个问题,如果是 ...

  4. json转实体类

    VS快速生成JSON数据格式对应的实体 Json生成类在线生成工具 http://tool.sufeinet.com/Creater/JsonClassGenerator.aspx http://js ...

  5. H5如何实现一行三列布局

    <!DOCTYPE html><html lang="en"><head>         <meta charset="UTF ...

  6. Java开发工具安装步骤内容如下

    Java开发工具安装步骤内容如下 安装 开发工具 STS 链接下载网址 eclipse 链接下载网址 JDK安装 jdk链接下载地址 Marven环境 marven链接下载地址 Tomcat tomc ...

  7. SwitchHosts—hosts管理利器

    SwitchHosts是一个管理.快速切换Hosts小工具,开源软件,一键切换Hosts配置,非常实用,高效.开发Web过程成,部署有多套环境,网址域名都相同,部署在不同的服务器上,有开发环境.测试环 ...

  8. [CentOS Server] Bug when calling matlab in bash

    尝试了好几遍,仍然不能用简写命令调用matlab,这里把过程记录如下. (1). 登录 server [She@She ~]$ ssh shecl@xx.xx.xx.xx Last :: from x ...

  9. 浅谈学习掌握linux系统的优势

    Linux系统让我们懂得了共享.开放.自由可以让人类生活的更加美好,开源精神是一种让每个从事Linux行业的技术人员从骨子里自豪的情怀,开源产品的兴盛受益于开源社区的强壮根基.Linux真的给了我很多 ...

  10. [字符哈希] POJ 3094 Quicksum

    Quicksum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16488   Accepted: 11453 Descri ...