typescript中对象属性可选属性与只读属性与索引签名
可选属性
type类型别名
type 会给一个类型起个新名字。 type 有时和 interface 很像,但是可以作用于原始值(基本类型),联合类型,元组以及其它任何你需要手写的类型。
interface
TypeScript 的核心原则之一是对值所具有的结构进行类型检查。 而接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。
这段代码给Pearent 定义了三个属性---Shape,xPop,yPop
后面跟的 ? 代表的是可选也就是可传入可不传入。
再函数形参中我们进行xPop = 0设置的意义是如果不传入xPop,则默认值设为 0
interface Pearent {
Shape:Shape,
xPop?:number,
yPop?:number
}
###

function Pearent({Shape,xPop = 0,yPop = 0}:Pearent){
let xpop = xPop
console.log(" ~ file: 02-可选属性.ts ~ line 10 ~ Pearent ~ xPop", xpop)
}
Pearent({Shape:`小红`})
Pearent({Shape:1,xPop:1})
只读属性
在属性前面加上readonly 关键字,变成只读属性
interface Home {
readonly resident :{
age:number,
name:string
}
}
function visit(home:Home){
console.log(home.resident.age);
// 但是可以修改只读属性内部属性
home.resident.age ++
}
function evict (home:Home){
// 修改只读属性就会报错
// home.resident= {
// name:`小红`
// age:18
// }
}
可以将可写的属性赋值给不可写的属性,实现属性修改
// 可读属性
interface person{
name:string,
age:number
}
//不可读写属性
interface readonlyPerson{
readonly name:string,
readonly age:number
}
//可读写数据
let newPerson:person = {
name:`小红`,
age:18
}
//将可读写数据赋给不可读写数据
let newPersons:readonlyPerson = newPerson
console.log(newPersons.age);//18
newPerson.age ++
console.log(newPersons.age);//19
索引签名
索引签名在实际开发中经常用到,例如服务端接口返回了很多数据但是存在我们不需要用的,如果定义接口的时候不定义它们会报错,这时我们使用索引签名,就可以避免这个问题
注意:TypeScript 的索引签名必须是 string 或者 number。索引签名的名称(如:{ [index: string]: { message: string } } 里的 index )除了可读性外,并没有任何意义。例如:如果有一个用户名,你可以使用 { username: string}: { message: string },这有利于下一个开发者理解你的代码。当你声明一个索引签名时,所有明确的成员都必须符合索引签名:
interface qingTian {
[props:string]:number
}
let Top = {
x:1,
b:2
}
interface Animal {
name:string
}
interface Dog extends Animal{
befff : string
}
interface Monky {
[index:string]:number|string
name:string
length:number
}
let momky:Monky ={
name:`小花`,
length:2
}
interface readOnly {
readonly [index:number]:string
}
let myarr:readOnly = [`a`,`b`,`c`]
// myarr[0]= b
扩展类型
通过interface定义多个接口类型,用extends继承属性,如果多类型,可以在extend后面的类型中继续加,用‘ ,’逗号链接
interface Zoom {
name:string,
age:number,
color?:string
}
interface Top extends Zoom{
unit:string
}
let address:Top = {
name:`小红`,
unit:`小蓝`,
age:18
}
interface circular {
radius:number
}
interface length{
length:number
}
interface less extends circular,length{
ref:boolean
}
交叉类型
交叉类型其实和扩展类型差不多,都是让两个类型为一个类型或变量服务。
interface RNG {
Ming:string
}
interface OMG{
Fafer:string
}
// type LPL = OMG & RNG
// let LPL:LPL ={
// Fafer:`小红`,
// Ming:`小明`
// }
function LPL(LPL:RNG&OMG){
console.log(LPL.Fafer);
console.log(LPL.Ming);
}
LPL({
Ming:`小明`,
Fafer:`小李`
})
看上去交叉类型和扩展类型是相似的,但是他们也有一丝的差别,就是在处理冲突的时候。
interface 允许变量名相同,并且会把相同变量名的属性整合在一起。
type 不允许创建相同变量名
// interface Sister {
// name:string
// }
// interface Sister{
// age:number
// }
// let person:Sister ={
// name:`小红`,
// age:12
// }
type Sister ={
name:string
}
type Sister

泛型类型对象
//这样判断为any失去了定义类型的意义
interface x {
contens:any
}
let box:x ={
contens:`什么类型都可以`
}
//使用unkown
interface y{
contens:unknown
}
let y:y = {
contens:`Hello , word`
}
// 使用类型缩小
if(typeof y.contens===`string`){
console.log(y.contens.toLocaleLowerCase());
}
// 使用类型断言
console.log((y.contens as string).toLocaleLowerCase());
// 使用函数重载定义
interface numberBOX {
contents:number
}
interface stringBox{
contents:string
}
interface booleanBox{
contents:boolean
}
function setContents (box:numberBOX,newBox:number):void
function setContents (box:stringBox,newBox:string):void
function setContents (box:booleanBox,newBox:boolean):void
function setContents(box:{contents:any},newBox:any){
box.contents = newBox
}
// 使用泛型对象定义
interface Box<type> {
contents:type
}
interface Apple{
// ...
}
let a:Apple = {}
type AppleBox = Box<Apple>
let ab:AppleBox = {
contents:a
}
type Box1<type> = {
contents:type
}
type Oneornull<type> = type|null
type OneorMany<type> = type | type[]
typescript中对象属性可选属性与只读属性与索引签名的更多相关文章
- javascript中对象访问自身属性的方式
在javascript中,通过对象的方法访问对象自身属性时,必须采用this.fieldName的方式. 原因是javascript中Function是无状态的,访问对象的属性时,必须指定当前的上下文 ...
- C#中获取多个对象list中对象共有的属性项
场景 有一组数据list<TestDataList> 每一个TestDataList是一个对象,此对象可能有温度数据,也可能没有温度数据. 有温度数据的情况下,温度数据属性又是一个list ...
- java1.8对集合中对象的特有属性进行排序
每天学习一点点,知识财富涨点点 1.创建对象user12 2.编写测试类 3.输出结果 加油!!!!
- JS 取Json数据中对象特定属性值
解析JSON JSON 数据 var str = '[{"a": "1","b": "2"}, {"a&quo ...
- Structs2中iterator的status属性的用法
iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用<s:iterator/>标签的时候有三个属性值得我们关注 1. value属性:可选的属性,va ...
- JScript中的prototype(原型)属性研究
今天看到同事使用js中的Prototype,感觉很是新鲜.由此想深入学习一下prototype(英['prəʊtətaɪp] 美['protə'taɪp]n. 原型:标准,模范),在学习prototy ...
- 巧用call,appl有 根据对象某一属性求最大值
查找对象数组中某属性的最大最小值的快捷方法 例如要查找array数组中对象的value属性的最大值 var array=[ { "index_id": 119, "are ...
- VB类模块中属性的参数——VBA中Range对象的Value属性和Value2属性的一点区别
在VB中,属性是可以有参数的,而VBA中属性使用参数非常常见.比如最常用的:Worksheet.Range("A1:A10") VB的语法,使用参数的不一定是方法,也有可能是属性 ...
- JavaScript中对象的属性
在JavaScript中,属性决定了一个对象的状态,本文详细的研究了它们是如何工作的. 属性类型 JavaScript中有三种不同类型的属性:命名数据属性(named data properties) ...
随机推荐
- 03 uniapp自定义导航栏的开发
在我眼里自定义导航分2类: 原生基础上 || 非原生基础上 总结:项目当中能原生就原生,提高性能 区别 uni-app 自带原生导航栏,在pages.json里配置. 原生导航的体验更好,渲染新页面时 ...
- 可控线性序列机(查看除了inout端口外的其他变量的波形的方法)
可控线性序列机: 可控:有个控制端控制何时输出线性序列. 线性序列机:输出一个线性序列. 知识点: 1.包含多个判定条件时用英文()括起来,用&&连接. 2.使能端EN的设置(类似于D ...
- 什么是FastAPI异步框架?(全面了解)
一:FastAPI框架 1.FastAPI是应该用于构建API的现代,快速(高性能)的 web 框架,使用Python 3.6+ 并基于标准的 Python 类型提示. 关键性: 快速: 可与Node ...
- P3622 [APIO2007]【一本通提高状态压缩类动态规划】动物园
广告 绿树公司 - 官方网站:https://wangping-lvshu.github.io/LvshuNew/ 绿树智能 - 官方网站:https://wangping-lvshu.github. ...
- 安卓系统使用USB转串口
概述 安卓系统支持多种 USB 外围设备,提供两种模式来支持实现 USB 外设接入系统:USB 配件模式和 USB 主机模式. 在 USB 配件模式下,接入的 USB 设备充当 USB 主机,并为 U ...
- javascript打印对象函数
//js对象打印函数 function writeObj(obj) { var description = ""; for (var i in obj) { var propert ...
- netdata检测工具的安装与使用
Netdata 是一款 Linux 性能实时监测工具..以web的可视化方式展示系统及应用程序的实时运行状态(包括cpu.内存.硬盘输入/输出.网络等linux性能的数据). Netdata文档地址: ...
- Vue ref属性 && props配置项
1 // # ref属性: 2 // # 1.用来给元素或者子组件注册引用信息(id的替代者) 3 // # 2.应用在html标签上获取的是真实的DOM元素,应用在组件标签上是组件实例对象(vc) ...
- 源码解析springbatch的job是如何运行的?
202208-源码解析springbatch的job是如何运行的? 注,本文中的demo代码节选于图书<Spring Batch批处理框架>的配套源代码,并做并适配springboot升级 ...
- 【沥血整理】灰度(二值)图像重构算法及其应用(morphological reconstruction)。
不记得是怎么接触并最终研究这个课题的了,认识我的人都知道我是没有固定的研究对象的,一切看运气和当时的兴趣.本来研究完了就放在那里了,一直比较懒的去做总结,但是想一想似乎在网络上就没有看到关于这个方面的 ...