当前位置:网站首页>TypeScript -- 第六节 泛型
TypeScript -- 第六节 泛型
2022-06-28 23:43:00 【Run Coder】
/* * 泛型 * 使用泛型创建可重用的组件,一个组件可以支持多种类型的数据 * 【自我理解】把参数的类型也设置为类似函数形参一样的形式,可以根据开发者的意愿随时改变参数的类型----<类型变量> * 包括:传入类型 和 返回类型 【学习程度】学了一遍,感觉知识点模模糊糊,还是需要练习和多加复习!! * */
// 示例
//identity函数叫做泛型
function identity<T>(arg: T): T {
return arg;
}
//定义泛型的两种方法 1.类型变量 2.类型推断
let output = identity<string>('string 泛型示例');
// let output = identity("myString"); // type of output will be 'string'
console.log(output)
//使用泛型变量
//数组 两种写作形式
function loggingIdentity<T>(arg: Array<T>): Array<T> {
console.log(arg.length); // Array has a .length, so no more error
return arg;
}
loggingIdentity<string>(['1','2','3']);
loggingIdentity<number>([1,2,3]);
function loggingIdentity1<T>(arg: T[]): T[] {
console.log(arg.length); // Array has a .length, so no more error
return arg;
}
//泛型类型
//泛型接口
interface GenericIdentityFn {
<T>(arg: T): T;
}
function identity2<T>(arg: T): T {
return arg;
}
//使用带有调用签名的对象字面量来定义泛型函数
let myIdentity: GenericIdentityFn = identity;
console.log(myIdentity(1))
//泛型类
//【备注】类有两部分:静态部分和实例部分。 泛型类指的是实例部分的类型,所以类的静态属性不能使用这个泛型类型。
class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
}
let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) {
return x + y; };
//泛型约束
//【注】定义一个接口来描述约束条件,被约束之后的函数不再适用于任意类型
//定义一个包含.length属性的接口,使用这个接口和extends关键字来实现约束
interface Lengthwise {
length: number;
}
function loggingIdentity2<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // Now we know it has a .length property, so no more error
return arg;
}
loggingIdentity2('string');
// loggingIdentity2(3); //报错 Argument of type '3' is not assignable to parameter of type 'Lengthwise'.
//在泛型约束中使用参数
function getProperty<T, K extends keyof T>(obj: T, key: K) {
//官网写法不全。。。
return obj[key];
}
let x = {
a: 100, b: 2, c: 3, d: 4 };
console.log(getProperty(x, "a")); // okay
// getProperty(x, "m"); // error: Argument of type 'm' isn't assignable to 'a' | 'b' | 'c' | 'd'.
//在泛型里使用类类型
class BeeKeeper {
hasMask: boolean;
}
class ZooKeeper {
nametag: string;
}
class Animal {
numLegs: number;
}
class Bee extends Animal {
keeper: BeeKeeper;
}
class Lion extends Animal {
keeper: ZooKeeper;
}
function createInstance<A extends Animal>(c: new () => A): A {
return new c();
}
createInstance(Lion).keeper.nametag; // typechecks!
createInstance(Bee).keeper.hasMask; // typechecks!
不确定性和未知的魅力,因为不确定,所以有希望,因为未知,所以期待!
边栏推荐
- Stm32f407----- register address name mapping analysis
- The secondary market is full of bad news. How should the market go next? One article will show you the general trend
- stm32F407-------LCD
- 2022 PMP project management examination agile knowledge points (4)
- Advice to friends
- Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)
- What are the virtual machine software? What are their respective roles?
- Interviewer: what is the internal implementation of strings in redis?
- 【软件分析】软件分析、设计与建模迭代式详解
- TDD and automated testing
猜你喜欢

【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)

从SQL注入绕过最新安全狗WAF中学习fuzz
![[mathematical modeling] fmincon() function of MATLAB nonlinear programming](/img/fc/46949679859b1369fcc83d0d8b637c.png)
[mathematical modeling] fmincon() function of MATLAB nonlinear programming

Huawei's level 22 experts have worked hard for ten years to complete the advanced practical document of cloud native service grid. 6

PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报

10. Standard i/o redirection and pipeline

Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)

Chapter IV memory management exercise

Interviewer: what is the internal implementation of strings in redis?
随机推荐
Non scientific class! The road of self-study!
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022
融云通信解决方案 破解企业沟通痛点
Stm32f407-------- NVIC interrupt priority management
Behaviortree in ros2
[stm32 Hal library] serial port communication
Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
Three communication skills in software testing
stm32F407-------LCD
Didn't find an internship. He summed it up
油猴脚本学习
Add the premise of ganggan
PHP利用CURL实现登录网站后下载Excel文件
[matlab]函数定义与使用
The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
Don't ask me how to do UI automation test again
机器学习6-决策树
ERROR 1067 (42000): Invalid default value for ‘end_ time‘ Mysql
Picture 64base transcoding and decoding