0
想象一下簡單的CollectionStore
,它具有創建和更新記錄的方法。 create()
接受一組屬性並返回與添加的id
屬性相同的集合。 update
接受相同結構的集合,但需要定義id
屬性。Typescript:獲取類型並在通用接口中返回聯合類型
如何在Typescript中表示create()
函數接受某種類型T
並返回T & {id: string}
?
我希望像要表達的模式:
interface CollectionStore<T> {
updateRecord(T & {id: string}): void;
createRecord(T): T & {id: string};
}
但是上面的代碼是無效的。請幫助=)