与XOR,{bar:string} XOR {can:numbers}的打字稿接口

发布于 2025-01-29 05:14:24 字数 120 浏览 4 评论 0 原文

我怎么说我想让一个接口是一个或另一个,而不是两者兼而有之?

interface IFoo {
    bar: string /*^XOR^*/ can: number;
}

How do I say that I want an interface to be one or the other, but not both or neither?

interface IFoo {
    bar: string /*^XOR^*/ can: number;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空名 2025-02-05 05:14:24

您可以与 从不从不 /a>实现这一目标的类型:

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

You can use union types along with the never type to achieve this:

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
溺深海 2025-02-05 05:14:24

这个问题,您可以使用

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;

​像这样:

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

As proposed in this issue, you can use conditional types (introduced in Typescript 2.8) to write a XOR type:

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;

And you can use it like so:

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
旧时浪漫 2025-02-05 05:14:24

您可以使用Union和可选的 noreferrer“> void Type

type IFoo = {bar: string; can?: void} | {bar?:void; can: number};

但是,您必须使用 - strictnullchecks 以防止都没有。

You can get "one but not the other" with union and optional void type:

type IFoo = {bar: string; can?: void} | {bar?:void; can: number};

However, you have to use --strictNullChecks to prevent having neither.

樱花坊 2025-02-05 05:14:24

尝试以下操作:

type Foo = {
    bar?: void;
    foo: string;
}

type Bar = {
    foo?: void;
    bar: number;
}

type FooBar = Foo | Bar;

// Error: Type 'string' is not assignable to type 'void'
let foobar: FooBar = {
    foo: "1",
    bar: 1
}

// no errors
let foo = {
    foo: "1"
}

try this:

type Foo = {
    bar?: void;
    foo: string;
}

type Bar = {
    foo?: void;
    bar: number;
}

type FooBar = Foo | Bar;

// Error: Type 'string' is not assignable to type 'void'
let foobar: FooBar = {
    foo: "1",
    bar: 1
}

// no errors
let foo = {
    foo: "1"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文