Can You Specify Multiple Type Constraints For TypeScript Generics

后端 未结 3 2150
梦如初夏
梦如初夏 2020-12-15 02:28

I have a generic interface like this example with a single type constraint:

export interface IExample {
    getById(id: number): T;
         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 02:52

    Typescript doesn't offer a syntax to get multiple inheritance for generic types. However, you can achieve similar semantics by using the Union types and Intersection types. In your case, you want an intersection :

    interface Example {}
    

    For a Union of both types :

    interface Example {}
    

提交回复
热议问题