【发布时间】:2020-08-10 23:22:03
【问题描述】:
我的应用中有多个对象。例如:
interface Recipe {
title: string,
ingredients: []
creator: string,
// ...
}
interface Vendor {
name: string
address: Address
// ...
}
用户应该能够创建可以接受任何这些接口的对象,例如:
interface Event<T> {
date: Date(),
type: T // This should be of type Recipe or Vendor object for example.
}
- 正确的定义方法是什么?
- 然后我将如何找出用户传递了哪个对象?
谢谢!
【问题讨论】:
-
嗨@Agata!我不确定,你是什么意思“定义这个的正确方法”?您已经正确定义了一个可以接受通用类型的事件。您想强制类型 nly 为“Recipe”或“Vendor”之一吗?
-
是的。我也不清楚如何识别用户通过了哪个对象?
标签: typescript object generics interface