接口

TS的核心原则之一是对值所具有的结构进行类型检测

接口初探

function printLabel(labelledObj: { label: string }) {
console.log(labelledObj.label);
} let myObj = { size: , label: "Size 10 Object" };
printLabel(myObj);

可选属性

  width?: number

interface SquareConfig {
color?: string
width?: number
} function createSquare(config: SquareConfig): { color: string, area: number } {
let newSquare = { color: 'white', area: }
if (config.color) { newSquare.color = config.color }
if (config.width) { newSquare.area = config.width * config.width }
return newSquare
} let mySquare = createSquare({ color: 'red' }) console.log(mySquare)

只读属性

  readonly x: number

interface Point {
readonly x: number
readonly y: number
} let p1: Point = { x: , y: }
p1.x = // error

  readonly  vs  const

    如果是变量那么使用const , 如果是属性那么使用readonly

额外的属性检测

  [propName: string]: any

interface Square {
color?: string
width?: number
[propName: string]: any
} function createSquare(config: Square): {color: string, area: number} {
let mySquare = {color: 'white', area: }
if (config.color) {
mySquare.color = config.color
}
if (config.width) {
mySquare.area = config.width * config.width
}
return mySquare
} createSquare({colur: 'black', width: })

函数类型

interface SearchFunc {
(source: string, subString: string) : boolean
} let mySearch: SearchFunc
mySearch = function(src: string, str: string) {
let result = src.search(str)
return result > -
}

可索引的类型

interface StringArray {
[index: number]: string
} let myArray: StringArray
myArray = ['Bob', "Fred"] let myStr: string = myArray[]

类类型

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

继承接口

interface Shape {
color: string
} interface Square extends Shape {
sideLen: number
} let square = {} as Square
square.color = 'red'
square.sideLen =

混合类型

interface Counter {
(start: number): string
interval: number
reset(): void
} function getCounter(): Counter {
let counter = (function (start: number) { }) as Counter
counter.interval =
counter.reset = function () { }
return counter
} let c = getCounter()
c()
c.reset()
c.interval = 4.9

TS-接口的更多相关文章

  1. Typescript基础(4)——接口

    前言 今天继续typescript的学习,开始ts接口部分的学习. 接口 接口的理解 首先,我们谈论一下现实生活中的接口.比如生活中常用的插座接口,有些插头是三孔插座的,有些是两孔插座的.插座接口规定 ...

  2. 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践

    1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择  ...

  3. 用Vue3构建企业级前端应用,TS能让你更轻松点

    摘要:Vue 3已经发布有一段时间了,到底有哪些新特性值得关注,如何用它构建企业级前端项目,怎样快速上手Vue 3?本篇文章将对此进行详细讲解. 前言 工欲善其事,必先利其器 --<论语> ...

  4. Angular2 Service实践——实现简单音乐播放服务

    引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...

  5. Angular2 Service实践

    引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...

  6. angualr4 路由 总结笔记

    使用cli命令创建根路由模块 ng g cl app.router 或自己建一个路由配置文件 如:app/app.router.ts // app/app.router.ts // 将文件修改为 im ...

  7. vscode 搭建react-native

    vscode 搭建react-native 选择:vscode + typings + eslint * vscode: 宇宙最强IDE家族的最新产品 * typings: 基于typescirpt的 ...

  8. typscript 语法1

    let isDone: boolean = false; let decLiteral: number = 0xf00d; let names: string = 'boob'; /** 使用模版字符 ...

  9. 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述

    定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...

  10. 【Vuejs】301- Vue 3.0前的 TypeScript 最佳入门实践

    前言 我个人对更严格类型限制没有积极的看法,毕竟各类转类型的骚写法写习惯了. 然鹅最近的一个项目中,是 TypeScript+ Vue,毛计喇,学之...-真香! 1. 使用官方脚手架构建 npm i ...

随机推荐

  1. android5.1 隐藏状态栏

    修改frameworks/base/core/res/res/values/dimens.xml文件中 <!-- Height of the status bar --> <!-- ...

  2. Manjaro配置中国源

    1.自动寻找中国源 sudo pacman-mirrors -i -c China -m rank//更新镜像排名sudo vim /etc/pacman.d/mirrorlist //查看选择的源s ...

  3. MySql进行批量插入时的几种sql写法

    insert into:插入数据,如果主键重复,则报错 insert repalce:插入替换数据,如果存在主键或unique数据则替换数据 insert ignore:如果存在数据,则忽略. INS ...

  4. MySQL练习题--sqlzoo刷题2

    SELECT from Nobel Tutorial 1.Change the query shown so that it displays Nobel prizes for 1950. SELEC ...

  5. 【leetcode】667. Beautiful Arrangement II

    题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...

  6. Repeatable Read

    在Repeatable Read隔离级别下,一个事务可能会遇到幻读(Phantom Read)的问题. 幻读是指,在一个事务中,第一次查询某条记录,发现没有,但是,当试图更新这条不存在的记录时,竟然能 ...

  7. Shiro学习(8)拦截器机制

    8.1 拦截器介绍 Shiro使用了与Servlet一样的Filter接口进行扩展:所以如果对Filter不熟悉可以参考<Servlet3.1规范>http://www.iteye.com ...

  8. 思维——cf1238C

    听思维的一道题,网上大多直接模拟,感觉有点麻烦,其实只要把连续段求出来,然后处理一下统计答案就行 两个注意点:1.除了第一个连续段,其余段长度都要+1 2.因为目的地是0,所以最后一段要特判一下 #i ...

  9. js设计模式——5.状态模式

    js设计模式——5.状态模式 代码演示 /*js设计模式——状态模式*/ // 状态(红灯,黄灯,绿灯) class State { constructor(color) { this.color = ...

  10. SQL Server2012 安装方法详解

    SQL Server2012 安装方法详解 - MonkeyBrothers的博客 - CSDN博客 https://blog.csdn.net/monkeybrothers/article/deta ...