typescript-tutorial icon indicating copy to clipboard operation
typescript-tutorial copied to clipboard

泛型

Open xcatliu opened this issue 4 years ago • 18 comments

https://ts.xcatliu.com/advanced/generics.html

xcatliu avatar Jun 08 '20 12:06 xcatliu

看不懂啊

zhoufanglu avatar Jul 14 '20 08:07 zhoufanglu

写的真好呀

andade1996 avatar Aug 10 '20 02:08 andade1996

不会TS的前端不是好程序员

Frank-1000 avatar Sep 02 '20 02:09 Frank-1000

是我ts版本高了吗(4.0.2),泛型类的写法不行呀,必须得加构造函数耶

TGY-forture avatar Oct 09 '20 07:10 TGY-forture

看了下官网,得把tsconfig.json的strict设为false

TGY-forture avatar Oct 09 '20 12:10 TGY-forture

·Property 'zeroValue' has no initializer and is not definitely assigned in the constructor.ts(2564)` 这个例子有问题啊

sunq0001 avatar Nov 13 '20 09:11 sunq0001

刚才查了下,官方文档也是错的,在stackoverflow上找到了答案

class GenericNumber<T> {
    // to get rid of  error, you can define constructor 
    // which takes [zeroValue] and [add] as arguments
        constructor(public zeroValue: T, public add: (x: T, y: T) => T){
            this.zeroValue = zeroValue;
            this.add = add;
        }
    }

sunq0001 avatar Nov 13 '20 10:11 sunq0001

function copyFields<T extends U, U>(target: T, source: U): T { for (let id in source) { target[id] = (<T>source)[id]; } return target; } 请问这段代码的(<T>source)是什么意思?这个写法没看明白

codon19 avatar Nov 16 '20 10:11 codon19

@codon19 <T>source 就是 source as T,把 source 断言成 T 类型

xcatliu avatar Nov 16 '20 11:11 xcatliu

@codon19 <T>source 就是 source as T,把 source 断言成 T 类型

嗯好的,谢谢

codon19 avatar Nov 17 '20 01:11 codon19

努力学习

Amigosen avatar Nov 26 '20 05:11 Amigosen

泛型后面感觉复杂化了,看不懂是解决什么问题的

a946547732 avatar Dec 08 '20 09:12 a946547732

有个问题,多个类型参数之间也可以互相约束:

function copyFields<T extends U, U>(target: T, source: U): T {
    for (let id in source) {
        target[id] = (<T>source)[id];
    }
    return target;
}

let x = { a: 1, b: 2, c: 3, d: 4 };

copyFields(x, { b: 10, d: 20 });

上例中,我们使用了两个类型参数,其中要求 T 继承 U。这里是不是应该写继承于?

a extends b 不是a继承于b吗?

整段话是不是可以理解为T和U要保证是相同类型的?

function copyFields<T, U extends T>(target: T, source: U): T {
  for (let id in source) {
    target[id] = (<T>source)[id];
  }
  return target;
}

换成这种形式为什么不行?

clousky2020 avatar Dec 17 '20 04:12 clousky2020

@sunq0001 刚才查了下,官方文档也是错的,在stackoverflow上找到了答案

class GenericNumber<T> {
    // to get rid of  error, you can define constructor 
    // which takes [zeroValue] and [add] as arguments
        constructor(public zeroValue: T, public add: (x: T, y: T) => T){
            this.zeroValue = zeroValue;
            this.add = add;
        }
    }

class GenericNumber<T> { zeroValue!: T; add!: (x: T, y: T) => T; }

加个! 就通过了

allenlinc avatar Dec 21 '20 17:12 allenlinc

@allenlinc

@sunq0001 刚才查了下,官方文档也是错的,在stackoverflow上找到了答案

class GenericNumber<T> {
    // to get rid of  error, you can define constructor 
    // which takes [zeroValue] and [add] as arguments
        constructor(public zeroValue: T, public add: (x: T, y: T) => T){
            this.zeroValue = zeroValue;
            this.add = add;
        }
    }

class GenericNumber<T> { zeroValue!: T; add!: (x: T, y: T) => T; }

加个! 就通过了

East-Apollo avatar Jan 19 '21 08:01 East-Apollo

@allenlinc

@sunq0001 刚才查了下,官方文档也是错的,在stackoverflow上找到了答案

class GenericNumber<T> {
    // to get rid of  error, you can define constructor 
    // which takes [zeroValue] and [add] as arguments
        constructor(public zeroValue: T, public add: (x: T, y: T) => T){
            this.zeroValue = zeroValue;
            this.add = add;
        }
    }

class GenericNumber<T> { zeroValue!: T; add!: (x: T, y: T) => T; }

加个! 就通过了

East-Apollo avatar Jan 19 '21 08:01 East-Apollo

这个加!是什么语法呀

East-Apollo avatar Jan 19 '21 08:01 East-Apollo

泛型的个人理解: 在泛型的结构中,T相当于类型的参数,使用时通过<type>传入具体的类型,用来替换T位置的类型。

snowords avatar Mar 18 '22 06:03 snowords