【发布时间】:2018-04-24 09:44:02
【问题描述】:
我有一个 Typescript 类型定义为:
export type IStepFunctionOperand =
| IStepFunctionOperand_StringEquals
| IStepFunctionOperand_StringGreaterThan
| IStepFunctionOperand_StringGreaterThanEquals
| IStepFunctionOperand_StringLessThan
| IStepFunctionOperand_StringLessThanEquals
| IStepFunctionOperand_NumericEquals
| IStepFunctionOperand_NumericGreaterThan
| IStepFunctionOperand_NumericGreaterThanEquals
| IStepFunctionOperand_NumericLessThan
| IStepFunctionOperand_NumericLessThanEquals;
每个条件句如下所示:
export interface IStepFunctionOperand_NumericLessThanEquals
extends IStepFunctionBaseLogicalOperand {
/** compare the value passed in -- and scoped by "Variable" -- to be numerically equal to a stated number */
NumericLessThanEquals?: number;
}
export interface IStepFunctionBaseLogicalOperand {
/** points to the specific area of context which is being evaluated in the choice */
Variable: string;
}
这可以满足我作为 type 的要求,但是将这个 type 做成 interface 会非常方便。如果我能够做到这一点,我就可以像这样定义一个接口:
export interface IStepFunctionChoiceItem<T> extends Partial<IStepFunctionOperand> {
// simple operands leverage inheritance but are optional
// complex operators
And?: IStepFunctionOperand[];
Or?: IStepFunctionOperand[];
Not?: IStepFunctionOperand;
// State machine
Next?: keyof T;
End?: true;
}
这可能吗?
【问题讨论】:
-
这样我就可以使用继承......见上文。
-
对不起,我看错了。您可以使用交叉点类型。
-
我对它们很熟悉,但在这里怎么用?
-
export type IStepFunctionChoiceItem<T> = Partial<IStepFunctionOperand> & {Next?: keyof T, members}; -
有效。如果您愿意,请添加为答案,我会立即将其标记为正确。非常感谢。
标签: typescript