TS-接口
接口
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-接口的更多相关文章
- Typescript基础(4)——接口
前言 今天继续typescript的学习,开始ts接口部分的学习. 接口 接口的理解 首先,我们谈论一下现实生活中的接口.比如生活中常用的插座接口,有些插头是三孔插座的,有些是两孔插座的.插座接口规定 ...
- 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践
1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择 ...
- 用Vue3构建企业级前端应用,TS能让你更轻松点
摘要:Vue 3已经发布有一段时间了,到底有哪些新特性值得关注,如何用它构建企业级前端项目,怎样快速上手Vue 3?本篇文章将对此进行详细讲解. 前言 工欲善其事,必先利其器 --<论语> ...
- Angular2 Service实践——实现简单音乐播放服务
引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...
- Angular2 Service实践
引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...
- angualr4 路由 总结笔记
使用cli命令创建根路由模块 ng g cl app.router 或自己建一个路由配置文件 如:app/app.router.ts // app/app.router.ts // 将文件修改为 im ...
- vscode 搭建react-native
vscode 搭建react-native 选择:vscode + typings + eslint * vscode: 宇宙最强IDE家族的最新产品 * typings: 基于typescirpt的 ...
- typscript 语法1
let isDone: boolean = false; let decLiteral: number = 0xf00d; let names: string = 'boob'; /** 使用模版字符 ...
- 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述
定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...
- 【Vuejs】301- Vue 3.0前的 TypeScript 最佳入门实践
前言 我个人对更严格类型限制没有积极的看法,毕竟各类转类型的骚写法写习惯了. 然鹅最近的一个项目中,是 TypeScript+ Vue,毛计喇,学之...-真香! 1. 使用官方脚手架构建 npm i ...
随机推荐
- myeclipse 报错:Set project compiler compliance settings to '1.5'
myeclipse 报错:Set project compiler compliance settings to '1.5' 解决方法:项目右击-->properties-->java c ...
- 2018-10-19-C#-GUID-ToString-
title author date CreateTime categories C# GUID ToString lindexi 2018-10-19 9:4:44 +0800 2018-4-1 10 ...
- Linux解压rar文件
Linux解压rar文件(unrar安装和使用,分卷解压) windows平台很多压缩文档为rar文件,那么怎么做到Linux解压rar文件(unrar安装和使用)? 简单,centos5安装unra ...
- Shell: 文本文件操作
文件显示和信息 wc wc 可以用于统计文件的行数和单词数. nl nl 在文件的每行内容前面加上行号. 基于行的操作 grep grep 用于筛选匹配特定字符的行. grep "Hello ...
- ZOJ3531: [SDOI2014] 旅行
Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. ...
- 2019华工校赛 B - 修仙时在做什么?有没有空?可以来炼丹吗?
题目链接:https://ac.nowcoder.com/acm/contest/625/B 解法:这题其实就是求2^18个点内最近的两个点的距离.我们可以容易想到朴素解法:把每个点作为源点跑最短路取 ...
- cmake 加入调试信息
1 首先在CMakeLists.txt中加入 SET(CMAKE_BUILD_TYPE "Debug")1在下面加入: SET(CMAKE_CXX_FLAGS_DEBUG &quo ...
- Linux系统关闭对ping命令做响应。
1.测试 ping 192.168.10.5 可以正常ping通, 2,修改 /proc/sys/net/ipv4/icmp_echo_ignore_all 文件的值=1 3.在测试 已经ping不 ...
- hdu 5791 思维dp
题目描述: 求序列A,B的公共子序列个数: 基本思路: 想到了dp,选的状态也对,但是就是就是写不出状态转移方程,然后他们都出了,到最后我还是没出,很难受,然后主要是没有仔细考虑dp[i][j],dp ...
- php高版本安装ecshop错误解决方法
1.Strict Standards: Non-static method cls_image::gd_version() should not be called statically in F:\ ...