[Typescript] Function Overloads
function reverse(str: string): string;
function reverse<T>(arr: T[]): T[];
function reverse<T>(stringOrArray: string | T[]): string | T[] {
if (typeof stringOrArray === ‘string‘) {
return stringOrArray
.split(‘‘)
.reverse()
.join(‘‘);
}
return stringOrArray.slice().reverse();
}
reverse(‘Pepperoni‘);
reverse([‘bacon‘, ‘pepperoni‘, ‘chili‘, ‘mushrooms‘]);Function overload doesn‘t compile to Javascript, it is just a way to tell typescript about type information
相关推荐
ChaITSimpleLove 2020-07-26
iconhot 2020-07-05
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26