[TOC] ## 概述 `Person`没有明确说明是基于`Named`的,但是有也不错报错 ``` interface Named { name: string; } class Person { name: string; } let p: Named; // OK, because of structural typing p = new Person(); ``` ## 符合接口的赋值 只要变量符合接口的属性即可,额外属性不会报错 ``` interface Named { name: string; } let x: Named; // y's inferred type is { name: string; location: string; } let y = { name: 'Alice', location: 'Seattle' }; x = y; ``` ## 比较两个函数 额外参数在 js 中非常常见 ``` let x = (a: number) => 0; let y = (b: number, s: string) => 0; y = x; // OK x = y; // Error ``` 返回值比较 ``` let x = () => ({name: 'Alice'}); let y = () => ({name: 'Alice', location: 'Seattle'}); x = y; // OK y = x; // Error, because x() lacks a location property ``` ## 函数重载 ### 枚举 枚举类型与数字类型兼容,并且数字类型与枚举类型兼容。不同枚举类型之间是不兼容的 ``` enum Status { Ready, Waiting }; enum Color { Red, Blue, Green }; let status = Status.Ready; status = Color.Green; // Error ```