这可能是一个奇怪的问题,但我很好奇是否有可能创建一个需要其中一个属性的接口。

所以,例如……

interface Message {
    text: string;
    attachment: Attachment;
    timestamp?: number;
    // ...etc
}

interface Attachment {...}

在上面的情况下,我想确保文本或附件都存在。


我现在就是这么做的。我觉得它有点啰嗦(为slack输入botkit)。

interface Message {
    type?: string;
    channel?: string;
    user?: string;
    text?: string;
    attachments?: Slack.Attachment[];
    ts?: string;
    team?: string;
    event?: string;
    match?: [string, {index: number}, {input: string}];
}

interface AttachmentMessageNoContext extends Message {
    channel: string;
    attachments: Slack.Attachment[];
}

interface TextMessageNoContext extends Message {
    channel: string;
    text: string;
}

你可以使用联合类型来做到这一点:

interface MessageBasics {
  timestamp?: number;
  /* more general properties here */
}
interface MessageWithText extends MessageBasics {
  text: string;
}
interface MessageWithAttachment extends MessageBasics {
  attachment: Attachment;
}
type Message = MessageWithText | MessageWithAttachment;

如果你想同时允许文本和附件,你可以这样写

type Message = MessageWithText | MessageWithAttachment | (MessageWithText & MessageWithAttachment);

谢谢@ryan-cavanaugh,让我找到了正确的方向。

我有一个类似的情况,但是是数组类型。在语法上有点困难,所以我把它放在这里供以后参考:

interface BaseRule {
  optionalProp?: number
}

interface RuleA extends BaseRule {
  requiredPropA: string
}

interface RuleB extends BaseRule {
  requiredPropB: string
}

type SpecialRules = Array<RuleA | RuleB>

// or

type SpecialRules = (RuleA | RuleB)[]

// or (in the strict linted project I'm in):

type SpecialRule = RuleA | RuleB
type SpecialRules = SpecialRule[]

更新:

请注意,稍后在代码中使用声明的变量时,可能仍然会收到警告。然后可以使用(变量作为类型)语法。 例子:

const myRules: SpecialRules = [
  {
    optionalProp: 123,
    requiredPropA: 'This object is of type RuleA'
  },
  {
    requiredPropB: 'This object is of type RuleB'
  }
]

myRules.map((rule) => {
  if ((rule as RuleA).requiredPropA) {
    // do stuff
  } else {
    // do other stuff
  }
})

您可以为所需的条件创建一些接口,并将它们以如下类型连接起来:

interface SolidPart {
    name: string;
    surname: string;
    action: 'add' | 'edit' | 'delete';
    id?: number;
}
interface WithId {
    action: 'edit' | 'delete';
    id: number;
}
interface WithoutId {
    action: 'add';
    id?: number;
}

export type Entity = SolidPart & (WithId | WithoutId);

const item: Entity = { // valid
    name: 'John',
    surname: 'Doe',
    action: 'add'
}
const item: Entity = { // not valid, id required for action === 'edit'
    name: 'John',
    surname: 'Doe',
    action: 'edit'
}

有一些很酷的Typescript选项,你可以使用https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk

你的问题是:创造一个既存在“文本”又存在附件的界面。你可以这样做:

interface AllMessageProperties {
  text: string,
  attachement: string,
}

type Message = Omit<AllMessageProperties, 'text'> | Omit<AllMessageProperties, 'attachement'>;

const messageWithText : Message = {
  text: 'some text'
}

const messageWithAttachement : Message = {
  attachement: 'path-to/attachment'
}

const messageWithTextAndAttachement : Message = {
  text: 'some text',
  attachement: 'path-to/attachment'
}

// results in Typescript error
const messageWithOutTextOrAttachement : Message = {

}

好吧,经过一段时间的尝试和错误,我发现答案并没有像我的用例预期的那样工作。所以为了防止其他人有同样的问题,我想我应该分享我是如何让它工作的。我的界面是这样的:

export interface MainProps {
  prop1?: string;
  prop2?: string;
  prop3: string;
}

我正在寻找的是一个类型定义,它会说我们既不能定义prop1也不能定义prop2。我们可以定义prop1而不是prop2。最后有prop2,但prop1没有。下面是我找到的解决方案。

interface MainBase {
  prop3: string;
}

interface MainWithProp1 {
  prop1: string;
}

interface MainWithProp2 {
  prop2: string;
}

export type MainProps = MainBase | (MainBase & MainWithProp1) | (MainBase & MainWithProp2);

这工作完美,除了一个警告是,当我试图引用prop1或prop2在另一个文件中,我一直得到一个属性不存在TS错误。下面是我解决这个问题的方法:

import {MainProps} from 'location/MainProps';

const namedFunction = (props: MainProps) => {
    if('prop1' in props){
      doSomethingWith(props.prop1);
    } else if ('prop2' in props){
      doSomethingWith(props.prop2);
    } else {
      // neither prop1 nor prop2 are defined
    }
 }

我只是想分享一下,因为如果我遇到了这种奇怪的事情,那么其他人可能也会遇到。


我在为我的案例寻找答案时偶然发现了这个线索(要么是propA,要么是prob,要么都不是)。Ryan Fujiwara的回答几乎做到了,但我已经丢失了一些支票。

我的解决方案:

interface Base {   
  baseProp: string; 
}

interface ComponentWithPropA extends Base {
  propA: string;
  propB?: never;
}

interface ComponentWithPropB extends Base {
  propB: string;
  propA?: never;
} 

interface ComponentWithoutProps extends Base {
  propA?: never;
  propB?: never;
}

type ComponentProps = ComponentWithPropA | ComponentWithPropB | ComponentWithoutProps;

这个解决方案使所有检查保持原样。也许有人会发现这很有用:)


你可以更深入地使用@robstarbuck解决方案创建以下类型:

type Only<T, U> = {
  [P in keyof T]: T[P];
} & {
  [P in keyof U]?: never;
};

type Either<T, U> = Only<T, U> | Only<U, T>;

然后消息类型看起来像这样

interface MessageBasics {
  timestamp?: number;
  /* more general properties here */
}
interface MessageWithText extends MessageBasics {
  text: string;
}
interface MessageWithAttachment extends MessageBasics {
  attachment: string;
}
type Message = Either<MessageWithText, MessageWithAttachment>;

使用此解决方案,您可以轻松地在MessageWithText或MessageWithAttachment类型中添加更多字段,而无需在其他类型中排除它。


还可以为通用属性使用抽象类,而不是接口,以防止某人意外实现该接口。

abstract class BaseMessage {
  timestamp?: number;
  /* more general properties here */
  constructor(timestamp?: number) {
    this.timestamp = timestamp;
    /* etc. for other general properties */
  }
}
interface IMessageWithText extends BaseMessage {
  text: string;
  attachment?: never;
}
interface IMessageWithAttachment extends BaseMessage {
  text?: never;
  attachment: string;
}
type Message = IMessageWithText | IMessageWithAttachment;

到目前为止还没有人提到过,但我认为,无论谁偶然看到这一页,都可以考虑使用受歧视的工会。如果我正确理解OP代码的意图,那么它可能会像这样转换。

interface Attachment {}

interface MessageBase {
    type?: string;
    user?: string;
    ts?: string;
    team?: string;
    event?: string;
    match?: [string, {index: number}, {input: string}];
}

interface AttachmentMessageNoContext extends MessageBase {
    kind: 'withAttachments',
    channel: string;
    attachments: Attachment[];
}

interface TextMessageNoContext extends MessageBase {
    kind: 'justText', 
    channel: string;
    text: string;
}

type Message = TextMessageNoContext | AttachmentMessageNoContext

const textMessage: Message = {
  kind: 'justText',
  channel: 'foo',
  text: "whats up???" 
}

const messageWithAttachment: Message = {
  kind: 'withAttachments',
  channel: 'foo',
  attachments: []
}

现在Message接口需要附件或文本,这取决于kind属性。


简单的“二选一”的例子:

type Props =
  | { factor: Factor; ratings?: never }
  | { ratings: Rating[]; factor?: never }

不使用扩展

使用这里描述的XOR: https://stackoverflow.com/a/53229567/8954109

// Create a type that requires properties `a` and `z`, and one of `b` or `c`
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;

interface Az {
  a: number;
  z: number;
}

interface B {
  b: number;
}

interface C {
  c: number;
}

type XorBC = XOR<B, C>;
type AndAzXorBC = Az & XorBC;
type MyData = AndAzXorBC;

const ok1: MyData = { a: 0, z: 1, b: 2 };
const ok2: MyData = { a: 0, z: 1, c: 2 };
const badBothBC: MyData = {
  a: 0, z: 1,
  b: 2,
  c: 3
};
const badNoBC: MyData = { a: 0, z: 1 };
const badNoZ: MyData = { a: 0, b: 2 };

对无效类型产生以下错误:

src/App.tsx:30:7 - error TS2322: Type '{ a: number; z: number; b: number; c: number; }' is not assignable to type 'AndAzXorBC'.
  Type '{ a: number; z: number; b: number; c: number; }' is not assignable to type 'Az & Without<C, B> & B'.
    Type '{ a: number; z: number; b: number; c: number; }' is not assignable to type 'Without<C, B>'.
      Types of property 'c' are incompatible.
        Type 'number' is not assignable to type 'undefined'.

30 const badBothBC: MyData = {
src/App.tsx:35:7 - error TS2322: Type '{ a: number; z: number; }' is not assignable to type 'AndAzXorBC'.
  Type '{ a: number; z: number; }' is not assignable to type 'Az & Without<C, B> & B'.
    Property 'b' is missing in type '{ a: number; z: number; }' but required in type 'B'.

35 const badNoBC: MyData = { a: 0, z: 1 };
         ~~~~~~~

  src/App.tsx:17:3
    17   b: number;
         ~
    'b' is declared here.
src/App.tsx:36:7 - error TS2322: Type '{ a: number; b: number; }' is not assignable to type 'AndAzXorBC'.
  Type '{ a: number; b: number; }' is not assignable to type 'Az & Without<C, B> & B'.
    Property 'z' is missing in type '{ a: number; b: number; }' but required in type 'Az'.

36 const badNoZ: MyData = { a: 0, b: 2 };
         ~~~~~~

  src/App.tsx:13:3
    13   z: number;
         ~
    'z' is declared here.

下面是一个非常简单的实用程序类型,我使用它来创建一个新类型,允许多个接口/类型中的一个被称为InterfacesUnion:

export type InterfacesUnion<Interfaces> = BuildUniqueInterfaces<UnionToIntersection<Interfaces>, Interfaces>;

type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;

export type BuildUniqueInterfaces<CompleteInterface, Interfaces> = Interfaces extends object
  ? AssignNever<CompleteInterface, Interfaces>
  : never;

type AssignNever<T, K> = K & {[B in Exclude<keyof T, keyof K>]?: never};

它可以这样使用:

type NewType = InterfacesUnion<AttachmentMessageNoContext | TextMessageNoContext>

它通过接受接口/类型的联合来运行,构建一个包含它们所有属性的单一接口,并返回相同的接口/类型的联合,其中每个接口都包含其他接口拥有而它们没有的附加属性,这些属性被设置为可选的never ([propertyName]?:永远不要)。

有例子/解释的游乐场


我进一步压缩了@Voskanyan David的解决方案,得到了我个人认为非常简洁的解决方案:

你仍然需要在某个地方定义Only<>和Either<>

type Only<T, U> = {
    [P in keyof T]: T[P];
} & {
    [P in keyof U]?: never;
};

type Either<T, U> = Only<T, U> | Only<U, T>

之后,可以在没有任何中间接口/类型的情况下定义它:

type Message {
    type?: string;
    channel?: string;
    user?: string;
    // ...etc
} & Either<{message: string}, {attachment: Attachment}>