A T*_*A T 12 schema interface xor typescript typescript2.0
我怎么说我想要一个接口是一个或另一个,但不是两者兼有?
interface IFoo {
bar: string /*^XOR^*/ can: number;
}
Run Code Online (Sandbox Code Playgroud)
Sar*_*ana 20
您可以使用联合类型以及never类型来实现此目的:
type IFoo = {
bar: string; can?: never
} | {
bar?: never; can: number
};
let val0: IFoo = { bar: "hello" } // OK only bar
let val1: IFoo = { can: 22 } // OK only can
let val2: IFoo = { bar: "hello", can: 22 } // Error foo and can
let val3: IFoo = { } // Error neither foo or can
Run Code Online (Sandbox Code Playgroud)
如本期所建议,您可以使用条件类型(在Typescript 2.8中引入)来编写XOR类型:
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用它:
type IFoo = XOR<{bar: string;}, {can: number}>;
let test: IFoo;
test = { bar: "test" } // OK
test = { can: 1 } // OK
test = { bar: "test", can: 1 } // Error
test = {} // Error
Run Code Online (Sandbox Code Playgroud)
您可以使用 union 和可选的void 类型获得“一个但不是另一个” :
type IFoo = {bar: string; can?: void} | {bar?:void; can: number};
Run Code Online (Sandbox Code Playgroud)
但是,您必须使用它--strictNullChecks来防止两者都没有。
| 归档时间: |
|
| 查看次数: |
1623 次 |
| 最近记录: |