这是我参与「第四届青训营 」笔记创作活动的第6天
基础数据类型
变量的定义方法:
- 自动识别数据类型 const q = 'string' const w = 1 const e = true const r = null const t = undefined
- 手动确定数据类型 const q: strng = 'string' const w: number = 1 const e: boolean = true const r: null = null const t: undefined = undefined
对象类型
图中
readonly 为只读。初始化后不可赋值
bobby?:string 表示可以不存在,若存在则为string类型
any 表示可以为任意属性
IBytedance类型中,约定以I开头表示是一种类型。
函数类型
此代码段表示返回类型为number的x+y的值
function add(x: numbeir, y: number):number{
return x+y;
}
函数重载
数组类型
第一种和第二种定义数组的方式比较常用。
IArr4表示数组内的元素可以为任意类型的数组。
Typescript 泛型
P3中,第二段报错的原因是因为在以下代码中
const getRepratArr: IGerRepeat = target => new Array(100).fill(target)
IGerRepeat后没有给出变量类型,故默认参数number类型,故报错。
类型别名 & 类型断言
type 是一种类型别名