世界上最伟大的投资就是投资自己的教育
// function getArray(items: any[]): any[] {
// return new Array().concat(items);
// }
// let myNumArray = getArray([100, 200, 300]);
// let myStrArray = getArray(["hello", "world"]);
// console.log(myNumArray);
// console.log(myStrArray);
// myNumArray.push(400);
// myStrArray.push("rails365");
// console.log(myNumArray);
// console.log(myStrArray);
// myNumArray.push("hfpp2012");
// myStrArray.push(500);
// console.log(myNumArray);
// console.log(myStrArray);
// [ 100, 200, 300, 400, 'hfpp2012' ]
// [ 'hello', 'world', 'rails365', 500 ]
// 可以用很多类型来代替 T
// T[] 代表数组,每个元素都是 T 类型
function getArray<T>(items: T[]): T[] {
return new Array<T>().concat(items);
}
// function getArray(items: number[]): number[] {
// return new Array().concat(items);
// }
// 调用,可以传入很多类型,代码复用
let myNumArray = getArray<number>([100, 200, 300]);
let myStrArray = getArray<string>(['hello', 'world']);
// 当不传入 T 时,会推荐出类型
// 不推荐这种写法,规范的作法是要指定类型
let myBooleanArray = getArray([true, false]);
myNumArray.push(400);
myStrArray.push('rails365');
// myBooleanArray.push('hfpp2012');
// function getArray1<T>(items: T): T {
// console.log(items.length);
// return T;
// }
function getArray1<T>(items: T[]): T[] {
console.log(items.length);
return items;
}
// myNumArray.push('hfpp2012');
// myStrArray.push(500);
// 限制为所有的元素是纯数字或字符串
//
// function getArray(items: number[]): number[] {
// return new Array().concat(items);
// }
// function getArray(items: string[]): string[] {
// return new Array().concat(items);
// }
// function getArray(items: boolean[]): boolean[] {
// return new Array().concat(items);
// }
06:261Free诱人的 TypeScript 视频教程 #1 介绍
14:082Free诱人的 TypeScript 视频教程 #2 为什么要用 TypeScript
04:543Free诱人的 TypeScript 视频教程 #3 安装 TypeScript
05:574Pro诱人的 TypeScript 视频教程 #4 类型定义
12:155Pro诱人的 TypeScript 视频教程 #5 var、let、const
08:036Pro诱人的 TypeScript 视频教程 #6 数组 - Array
04:597Pro诱人的 TypeScript 视频教程 #7 元组 - Tuple
05:538Pro诱人的 TypeScript 视频教程 #8 函数 - Function - Arrow Function
05:359Pro诱人的 TypeScript 视频教程 #9 函数返回值类型 - void
08:2710Pro诱人的 TypeScript 视频教程 #10 函数 - 默认参数和可选参数
06:2111Pro诱人的 TypeScript 视频教程 #11 函数 - Rest Parameters
07:1412Pro诱人的 TypeScript 视频教程 #12 任意类型 - any
08:5513Pro诱人的 TypeScript 视频教程 #13 联合类型 - union type、null、undefined
09:3714Pro诱人的 TypeScript 视频教程 #14 类 - 面向对象 - class 介绍
11:4615Pro诱人的 TypeScript 视频教程 #15 面向对象 - 类 - 构造方法(constructor)- 方法(methods)
13:1016Pro诱人的 TypeScript 视频教程 #16 面向对象 - 继承和多态 - Inheritance and Polymorphism
15:2217Pro诱人的 TypeScript 视频教程 #17 面向对象 - 成员可见性 - Member visibility - public 和 private
22:5518Pro诱人的 TypeScript 视频教程 #18 面向对象 - 成员可见性 - Member visibility - 深入解析 private 和 protected 的异同
10:2419Pro诱人的 TypeScript 视频教程 #19 面向对象 - 成员可见性 - Member visibility - 深入探索 constructor
16:1220Pro诱人的 TypeScript 视频教程 #20 面向对象 - 静态属性和方法 - static - 深入探索成员可见性
03:4821Pro诱人的 TypeScript 视频教程 #21 面向对象 - 只读属性 - readonly
07:2222Pro诱人的 TypeScript 视频教程 #22 枚举类型 - enum
05:3023Pro诱人的 TypeScript 视频教程 #23 nodemon 和 ts-node
14:5624Pro诱人的 TypeScript 视频教程 #24 接口介绍 - Interfaces - 鸭子类型
07:0125Pro诱人的 TypeScript 视频教程 #25 接口使用 - Interfaces
06:1226Pro诱人的 TypeScript 视频教程 #26 接口 - 方法
12:0227Pro诱人的 TypeScript 视频教程 #27 类型别名 - type alias
16:2328Pro诱人的 TypeScript 视频教程 #28 实例演练类实现接口 - Class Types Implementing an interface
09:1929Pro诱人的 TypeScript 视频教程 #29 接口 - 可选属性 - Excess Property Checks
04:0330Pro诱人的 TypeScript 视频教程 #30 接口 - 只读属性
08:2531Pro诱人的 TypeScript 视频教程 #31 接口 - Function Types
11:3832Pro诱人的 TypeScript 视频教程 #32 结合接口详细谈谈类型断言 part 1
10:4433Pro诱人的 TypeScript 视频教程 #33 结合接口详细谈谈类型断言 part 2
05:3934Pro诱人的 TypeScript 视频教程 #34 接口 - 继承与实现多个接口
08:5535Pro诱人的 TypeScript 视频教程 #35 接口 - 接口继承类 - Interface Extending Classes
07:2036Free诱人的 TypeScript 视频教程 #36 接口 - Indexable Types part 1
14:1737Free诱人的 TypeScript 视频教程 #37 接口 - Indexable Types part 2
05:2438Pro诱人的 TypeScript 视频教程 #38 如何处理列表数据
09:2739Pro诱人的 TypeScript 视频教程 #39 类 - 抽象类(补充)
03:3540Pro诱人的 TypeScript 视频教程 #40 Class - Parameter Properties(补充)
09:1141Pro诱人的 TypeScript 视频教程 #41 Class - Accessors getters/setters(补充)
06:4242Pro诱人的 TypeScript 视频教程 #42 --noImplicitThis flag and arrow function
07:4543Pro诱人的 TypeScript 视频教程 #43 五种定义函数类型的方法
08:3144Pro诱人的 TypeScript 视频教程 #44 函数重载 - Function Overloading - part 1
09:2045Pro诱人的 TypeScript 视频教程 #45 函数重载 - Function Overloading - part 2
10:1046Pro诱人的 TypeScript 视频教程 #46 Type Guards part 1(typeof)
09:3947Pro诱人的 TypeScript 视频教程 #47 Type Guards part 2(instanceof)
07:3648Pro诱人的 TypeScript 视频教程 #48 Type Guards For null and undefined - --strictNullChecks
05:1949Pro诱人的 TypeScript 视频教程 #49 非空断言操作符 ! - Non-Null Assertion Operator !
07:4150Pro诱人的 TypeScript 视频教程 #50 never 类型 part 1
11:4451Pro诱人的 TypeScript 视频教程 #51 never 类型 part 2
06:5852Pro诱人的 TypeScript 视频教程 #52 Discriminated Unions
09:4053Pro诱人的 TypeScript 视频教程 #53 泛型 - generics - 为何要使用泛型
08:35Free诱人的 TypeScript 视频教程 #54 泛型 - generics - 使用
06:2255Pro诱人的 TypeScript 视频教程 #55 泛型 - 在类中使用(简洁)
05:1656Pro诱人的 TypeScript 视频教程 #56 泛型 - 在函数中使用(简洁)
04:4957Pro诱人的 TypeScript 视频教程 #57 泛型 - 在接口中使用 part 1(简洁)
02:4758Pro诱人的 TypeScript 视频教程 #58 泛型 - Interfaces describing indexables part 2(简洁)
05:3159Pro诱人的 TypeScript 视频教程 #59 泛型 - OOPs style interfaces part 3(简洁)
07:4460Pro诱人的 TypeScript 视频教程 #60 扩展运算符 - Spread Operator(简洁)
03:2461Pro诱人的 TypeScript 视频教程 #61 用 keyof 来定义被允许的属性名称(简洁)
04:2762Pro诱人的 TypeScript 视频教程 #62 泛型 - Generic Constraints - 约束条件 part 1(65 集有补充)
05:3363Pro诱人的 TypeScript 视频教程 #63 泛型 - Generic Constraints - 约束条件 part 2(简洁)
02:3064Pro诱人的 TypeScript 视频教程 #64 泛型 - 类型别名(简洁)
05:1465Pro诱人的 TypeScript 视频教程 #65 泛型 - 约束 - 为什么要使用 extends(补充 62 集)
04:0666Free诱人的 TypeScript 视频教程 #66 模块
06:0067Free诱人的 TypeScript 视频教程 #67 配置文件 tsconfig.json 使用指南
05:2168Pro诱人的 TypeScript 视频教程 #68 如何使用第三方库 part 1
05:5769Free诱人的 TypeScript 视频教程 #69 如何使用第三方库 part 2(完结)
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
知乎:https://www.zhihu.com/people/rails365
掘金:https://juejin.cn/user/1574156379888263
b 站:https://space.bilibili.com/31152817
Github:https://github.com/hfpp2012
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
Discord:https://discord.gg/5rdjnEnU7F
Twitter:https://twitter.com/qiuzhi99pro
Facebook:https://twitter.com/qiuzhi99pro
Instagram:https://www.facebook.com/pro.qiuzhi/
▬▬▬▬▬▬ 微信相关 👋 ▬▬▬▬▬▬




© 汕尾市求知科技有限公司 | 专业版网站 | 在线学员:1128
粤公网安备 44152102000088号
| 粤ICP备19038915号
getArray 中如果传入的是多种类型的,T 就会变成多种类型的联合类型