Crossing types (merging multiple types into one type)

function extend<T, U> (first: T, second: U): T & U {
    let result = {} as T & U
    for (let id in first) {
        result[id] = first[id] as any
    }
    
    for (let id in second) {
        if(! result.hasOwnProperty(id)) { result[id] = second[id] as any } }return result
}

class Person {
    constructor(public name: string) {
        
    }
}

interface Loggable {
    log (): void
}

class ConsoleLogger implements Loggable {
    log () {}
}
var jim = extend(new Person('jim'), new ConsoleLogger())
console.log(jim.name)
jim.log()
Copy the code

The joint type

// We can only pass in number and string from the function implementation, but we can pass in any type.function padLeft(value: string, padding: any) {
    if (typeof padding === 'number') {
        return Array(padding + 1).join(' ') + value
    } else if (typeof padding === 'string') {
        return padding + value
    }
    throw new Error('Expected string or number got' + padding)
}
paddLeft('Hello World'And 4) / /' Hello World'// Implement with federated types (type exceptions can be detected at compile time)function padLeft(value: string, padding: string | number) {
    if (typeof padding === 'number') {
        return Array(padding + 1).join(' ') + value
    } else if (typeof padding === 'string') {
        return padding + value
    }
    throw new Error('Expected string or number got' + padding)
}
Copy the code
interface Bird {
    fly()
    layEggs()
}
interface Fish {
    swim()
    layEggs()
}

function getSmallPet(): Fish | Bird {
    // ...
}
letPet = getSmallPet () pet. LayEggs () / / pet. Swim () error, Fish | Bird can only determine contains common members, compile time is not sure finally returned to specific types of Fish or BirdCopy the code

Type of protection

1. Type predicate interface Bird {fly() layEggs()} interface Fish {swim() layEggs()}function getSmallPet(): Fish | Bird {
    // ...
}
let pet = getSmallPet()

function isFish(pet: Fish | Bird): pet is Fish {
    return(pet as Fish).swim ! == undefiend }if (isFish(pet)) {
    pet.swim()
} else{pet.fly()} // 2. Typeof type protectionfunction padLeft(value: string, padding: string | number) {
    if (typeof padding === 'number') {
        return Array(padding + 1).join(' ') + value
    } else if (typeof padding === 'string') {
        return padding + value
    }
    throw new Error('Expected string or number got' + padding)
}
paddLeft('Hello World'And 4) / /' Hello World'3. Instanceof type protection class Bird {fly() {
        console.log('bird fly')}layEggs() {
        console.log('bird lay eggs')
    }
}
class Fish {
    swim() {
        console.log('fish swim')}layEggs() {
        console.log('fish lay eggs')}}function getRandomPet(): Fish | Bird {
    returnMath. The random () > 0.5? new Bird() : new Fish() }let pet = getRandomPet()
if (pet instanceof Bird) {
    pet.fly()
}
if (pet instanceof Fish) {
    pet.swim()
}
Copy the code

A type that can be null

// null and undefined can be assigned to all typeslet s = 'foo'S = null // strictNullChecks fails if strictNullChecks is addedlet sn: string | null = 'bar'Sn = null sn = undefined // strictNullChecks fails if strictNullChecks is addedCopy the code
/ / indicate the optional second parameter number type, is actually the number | undefined combination type fucntion f (y, x: number? : number) {return(x + y | | 0)} f (1, 2) f (1) f (1, undefined) f (1, null) compiled with / / compile time parameters - strictNullChecks pass, don't add can passCopy the code
// Null type protectionfunction broken (name: string | null): string {
    function postfix (epithet: string) {
        returnname! .charAt(0) +'. the' + epithet
    }
    name = name || 'Bob'
    return postfix(name)
}
Copy the code

String literals

type Easing = 'ease-in' | 'ease-out' | 'ease-in-out'

class UIElement {
    animate(dx: number, dy: number, easing: Easing) {
        if (easing === 'ease-in') {}else if (easinig === 'ease-out') {}else if (easing === 'ease-in-out') {}else{}}}let button = new UIElement()
button.animate(0, 0, 'ease-in')
// button.animate(0, 0, 'uneasy') 'uneasy'Does not meet theCopy the code