类型别名
类型别名是我们可以将自己定义的数据类型,定义一个别名,使用type修饰符
类型别名语法如下
ts
type 新类型名 = 原类型名下面是个简单的例子
ts
type Age = number
const age:Age = 181.1 修饰函数
ts
type niubi = (content: string) => string
type niubi2 = <T>(content: T) => T
// 使用
const fn1: niubi = content => {
return content
}
console.log(fn1('你好'))1.2 修饰对象
ts
type Item = {
name: string
age: number
gender: string
address?: string
}
const obj: Item = {
name: 'John',
age: 25,
gender: 'Male'
}1.3 修饰数组
ts
type Arr = number[]
type Arr1 = Array<number>
const arr: Arr = [1, 2, 3]
const arr1: Arr1 = [1, 2, 3]通常使用type定义数组包对象的类型
ts
interface Item{
value: number
label:string
}
type Items = Array<Item>