首页 > 其他分享 >TypeScript 之 Interface

TypeScript 之 Interface

时间:2022-12-05 21:33:09浏览次数:48  
标签:TypeScript string JsonResponse number content interface Interface size

Interface

描述:用来描述对象的形状,能够被继承

常用语法 ( Common Syntax )

1. 描述普通对象

interface JsonResponse {
  version:number;
  outOfStock?: boolean;
  readonly body: string;
  update: (retryTimes: number) => void;
  update2(retryTimes: number):void
}
interface JsonResponse2 { [key: string]: number }

2. 描述函数

上个例子中,我们描述的是一个对象中拥有的一些属性。Interface 也可以直接来描述一个函数。

因为在 JS 中,一切皆是对象,函数在 JS 中也是对象,可以拥有属性,并且可以被调用。

interface JsonResponse {
  (): string;
  toFn: string
}
const fn: JsonResponse = () => {
  return 'str'
}
fn.toFn = 'content'

3. 描述构造函数

还未搞懂...,有看到此随笔的朋友,推荐下相关链接

标签:TypeScript,string,JsonResponse,number,content,interface,Interface,size
From: https://www.cnblogs.com/lxrNote/p/16953606.html

相关文章

  • USB_VENDOR_AND_INTERFACE_INFO
    {USB_VENDOR_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID,0xff,0xff,0xff)},{USB_VENDOR_AND_INTERFACE_INFO(HUAWEI_VENDOR_ID,0xff,0x01,0x01)},{USB_VENDOR_A......
  • [Typescript] 127. Hard - Assign
    Youhaveatargetobjectandasourcearrayofobjects.Youneedtocopypropertyfromsourcetotarget,ifithasthesamepropertyasthesource,youshould......
  • 我要涨知识——TypeScript 常见面试题(二)
    又是一个年底来了,好大一批人可能又准备跑路了,最近回家待产话不多说,赶紧开干,给自己整了一个前端面试小助手——微信小程序内搜索“WEB学习学习加油站”,整理了前端经典高频......
  • TS中interface和type的区别
    1.概念1.接口(interface)接口主要用于类型检查,他只是一个结构契约,定义了具有相似的名称和类型的对象结构。除此之外,接口还可以定义方法和事件。2.类型别名(TypeAlias)不同......
  • [Typescript] 126. Hard - Two Sum
    Givenanarrayofintegers nums andaninteger target,returntrueiftwonumberssuchthattheyaddupto target./*_____________YourCodeHere________......
  • Typescript类型题材 - NumberRange
    题目中文有时我们需要限制数字的范围...示例:typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9EnglishSometimeswewanttolimittheran......
  • Typescript类型题材 - ConstructTuple
    题目中文构造一个给定长度的元组。例如typeresult=ConstructTuple<2>//期望得到[unknown,unkonwn]EnglishConstructatuplewithagivenlength.Forexam......
  • TypeScript中使用NodeJs日期格式化库myjs-common
    依赖包安装#安装myjs-common包[email protected]格式器表达式YEAR_FORMAT:年格式化-yyyyMONTH_FORMAT:月格式化-yyyy-MMDATE_FORMAT:日期格式化-yyyy-MM-ddH......
  • [Typescript] 125. Extreme - Object Key Paths
    Getallpossiblepathsthatcouldbecalledby _.get (alodashfunction)togetthevalueofanobjecttypeT1=ObjectKeyPaths<{name:string;age:number......
  • TypeScript 常见方法
    TypeScriptArray(数组)数组对象是使用单独的变量名来存储一系列的值。数组非常常用。假如你有一组数据(例如:网站名字),存在单独变量如下所示varsite1="Google";varsite2......