TypeScript基础入门 - 接口 - 类类型

转载地址node

TypeScript基础入门 - 接口 - 类类型

项目实践仓库git

https://github.com/durban89/typescript_demo.git
tag: 1.0.12

为了保证后面的学习演示须要安装下ts-node,这样后面的每一个操做都能直接运行看到输出的结果。github

npm install -D ts-node

后面本身在练习的时候能够这样使用typescript

npx ts-node src/learn_basic_types.ts
npx ts-node 脚本路径

接口

TypeScript的核心原则之一是对值所具备的结构进行类型检查。 它有时被称作“鸭式辨型法”或“结构性子类型化”。 在TypeScript里,接口的做用就是为这些类型命名和为你的代码或第三方代码定义契约。npm

类类型

实现接口

与C#或Java里接口的基本做用同样,TypeScript也可以用它来明确的强制一个类去符合某种契约。以下实现ide

interface SomeClassInterface {
    property1: string;
}

class implementSomeInterface implements SomeClassInterface {
    property1: string;
    constructor(arg1: number, arg2: number) {}
}

你也能够在接口中描述一个方法,在类里实现它,如同下面的setProperty1方法同样:函数

interface SomeClassInterface {
    property1: string;
    // 设置property1
    setProperty1(p: string): any;
}

class implementSomeInterface implements SomeClassInterface {
    property1: string;
    constructor(arg1: number, arg2: number) {}
    setProperty1(p: string): any {
        this.property1 = p;
    }
}


接口描述了类的公共部分,而不是公共和私有两部分。 它不会帮你检查类是否具备某些私有成员。学习

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

当你操做类和接口的时候,你要知道类是具备两个类型的:静态部分的类型和实例的类型。 你会注意到,当你用构造器签名去定义一个接口并试图定义一个类去实现这个接口时会获得一个错误:以下this

interface SomeConstructor {
    new (arg1: string, arg2: string): any
}

class SomeClass implements SomeConstructor {
    property1: string;
    constructor(arg1: string, arg2: string) {}
}

运行后报错误以下spa

⨯ Unable to compile TypeScript:
src/interface_7.ts(20,7): error TS2420: Class 'SomeClass' incorrectly implements interface 'SomeConstructor'.
  Type 'SomeClass' provides no match for the signature 'new (arg1: string, arg2: string): any'.

这里由于当一个类实现了一个接口时,只对其实例部分进行类型检查。 constructor存在于类的静态部分,因此不在检查的范围内。所以,咱们应该直接操做类的静态部分。 看下面的例子,咱们定义了两个接口,SomeConstructor为构造函数所用和SomeClassInterface为实例方法所用。 为了方便咱们定义一个构造函数createSomeFunc,它用传入的类型建立实例。

interface SomeClassInterface {
    property1: string;
    // 设置property1
    setProperty1(p: string): any;
    getProperty1(): string;
}

interface SomeConstructor {
    new(arg1: string, arg2: string): SomeClassInterface
}

function createSomeFunc(ctr: SomeConstructor, arg1: string, arg2: string): SomeClassInterface {
    return new ctr(arg1, arg2)
}

class ImplementSomeInterface1 implements SomeClassInterface {
    property1: string;
    property2: string;
    constructor(arg1: string, arg2: string) {
        this.property1 = arg1;
        this.property2 = arg2;
    }
    setProperty1(p: string): any {
        this.property1 = p;
    }
    
    getProperty1() {
        return this.property1 + ' = implementSomeInterface1';
    }
}

class ImplementSomeInterface2 implements SomeClassInterface {
    property1: string;
    property2: string;
    constructor(arg1: string, arg2: string) {
        this.property1 = arg1;
        this.property2 = arg2;
    }
    setProperty1(p: string): any {
        this.property1 = p;
    }
    
    getProperty1() {
        return this.property1 + ' = implementSomeInterface2';
    }
}

let instance1 = createSomeFunc(ImplementSomeInterface1, 'arg1', 'arg2');
let instance2 = createSomeFunc(ImplementSomeInterface2, 'arg1', 'arg2');
console.log(instance1.getProperty1());
console.log(instance2.getProperty1());

运行后获得的结果以下

arg1 = implementSomeInterface1
arg1 = implementSomeInterface2

由于createSomeFunc的第一个参数是SomeConstructor类型,在createSomeFunc(ImplementSomeInterface1, 'arg1', 'arg2')里,会检查ImplementSomeInterface1是否符合构造函数签名。

我以为这里的话官方写的有点复杂了,为何必定要使用一个构造函数接口呢,好比下面

let instance3 = new ImplementSomeInterface2('arg1','arg2')
console.log(instance3.getProperty1());

同样能够实现实例化,而且调用方法函数

本实例结束实践项目地址

https://github.com/durban89/typescript_demo.git
tag: 1.0.13
相关文章
相关标签/搜索