any可以是任何类型
    下列代码即使我们期望返回值是string,但是仍然可以返回任何类型的值

    1. function selectDeliverAddress(addressOrIndex: any):string {
    2. if(typeof addressOrIndex === 'number' && addressOrIndex < deliveryAddresses.length) {
    3. return deliverAddresses[addressOrIndex]
    4. }
    5. return addressOrIndex
    6. }
    7. const myFavouriteAddress = selectDeliverAddress(true)

    如果我们使用unknown类型,我们必须做类型检查,否则会报错
    image.png

    1. function selectDeliverAddress(addressOrIndex: any):string {
    2. if(typeof addressOrIndex === 'number' && addressOrIndex < deliveryAddresses.length) {
    3. return deliverAddresses[addressOrIndex]
    4. } else if(typeof addressOrIndex === 'string'){
    5. return addressOrIndex
    6. }
    7. return ''
    8. }
    9. const myFavouriteAddress = selectDeliverAddress(true)

    P.S.
    Subtypes and Supertypes
    For example, any is the supertype of all types, and string is a subtype of any.