TypeScript interface

面试题库
• 阅读 5878
typescript之旅

1.TypeScript-Basic
2.TypeScript interface
3.Typescript-module(1)
4.TypeScript Modules(2)
5.Typescript tsconfig
6.TypeScript Functions
7.Typescript Class

Interfaces

今天来说说接口,首先说明如果你是java程序员,请一定忘记interface,此处的接口和彼处接口完全不是一个思想。

首先来一个最简单的接口
字面量接口

不使用interface关键字就定义了一个接口

function printLabel(labelledObj: {label: string}) {
  console.log(labelledObj.label);
}

var myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

上面没有interface关键字,哪个是接口呢?
{label: string}
你说这个玩意不是变量labelledObj的类型吗?我知道typescript说白了就是js的类型系统,前面也介绍了如:Boolean,Number,String,Array,Enum,Any,Void
其实接口就是定义了一个对象有哪些属性,并且属性值是什么类型

好了,来个高级点的(interface关键词上场)

interface LabelledValue {
  label: string;
}

function printLabel(labelledObj: LabelledValue) {
  console.log(labelledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

OK,懂了吗,自定义类型
啊,太棒了,以后再也不用判断某个属性是否un

可选属性

interface SquareConfig {
  color?: string;
  width?: number;
}

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

let mySquare = createSquare({color: "black"});

好处:

  1. 预定义属性

  2. 编译器可以捕获对不存在属性引用的错误

interface SquareConfig {
  color?: string;
  width?: number;
}

function createSquare(config: SquareConfig): { color: string; area: number } {
  let newSquare = {color: "white", area: 100};
  if (config.color) {
    // Error: Property 'collor' does not exist on type 'SquareConfig'
    newSquare.color = config.collor;  // Type-checker can catch the mistyped name here
  }
  if (config.width) {
    newSquare.area = config.width * config.width;
  }
  return newSquare;
}

let mySquare = createSquare({color: "black"});

对于这个错误例子,我们引出了下一节

额外的属性检查

上面的例子也许有人会争辩(js的忠实用户者),js对象是灵活的,你现在搞得跟JAVA一样了!
反对~反对!

是的,typescript对对象字面量会特殊对待(即经过额外属性检查),当将他们赋值给变量或作为函数参数时。
因此你会得到一个错误

// error: 'colour' not expected in type 'SquareConfig'    
let mySquare = createSquare({ colour: "red", width: 100 });

解决办法
1.类型断言法(类似于c语言的强制类型转换,告诉编译器,我就要转,别给我报错啊!)

let mySquare = createSquare({ colour: "red", width: 100 } as SquareConfig);

2.钻空子法(我们了解编译器的检查方式了,我绕过去就行了)

let squareOptions = { colour: "red", width: 100 };
let mySquare = createSquare(squareOptions);

函数类型

首先我们要肯定interface使我们定义了各种对象外形。
现在我们把string,number,boolean,object类型都定义了,完工了!
且慢,等等~对于其他语言是够了,但js~~~
你想,js的函数是一等公民,你能忘了它~

当当当当,来了!

interface SearchFunc {
  (source: string, subString: string): boolean;
}

demo:

let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
  let result = source.search(subString);
  if (result == -1) {
    return false;
  }
  else {
    return true;
  }
}

好处:
1.检查参数类型
2.返回值类型检查,上例,如果你返回字符串或数字,就报错了

数组类型

数组可以想象成一种特殊函数,只有一个参数(index)的函数

interface StringArray {
  [index: number]: string;
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

OK,没有问题吗?index只能是number类型吗?我还知道一种叫关联数组的玩意(index是string)

我们经常这样用obj.property和obj["property"]

  1. 目前支持两种index类型:number,string

  2. 可以同时使用两种索引类型

  3. 限制:数字索引返回值的类型必须是字符串索引返回值的类型的子类型

demo

interface NumberDictionary {
  [index: string]: number;
  length: number;    // 可以,length是number类型
  name: string       // 错误,`name`的类型不是索引类型的子类型
}

类类型(肩负重任的inteface关键字)

  • 与C#或Java里接口的基本作用一样,TypeScript也能够用它来明确的强制一个类去符合某种契约。

interface ClockInterface {
    currentTime: Date;
}

class Clock implements ClockInterface {
    currentTime: Date;
    constructor(h: number, m: number) { }
}
interface ClockInterface {
    currentTime: Date;
    setTime(d: Date);
}

class Clock implements ClockInterface {
    currentTime: Date;
    setTime(d: Date) {
        this.currentTime = d;
    }
    constructor(h: number, m: number) { }
}
  • 接口描述了类的公共部分,而不是公共和私有两部分。 它不会帮你检查类是否具有某些私有成员。

类静态部分与实例部分的区别

  • 这部分比较冷~

  • 类是具有两个类型的:静态部分的类型和实例的类型

  • 构造函数是静态部分,而一个类去实现一个带有构造器签名的接口时会报错

interface ClockConstructor {
    new (hour: number, minute: number);
}

class Clock implements ClockConstructor {
    currentTime: Date;
    constructor(h: number, m: number) { }
}

那如何才能使用接口对构造器类型检查呢

interface ClockConstructor {
    new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
    tick();
}

function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
    return new ctor(hour, minute);
}

class DigitalClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("beep beep");
    }
}
class AnalogClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("tick tock");
    }
}

let digital = createClock(DigitalClock, 12, 17);
let analog = createClock(AnalogClock, 7, 32);

拓展接口

  • 和类一样,接口也可以相互扩展。 这让我们能够从一个接口里复制成员到另一个接口里,可以更灵活地将接口分割到可重用的模块里

interface Shape {
    color: string;
}

interface Square extends Shape {
    sideLength: number;
}

let square = <Square>{};
square.color = "blue";
square.sideLength = 10;

多继承

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;

混合类型

  • 还是JS的特殊语法

function demo(){
    ...
}

demo.attr1 = '1';
demo.fun = function(){
    ...
}

这是什么情况,函数类型,对象类型,额,混合类型

interface Counter {
    (start: number): string;
    interval: number;
    reset(): void;
}

function getCounter(): Counter {
    let counter = <Counter>function (start: number) { };
    counter.interval = 123;
    counter.reset = function () { };
    return counter;
}

let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;

接口继承类

  • 还不是很明白

点赞
收藏
评论区
推荐文章
皮卡皮卡皮 皮卡皮卡皮
4年前
了解什么是 TypeScript
内容纲要了解什么是TypeScriptTypeScript基本语法TypeScript介绍TypeScript是什么TypeScript是JavaScript的强类型版本。然后在编译期去掉类型和特有语法,生成纯粹的JavaScript代码。由于最终在浏览器中运行的仍然是JavaScript,所以TypeScript并
Easter79 Easter79
3年前
vue+ts打造一个酷炫的星空聊天室
😛闲暇时间想做一个聊天室复盘一下这些年学习到的技术,于是在2020年6月24号就开始了Genal聊天室的开发之旅。😈项目采用全typescript开发,这是为了以后的功能迭代打基础。当然,我本身也是很喜欢typescript的。项目界面 !(https://oscimg.o
洛竹 洛竹
4年前
向微软官方贡献 @types 包
在前端社区中,TypeScript差不多是老生常谈的主题了。这不仅反映了TypeScript的流行度,也反映了它的学习上手成本。今天我们不来探讨TypeScript本身。而是记录一下我艰难地发布一个包的历程。ayearago<imgsrc"https://p9juejin.byteimg.com/toscnik3u1fbp
Wesley13 Wesley13
3年前
unity3d支持typescript开发(六)
目录1.unity3d支持typescript开发(一)(https://my.oschina.net/ahl5esoft/blog/4732529"unity3d支持typescript开发(一)")2.unity3d支持typescript开发(二)(https://my.oschina.net/ahl5esoft/blog
Easter79 Easter79
3年前
TypeScript 4.0正式发布!现在是开始使用它的最佳时机
!(https://oscimg.oschina.net/oscnet/9d5dde21c00d49b6b148bfabfc93db57.jpg)作者|DanielRosenwasser译者|王强策划|李俊辰稿源|前端之巅微软宣布TypeScript4.0正式版上线了!这一新版本深入改进了表现力、生产力
Easter79 Easter79
3年前
TypeScript学习笔记
TypeScript学习笔记TypeScript是JavaScript的超集,任何合法的JS程序都是合法的TypeScript程序TypeScript通过向JavaScript增加可选的静态类型声明来把JavaScript变成强类型程序语言。静态类型声明可约束函数、变量、属性等程序实体。TypeScript语言内
Easter79 Easter79
3年前
TypeScript系列1
1\.简介  随着PC端快速向移动端迁移,移动(体验)优先的概念也越来越响。由于ReactJS目前移动端仅仅支持iOS,因此移动端Web开发框架只能选择:AngularJS/Angula2Ionic框架Cordova。想要学习好Angula2以及阅读其代码,就必须了解和学习TypeScript,也因此需要学习好ES6以
面试题库
面试题库
Lv1
门有车马客,驾言发故乡。
文章
3
粉丝
0
获赞
0