typescript基本数据类型01(笔记)

// any类型
// 
let message: any = "Hello world"
message = 123
message = true
message = {}

// any类型
const arr: any[] = []


// unknown类型 不确定类型
function foo(){
    return 'abc'
}
function bar(){
    return 123
}
let flag = true
let result: any
if(flag) {
    result = foo()
} else {
    result = bar()
}
console.log(result);


// unknow只能赋值给any和unknown类型
// void 类型
function sum(num1: number, num2: number): void {
    console.log(num1 + num2);
    return null // void返回值类型可以是undefined 也可以值null 当一个函数没有返回值的时候就会返回undefined
}
sum(20, 30)
// sum('abc', 'cbd')


// never类型 表示永远不会发生值的一个类型
function foo1():never { // 这个函数永远不会有返回值
    // 死循环
    while(true) {
    }
}
function bar1():never {
    throw new Error() // 抛出异常 没有返回结果
}
// never 到底应用在什么场景

// 封装了一个核心函数
// 联合类型 传入的参数可以是string类型也可以是number类型
function handleMessage(message: string | number | boolean){
    switch(typeof message) {
        case 'string':
            console.log("string处理方式处理message");
            break
        case 'number':
            console.log("number处理方式处理message");
            break
        case 'boolean':
            console.log("boolean处理方式处理message");
            break
        default:
            const check: never = message
    }
}
handleMessage('abc')
handleMessage(123)
handleMessage(false) // 会报错


// tuple 元组:多种元素的组合
const info2: any[] = ['why', 18, 1.88]
const infoObj = {
    name: 'why',
    age: 18,
    height: 1.88
}
const myname = info2[0]
// 元组的特点:
const infos: [string, number, number] = ['组合NGSHAN', 18, 1.88]
console.log(infos[0], infos[1], infos[2]);
// 开发里面有什么用呢?应用场景
// function useState(state: any) {
//     let currentState = state
//     const changeState = (newState: any) => {
//         currentState = newState
//     }
//     const tuple: [any, (newState: any)=>void] = [currentState, changeState]
//     return tuple
// }
// const [counter, setCounter] = useState(10)
// setCounter(1000)
// const [title, setTitle] = useState('abc')

// 优化 需要用到泛型
function useState<T>(state: T) {
    let currentState = state
    const changeState = (newState: T) => {
        currentState = newState
    }
    const tuple: [T, (newState: T)=>void] = [currentState, changeState] // 元组
    return tuple
}
const [counter, setCounter] = useState(10)
setCounter(1000)
const [title, setTitle] = useState('abc')

// 函数类型
const foor: () => void = () => {} // 表示的是一个类型
// 开发里面一般会这样写 函数类型
type MyFunction = () => void
const fool: MyFunction = () => {}


// 函数的参数和返回值类型
// 给参数加上类型注解
// 给返回值加上
// 在开发中可以不写返回值的类型,它会自动推导
// 对参数的个数也是有限制的
function summ(num1:number, num2:number):number {
    return num1 + num2
}
// 封装一个第三方库的时候可以写,便于理解


// 匿名函数的参数类型
// 通常情况下,在定义一个函数时 都会给参数加上类型注解
function tool(message: string) {}
const names = ['abc', 'cba', 'bac']
// 根据上下文环境推导出来的, 这个时候可以不添加类型注解
names.forEach((item: string) => {})


// 对象类型
function printPoint(point: {x: number, y: number}) {
    console.log(point.x);
    console.log(point.y);
}
printPoint({x: 12, y: 22})


// 可选类型
// function printPoint2(point: {x: number, y: number, z?: number }) {
// 对象类型起别名
type IdType = {
    x: number
    y: number
    z?: number
}
function printPoint2(point: IdType) {
    console.log(point.x);
    console.log(point.y);
    console.log(point.z);
}
printPoint2({x: 12, y: 22})
printPoint2({x: 12, y: 22, z: 77})


// 联合类型 union type
// function printId(id: number|string|boolean) {
// 类型别名 type是用来定义类型别名的
type UnionType = number|string|boolean
function printId(id: UnionType) {
    // 使用联合类型的值时需要特别的小心
    console.log(id);
    if(typeof id === 'string') { // narrow: 缩小的意思
        console.log(id.toUpperCase());
    } else {
        console.log(id);
    }
}
printId(123)
printId('abc')
printId(true)


// 可选类型和联合类型的关系
// 让一个参数本身是可选的
// 当一个参数是可选类型的时候 它本质上表示的是
function foot(message?: string|undefined){
    console.log(message);
}
foot('123')
foot(undefined)
foot()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值