我有一些过滤操作
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
}
是否可以基于Operation
参数确定T
类型?例如,如果我的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
}
答案 0 :(得分:1)
假设我正确理解了约束,则可以使用conditional types来表示它:
type Operation<T> =
| FilterOp
| (T extends Array<any> ? ArrayFilterOp : never)
| (T extends string ? StringFilterOp : never)
| (T extends Date ? DateFilterOp : never);
type GenericFilter<T> = {
Property: string;
Value: T;
Operation: Operation<T>;
};
这将支持您陈述的用例:
let dateFilter: GenericFilter<Date> = {
Property: "DateCreated",
Value: new Date(),
Operation: "DateIn"
};
以及使用辅助功能,因此您可以推断 T
,而无需手动指定:
const asGenericFilter = <T>(filt: GenericFilter<T>) => filt;
let stringFilter = asGenericFilter({
Property: "Name",
Value: "Alice",
Operation: "StartsWith" // hinted as Operation<string>
})
希望有所帮助;祝你好运!
答案 1 :(得分:0)
我设法通过OperationMap
type OperationMap<T> =
T extends Date
? (DateFilterOp | FilterOp)
: T extends Array<any>
? ArrayFilterOp
: T extends String
? FilterOp | StringFilterOp
: FilterOp;
type GenericFilter<T> = {
Property: string,
Value: T,
Operation: OperationMap<T>
}