打字稿 - 如何结合联合和交集类型

lee*_*ell 6 typescript reactjs union-types

我有以下组件:

export enum Tags {
  button = 'button',
  a = 'a',
  input = 'input',
}

type ButtonProps = {
  tag: Tags.button;
} & ({ a?: string; b?: undefined } | { a?: undefined; b?: string }) &
  JSX.IntrinsicElements['button'];

type AnchorProps = {
  tag: Tags.a;
} & ({ a?: string; b?: undefined } | { a?: undefined; b?: string }) &
  JSX.IntrinsicElements['a'];

type InputProps = {
  tag: Tags.input;
} & ({ a?: string; b?: undefined } | { a?: undefined; b?: string }) &
  JSX.IntrinsicElements['input'];

type Props = ButtonProps | AnchorProps | InputProps;

const Button: React.FC<Props> = ({ children, tag }) => {
  if (tag === Tags.button) {
    return <button>{children}</button>;
  }
  if (tag === Tags.a) {
    return <a href="#">{children}</a>;
  }
  if (tag === Tags.input) {
    return <input type="button" />;
  }
  return null;
};

// In this instance the `href` should create a TS error but doesn't...
<Button tag={Tags.button} href="#">Click me</Button>

// ... however this does
<Button tag={Tags.button} href="#" a="foo">Click me</Button>
Run Code Online (Sandbox Code Playgroud)

为了能够提出这个问题,这已经被剥离了一点。关键是我正在尝试一个有区别的联合以及交叉点类型。我正在尝试根据标签值实现所需的道具。因此,如果Tags.button使用,则使用 JSX 的按钮属性(并且href在上面的示例中应该创建一个错误,因为它在button元素上是不允许的)-但是另一个复杂性是我想要ab使用它们,但它们不能一起使用-因此交叉点类型。

我在这里做错了什么,为什么在添加aorb属性时类型只能按预期工作?

更新

我添加了一个带有示例的操场,以显示它何时应该出错以及何时应该编译。

操场

hlf*_*rmn 5

在您的示例中,有两个问题必须解决,并且都源于相同的“问题”(功能)。

在 Typescript 中,以下内容并不像我们有时想要的那样工作:

interface A {
  a?: string;
}

interface B {
  b?: string;
}

const x: A|B = {a: 'a', b: 'b'}; //works
Run Code Online (Sandbox Code Playgroud)

您想要的是从 A 中明确排除 B,从 B 中排除 A - 这样它们就不能一起出现。

这个问题讨论了类型的“异或”,并建议使用包ts-xor,或编写自己的包。这是来自那里的答案的示例(在 ts-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)

现在,有了这个,我们终于可以解决您的问题:

interface A {
  a?: string;
}

interface B {
  b?: string;
}

interface C {
  c?: string;
}

type CombinationProps = XOR<XOR<A, B>, C>;

let c: CombinationProps;
c = {}
c = {a: 'a'}
c = {b: 'b'}
c = {c: 'c'}
c = {a: 'a', b: 'b'} // error
c = {b: 'b', c: 'c'} // error
c = {a: 'a', c: 'c'} // error
c = {a: 'a', b: 'b', c: 'c'} // error
Run Code Online (Sandbox Code Playgroud)

更具体地说,您的类型将是:

interface A {a?: string;}
interface B {b?: string;}

type CombinationProps = XOR<A, B>;

type ButtonProps = {tag: Tags.button} & JSX.IntrinsicElements['button'];
type AnchorProps = {tag: Tags.a} & JSX.IntrinsicElements['a'];
type InputProps = {tag: Tags.input} & JSX.IntrinsicElements['input'];

type Props = CombinationProps & XOR<XOR<ButtonProps,AnchorProps>, InputProps>;
Run Code Online (Sandbox Code Playgroud)