TS 语法
TS变量的声明
var/let/const 标识符:数据类型 = 赋值;
var name: string = "abc"
let age: number = 18
const height: number = 1.88
export {}
有时候我们会发现,在数据类型中,会有大写的String,也会有小写的string,区别是什么?
小写string:TypeScript中的字符串类型
大写String:JavaScript的字符串包装类的类型,是一个类
在TS中如果const msg: String = "test"是错误的,他不是一个类。
特殊的:let foo = "foo"没有添加类型注解,但是我为他赋值了一个字符串,默认情况下进行赋值时,会将赋值的值的类型,作为前面标识符的类型。这个过程称为:类型推导/推断
JS和TS相同的数据类型
number类型
boolean类型
string类型
Array类型
Array:数组
数组里面存放的数据类型最好是固定的,不要存不同的类型
首先要确定一个事实:names是一个数组类型,但是数组中存放的是什么类型的元素呢?
1.数组类型并且存放的元素是string类型(用泛型指定元素类型)
const names: Array<string> = [] //不推荐
2.第二种写法
const names2: string[] = [] //推荐
对象
数据类型其实默认是可以推导出来的,也可以自己指定
const info = {
name: "why",
age: 18
}
console.log(info.name)
null和undefined
const n1: null = null //null类型只有一个值:null
const n2: undefined = undefined //只有一个值
symbol类型
对象里面不允许两个相同的key
但是我可以使用Symbol避免
const title1 = Symbol("title")
const title2 = Symbol("title")
const info = {
[title1]:"帅哥",
[title2]:"美女",
}
export {}
TS特有的数据类型
any类型
当我们进行类型断言/偷懒时,会用到any类型,用any就相当于使用了JS原生代码了,那还用啥TS
const message: any = "GG"
message = 123//我重新赋值成number也是可以的
message = true
const arr: any[] = [] // 不推荐
unknown类型
function foo(){
return "abc"
}
function bar(){
return 123
}
let flag = true
//因为flag的值会变,所以我的result不能确定是字符串还是数值
//这个时候就要用到unknown类型
let result: unknown
if(flag){
return = foo()
}else {
return bar()
}
unknown:只能赋值给any和unknown类型
我将unknown类型赋值给string,就会报错
因为我不允许一个不确定的值,被你拿去乱用
let result: unknown
let msg: string = result
any:只能赋值给任意类型
我将any类型赋值给string(其他任意类型),没有报错
let result: any
let msg: string = result
void类型(了解)
//函数没有返回值 就是void类型,不写默认帮你写了
//其实没有返回值,默认返回的是一个undefined
function sum(num1,num2): void{
console.log(num1+num2)
}
sum(20,30)
//sum("abc","cba")
never类型
tuple类型
tuple元组:多种元素的组合
因为数组我们不推荐存放多种不同的数据类型,对象的键值对形式写的又特别麻烦,这个时候元组就发挥作用了。
//元组里面必须要有值
const info: [string,number,boolean] = ["abc",18]
const name = info[0]
console.log(name.length)
tuple的应用场景
function useState(state: any){
let currentstate = state
const changestate = (newstate: any) => {
currentstate = newstate
}
//changestate是一个函数类型
//(newstate: any)=>void是changestate的类型
const tuple: [any,(newstate: any)=>void] = [currentstate,changestate]
return tuple
}
const [counter,setCounter] = usestate(10);
setCounter(1000)
export {}
函数类型的写法:
type MyFunction =() => void
const foo: MyFunction =() => {}
函数的参数类型
//参数加上类型注解,加上返回值类型
//在开发中,我们一般不写返回值类型,会自动推导
function sum(num1: number,num2: number): number{
return num1 + num2
}
sum(123,321) 如果你只传1个参,也会报错的,ts非常严格
匿名函数的参数类型
//通常情况下,我们在定义函数时,都会给参数加上类型化解
function foo(message: string){
}
const names = ["abc","cba","nba"]
//写全:function(item) 这里是匿名函数
//这里可以不用写类型
names.forEach((item)=>{
//item根据上下文的环境推导出来,这个时候可以不添加类型
//上下文中的函数:可以不添加类型注解
item.split("")
})
对象类型
形参类型string
function print(message: string){
}
形参point 传进来必须有x和y,是一个对象类型并且是number类型
function printPoint(point: {x:number,y:number}){
console.log(point.x)
console.log(point.y)
}
//调用时,必须传数字类型的x和y
printPoint({x:123,y:321})
可选类型
// z? 表示 我调用该方法时,可以传参也可以不传
function printPoint(point: {x:number,y:number,z?:number}){
console.log(point.x)
console.log(point.y)
console.log(point.z)//没传值就是undefined
}
//我没有传参z,也没有报错
printPoint({x:123,y:321})
printPoint({x:123,y:321,z:111})
联合类型
//联合类型,用 | 来联合 ,可以传任意两个类型
function printID(id:number | string){
console.log(id)
//使用联合类型的时候需要特别小心
id.toUpperCase()
这样是会报错的,因为可能传来的是number
解决方案:
if(typeof id === 'string'){
//通过if,TS可以帮助确定id一定是string类型
id.toUpperCase()
}else{
console.log(id)
}
}
printID(123)
printID("abc")
可选类型和联合类型的关系
//当一个参数是可选类型的时候
//类似于表示:这个参数是 类型|undefined 这种形式的联合类型
function foo(message?: string){
console.log(message)
}
foo()//不传就是 传undefined,也可以传string/undefined
类型别名
//type用于定义类型别名(type alias)
type IDType = string | number | boolean
//对象别名
type PointType = {
x:number,
y:number,
z?:number
}
function printId(id:IDType){
}
//要求调用这个方法时,这个参数值必须是PointType对象类型
function printPoint(point: PointType)