pengkobe / reading-notes

:stars: to record daily reading notes. I build an issue blog to record daily FE study notes. suggestion and comments are welcomed.
https://github.com/pengkobe/reading-notes/issues
MIT License
13 stars 1 forks source link

typescript 的有趣语法 #488

Open pengkobe opened 5 years ago

pengkobe commented 5 years ago

说是有趣,其实就是语法糖啦。

泛型

function identity<T>(arg: T): T {
    return arg;
}

联合类型

 string | number

高级类型


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