首页 > 其他分享 >兼收并蓄 TypeScript - 类: generics

兼收并蓄 TypeScript - 类: generics

时间:2024-09-20 12:29:07浏览次数:1  
标签:TypeScript console log 兼收并蓄 webabcd let result generics 泛型

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

兼收并蓄 TypeScript - 类: generics

示例如下:

class\generics.ts

{
    // Generics - 泛型
    
    // 泛型的简单示例
    function createArray<T>(length: number, value: T): Array<T> {
        let result: T[] = [];
        for (let i = 0; i < length; i++) {
            result[i] = value;
        }
        return result;
    }

    let a = createArray<string>(3, 'x');
    console.log(a); // ['x', 'x', 'x']

    // 可以省略 <string> 因为 TypeScript 可以根据传入的参数推导出类型
    let b = createArray(3, 'x');
    console.log(b); // ['x', 'x', 'x']
}

{
    // 可以定义多个不同的泛型类型
    function swap<T, U>(tuple: [T, U]): [U, T] {
        return [tuple[1], tuple[0]];
    }
    
    let a = swap<number, string>([7, 'seven']);
    console.log(a); // ['seven', 7]

    // 可以省略 <number, string> 因为 TypeScript 可以根据传入的参数推导出类型
    let b = swap([7, 'seven']);
    console.log(b); // ['seven', 7]
}

{
    // 泛型约束,用于约束泛型必须继承自某个类或某些接口

    interface Interface1 {
        name: string;
    }
    interface Interface2 {
        age: number;
    }
    class Class1 implements Interface1, Interface2 {
        constructor(public name: string, public age: number) {
        }
    }

    // 泛型 T 必须继承自 Interface1 和 Interface2
    function hello<T extends Interface1 & Interface2>(p: T): string {
        return `${p.name} ${p.age}`;
    }

    let a = new Class1('webabcd', 44);
    console.log(hello(a)); // webabcd 44
}

{
    // 通过接口定义泛型
    interface CreateArray<T> {
        (length: number, value: T): Array<T>;
    }
    
    let createArray: CreateArray<string> = function<T>(length: number, value: T): Array<T> {
        let result: T[] = [];
        for (let i = 0; i < length; i++) {
            result[i] = value;
        }
        return result;
    }

    let a = createArray(3, 'x');
    console.log(a); // ['x', 'x', 'x']
}

{
    // 泛型类
    class Class1<T> {
        salt?: T;
        add?: (x: T, y: T) => T;
    }
    
    let a = new Class1<number>();
    a.salt = 10;
    a.add = function(x, y) { 
        return (x + y) * this.salt!; 
    };

    console.log(a.add(2, 3)); // 50
}

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

标签:TypeScript,console,log,兼收并蓄,webabcd,let,result,generics,泛型
From: https://www.cnblogs.com/webabcd/p/18422264/typescript_class_generics

相关文章

  • 兼收并蓄 TypeScript - 类: 模块
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-类:模块示例如下:module\main.ts/***本例用于演示import,export*///从指定的模块中导入指定的被export的变量或函数或对象import{name,hello}from'./a';//从指定......
  • 兼收并蓄 TypeScript - 进阶: ArrayBuffer
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:ArrayBuffer示例如下:advanced\arrayBuffer.ts{/***1、ArrayBuffer-内存之中的一段二进制数据,需要通过视图操作数据*2、TypedArray-视图,用于操作ArrayBuf......
  • 兼收并蓄 TypeScript - 进阶: promise
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:promise示例如下:advanced\promise.ts{/***Promise-用于异步编程(非多线程)*有3种状态:pending(进行中),fulfilled(已成功),rejected(已失败)*状态只能从......
  • 兼收并蓄 TypeScript - 进阶: async/await
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:async/await示例如下:advanced\async_await.ts{/***async/await-用于异步编程(非多线程)*asyncfunction返回的是Promise对象*await用于等Pro......
  • 兼收并蓄 TypeScript - 进阶: iterator, generator
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:iterator,generator示例如下:advanced\iterator_generator.ts{/***iterator-迭代器(可迭代对象有Array,TypedArray,Map,Set,String)*next()-迭代到......
  • 兼收并蓄 TypeScript - 进阶: proxy, reflect
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:proxy,reflect示例如下:advanced\proxy_reflect.ts{//Proxy-代理(拦截目标对象的属性操作和函数操作)lettarget={name:'webabcd',age:40,......
  • 兼收并蓄 TypeScript - 第三方库: 类型声明
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-第三方库:类型声明示例如下:third\typeDeclaration.ts/**类型声明用于TypeScript调用JavaScript*类型声明定义在.d.ts声明文件中*比如aes.js文件,其对应的声明文件为ae......
  • 兼收并蓄 TypeScript - 第三方库: crypto-js
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-第三方库:crypto-js示例如下:third\cryptojs.ts/**本例以在TypeScript中使用crypto-js为例*crypto-js是一个纯js项目,是不能直接在typescript中使用的,需要相应的.d.ts......
  • 兼收并蓄 TypeScript - 基础: number
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:number示例如下:basic\number.ts{//将指定类型的数据转换为number类型console.log(Number("100"),Number(true),Number({}),Number([]));//1001NaN0//......
  • 兼收并蓄 TypeScript - 基础: string
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:string示例如下:basic\string.ts{leta="\x7A";//十六进制的“7A”是字符“z”letb="\u{7A}";//十六进制的“7A”是字符“z”letc="\u{738B}";......