首页 > 其他分享 >typescript中的type和interface的区别

typescript中的type和interface的区别

时间:2023-02-23 00:33:16浏览次数:42  
标签:typescript string number MyType interface type name

// 1,写法不一样
type MyType = {
  name: string
  age: number
}
interface MyType {
  name: string;
  age: number
}

// 2,拓展方式不一样
type MyType = {
  name: string
  age: number
}
type MyType2 = MyType & {
  last: string
}

interface MyType {
  name: string;
  age: number
}
interface MyType2 extends MyType {
  last: string
}

// 3,interface可以重复定义拓展属性,type不行
interface MyType {
  name: string
}
interface MyType {
  age: number
}
/*
  等同于 interface MyType {
        name: string;
        age: number
  }
*/

type MyType = {
  name: string
}
type MyType = { // 报错
  age: number
}




 

标签:typescript,string,number,MyType,interface,type,name
From: https://www.cnblogs.com/itpyy/p/17146510.html

相关文章

  • Typecho——Argument 1 passed to Typecho\Router::get() must be of the type strin
    前言开发EasyBe主题的置顶功能时候,测试的时候出现了Typecho\Router::get()报错,根据对应的报错信息查看了下源码后解决了该问题;内容查询SQL//获取全部文章functionge......
  • 【TypeScript 4.5】007-第 7 章 类型操纵
    【TypeScript4.5】007-第7章类型操纵文章目录​​【TypeScript4.5】007-第7章类型操纵​​​​一、从类型中创建类型​​​​1、概述​​​​2、方法​​​​二、泛......
  • 【TypeScript 4.5】006-第 6 章 对象类型
    【TypeScript4.5】006-第6章对象类型文章目录​​【TypeScript4.5】006-第6章对象类型​​​​一、认识对象类型​​​​1、概述​​​​说明​​​​对象类型​​......
  • 【TypeScript 4.5】004-第 4 章 类型缩小
    【TypeScript4.5】004-第4章类型缩小文章目录​​【TypeScript4.5】004-第4章类型缩小​​​​一、typeof类型守卫​​​​1、什么是类型缩小​​​​含义​​​​......
  • 【TypeScript 4.5】003-第 3 章 常用类型
    【TypeScript4.5】003-第3章常用类型文章目录​​【TypeScript4.5】003-第3章常用类型​​​​一、基元类型string、number和boolean​​​​1、基本含义​​​......
  • 【TypeScript 4.5】001-第 1 章 TypeScript 介绍
    【TypeScript4.5】001-第1章TypeScript介绍文章目录​​【TypeScript4.5】001-第1章TypeScript介绍​​​​一、什么是TypeScript​​​​二、JS、ES以及TS的关......
  • 【TypeScript 4.5】002-第 2 章 TypeScript 入门
    【TypeScript4.5】002-第2章TypeScript入门文章目录​​【TypeScript4.5】002-第2章TypeScript入门​​​​一、发现问题​​​​1、字符串​​​​2、函数​​​......
  • 【TypeScript 4.5】005-第 5 章 函数
    【TypeScript4.5】005-第5章函数文章目录​​【TypeScript4.5】005-第5章函数​​​​一、函数类型表达式​​​​1、概述​​​​函数​​​​函数类型表达式​​......
  • 【TypeScript 编程】001-002 第 1 章 导言 与 第 2 章 TypeScript 概述
    【TypeScript编程】001-002第1章导言与第2章TypeScript概述文章目录​​【TypeScript编程】001-002第1章导言与第2章TypeScript概述​​​​第1章......
  • TypeScript--初识
    1.基础类型(注意:TypeScript和JavaScript没有整数类型)1.1 任意类型anyletx:any=1;letarrayList:any[]=[1,false,'fine'];1.2数字类型 numberletbinaryLit......