[TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types相关的知识,希望对你有一定的参考价值。

TypeScript has ‘interface‘ and ‘type‘, so when to use which?

 

interface hasName {
    firstName: string;
    lastName: string;
}

interface hasAddress {
    address: string
}

type Player = (hasName & hasAddress) | null;

let player: Player = {firstName: Joe, lastName: Jonse, address: USA};

    

 

It is recommended that to use ‘interface‘ to define the props that obj should have.

‘type‘ is recommended to use when combine multi interfaces to descide a single object like what we show in example.

以上是关于[TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types的主要内容,如果未能解决你的问题,请参考以下文章

TypeScript-泛型

typescript 创建二维数组

使用 typescript 在类级别应用 scss 值

TypeScript 函数基础

TypeScript 函数基础

如何将数据组表示为通用 Typescript 接口