【发布时间】:2019-09-25 01:16:40
【问题描述】:
我有一些过滤操作
type FilterOp = 'Equals' | 'NotEquals' | 'Greater' | 'GreaterEqual' | 'Less' | 'LessEqual';
type ArrayFilterOp = 'In' | 'NotIn';
type StringFilterOp = 'StartsWith' | 'EndsWith' | 'Contains' | 'NotContains';
type DateFilterOp = 'DateIn' | 'DateNotIn'
这是我当前的过滤器定义
type GenericFilter<T> = {
Property: string,
Value: T,
Operation: FilterOp | ArrayFilterOp | StringFilterOp | DateFilterOp
}
是否可以根据T 参数以某种方式确定Operation 类型?例如,如果我的T 是Date,那么打字稿将只允许将DateFilterOp 和FilterOp 分配给Operation 属性
let dateFilter: GenericFilter<Date> = {
Property: "DateCreated",
Value: new Date(),
Operation: // now I can only set value from FilterOp or DateFilterOp
}
【问题讨论】:
标签: typescript typescript-typings typescript-generics