Typescript array of different generic types

后端 未结 2 968
失恋的感觉
失恋的感觉 2020-12-11 20:56

If I have an array a where each element is an object consisting of two properties first and second, how should I declare `a\'s type su

2条回答
  •  北海茫月
    2020-12-11 21:22

    If you want an array where first will always have the same type, this will do

    interface IArrayGeneric {
        first: T;
        second: (arg: T) => string;
    }
    
    const a: Array>;
    

    This will ensure that you can't put any object into a that doesn't satisfy the above requirements, but will also constrain T to one specific type you choose.

提交回复
热议问题