TypeScript中泛型对象、泛型类

一. 概览

本文详细介绍泛型中泛型对象和泛型类的使用,结合实际应用场景,加深对泛型的理解、使用。

二. 泛型对象

举个例子

const test = {
   
    a: 1,
    b: 1
}

一个类型有没有一种可能让我么在定义的时候传入一个类似于变量的形参,在使用的时候传入具体的类型参数。在定义前不知道具体的参数类型

当然我们也可以定义为any,但是和没有限制差别不大

引入泛型对象

interface ITest<T> {
   
    id: Number,
    content: T,
    completed: boolean
}

const test:ITest<string> = {
   
    id: 1,
    content: '',
    completed: true
}

泛型实参会限制泛型形参变量的类型

作为函数的形参和实参

function createTodo<T>({
   
    id,
    content,
    completed
}: ITest<T>) {
   
    return {
   
        id,
        content,
        completed
    }
}

const todo1 = createTodo<string[]>({
   id:2, content: ['唱','跳','rap'],completed:true})

三. 泛型类

在class中,所以属性都应该是私有的,如果需要获取属性,设置属性都应该由公共的getter setter方法去完成。
完整代码


interface ITodoProps<T> {
   
    id: number,
    content: T;
    completed: boolean
}
    
interface ITodo<T> {
   
 get(): ITodoProps<T>,
 create():string
}
    
     
class Todo<T> implements ITodo<T>{
   
    private id: number;
    private content: T;
    private completed: boolean;
   
    constructor(id: number, content: T, completed: boolean) {
   
        this.id = id;
        this.content = content;
        this.completed = completed;
    }

   get(): ITodoProps<T> {
   
        return {
   
        id: this.id,
        content: this.content,
        completed: this.completed
   }}

   create(): string {
   
    if (typeof this.content === "string") {
   
      return this.createStringContent(this.content);
    } else if (Array.isArray(this.content) && typeof this.content[0] === "string") {
   
      return this.createArrayContent(this.content as string[]);
    } else {
   
      return this.createObjectContent(this.content);
    }
   }

    private createStringContent(content: string): string {
   
     return content
    }
    private  createArrayContent(content: string[]): string {
   
     return content.join(',')
    }

    private createObjectContent(content: T): string {
   
      let _content = '';
      for (let key in content) {
   
        let _content = "";
        _content += `${
     key}: ${
     content[key]}`
      }
      return _content
    }
}
    
    const todo11 = new Todo<string[]>(2,['逛街','打球','看电影'],false)
    const todo12= new Todo<{
   [key:string]:number}>(3, {
   sleep:1,walking:2,study:3},false)
    
    const todoString111 =todo11.create()
    const todoString112=todo12.create()
    
    const todoList = [todoString111,todoString112]
    const todo1 = todo11.get()
    const todo2 = todo12.get()
    
    function getTodoProp<T, K extends keyof T>(todo: T,key: K) {
   
       return todo[key]
    }
    
    getTodoProp(todo1,'id')

可以结合例子,去看泛型类的使用

keyof的使用

 function getTodoProp<T, K extends keyof T>(todo: T,key: K) {
   
       return todo[key]
    }
 getTodoProp(todo1,'id')

疑问:getTodoProp(todo1,‘id’)为什么可以编译成功,不需要传T的具体类型吗?

解答:

  1. getTodoProp 函数使用了泛型和 TypeScript 的索引类型查询,可以在不使用 T 类型的情况下获取 todo 对象上的 key 属性对应的值。
  2. 在函数定义中,泛型类型参数 T 表示传入的 todo 参数的类型,而泛型类型参数 K 是具有约束条件 extends keyof T 的索引类型查询,表示必须是 T 对象的属性名之一。

当我们在调用 getTodoProp 函数时,传入了一个 todo1 对象作为 todo 参数,而 TypeScript 会根据该对象的类型自动推断 T 类型为:
{
id: number;
content: string;
}

在 getTodoProp(todo1, ‘id’) 调用中,K 类型被推断为 ‘id’,因此 key 参数的类型为 ‘id’,这个类型也符合泛型类型参数 K 的约束条件。

类似地

function getSplitValue3<E>(value: E[],type: string):string {
   
    return value.join(type)
}

const aa = getSplitValue3<string>(['a','b','c'],',')
const bb = getSplitValue3<number>([1,2,3],',')

可以省略为

function getSplitValue3<E>(value: E[],type: string):string {
   
    return value.join(type)
}

const aa = getSplitValue3(['a','b','c'],',')
const bb = getSplitValue3([1,2,3],',')

相关推荐

  1. TypeScript对象

    2023-12-08 20:56:05       41 阅读
  2. TypeScript函数

    2023-12-08 20:56:05       47 阅读
  3. typescript

    2023-12-08 20:56:05       13 阅读
  4. TypeScript

    2023-12-08 20:56:05       20 阅读
  5. Typescript

    2023-12-08 20:56:05       22 阅读
  6. typeScript9 (

    2023-12-08 20:56:05       17 阅读
  7. TypeScript:

    2023-12-08 20:56:05       16 阅读
  8. TypeScript 类型

    2023-12-08 20:56:05       12 阅读
  9. 理解typescript

    2023-12-08 20:56:05       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 20:56:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 20:56:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 20:56:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 20:56:05       20 阅读

热门阅读

  1. 【python】vscode中选择虚拟环境venv

    2023-12-08 20:56:05       38 阅读
  2. Linux DAC权限的简单应用

    2023-12-08 20:56:05       31 阅读
  3. 做题笔记:SQL Sever 方式做牛客SQL的题目--VQ34

    2023-12-08 20:56:05       32 阅读
  4. 面试经典150题(1-2)

    2023-12-08 20:56:05       35 阅读
  5. 如何在Go中编写Switch语句

    2023-12-08 20:56:05       36 阅读
  6. 基于Docker安装Mysql:5.5

    2023-12-08 20:56:05       23 阅读