Extending a generic parameter in Typescript

前端 未结 2 1182
醉梦人生
醉梦人生 2020-12-21 03:52

I want to create a utility function which creates a checklist by adding an isChecked knockout observable property to each item in an array. This function should

相关标签:
2条回答
  • 2020-12-21 04:13

    There isn't a way to express this in TypeScript.

    You can obviously do this instead:

    interface CheckListItem<T> {
        isChecked: KnockoutObservable<boolean>;
        item: T;
    }
    

    This has the advantage of not breaking the object when it happens to have its own isChecked property.

    0 讨论(0)
  • 2020-12-21 04:18

    Since TypeScript 1.6 you can use intersection types:

    type CheckListItem<T> = T & {
      isChecked: KnockoutObservable<boolean>;
    };
    
    0 讨论(0)
提交回复
热议问题