相关疑难解决方法(0)

打字稿界面 - 可以使"一个或另一个"属性成为必需吗?

可能是一个奇怪的问题,但我很好奇是否可以创建一个需要一个属性或另一个属性的接口.

所以,例如......

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

interface Attachment {...}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我想确定是存在text还是attachment存在.

希望这是有道理的.

提前致谢!


编辑:这就是我现在正在做的事情.认为它有点冗长(输入botkit for slack).

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;
}
Run Code Online (Sandbox Code Playgroud)

typescript

34
推荐指数
9
解决办法
2万
查看次数

带XOR的TypeScript接口,{bar:string} xor {can:number}

我怎么说我想要一个接口是一个或另一个,但不是两者兼有?

interface IFoo {
    bar: string /*^XOR^*/ can: number;
}
Run Code Online (Sandbox Code Playgroud)

schema interface xor typescript typescript2.0

12
推荐指数
3
解决办法
1623
查看次数

Typescript Discriminated Union允许无效状态

我试图使用Typescript Discriminated Union来模拟异步加载数据时的一个相当常见的场景:

type LoadingState = { isLoading: true; }
type SuccessState = { isLoading: false; isSuccess: true; }
type ErrorState =   { isLoading: false; isSuccess: false; errorMessage: string; }

type State = LoadingState | SuccessState | ErrorState;
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这应该根据类型定义限制允许的值组合.但是,类型系统很乐意接受以下组合:

const testState: State = {
    isLoading: true,
    isSuccess: true,
    errorMessage: "Error!"
}
Run Code Online (Sandbox Code Playgroud)

我希望这里有一个错误.有什么我缺少或在某种程度上滥用类型定义?

discriminated-union typescript

8
推荐指数
1
解决办法
471
查看次数

为什么 Typescript 允许将 `{a: 1, b: 2}` 分配给类型 `{a: any} | {b:任何}`?

考虑这个代码:

let foo: {a: any} | {b: any} = {a: 1, b: 2};
Run Code Online (Sandbox Code Playgroud)

我希望 TypeScript 拒绝此代码,因为此联合类型意味着第一种类型或第二种类型,并且该值不可分配给它们中的任何一种,因此类型检查应拒绝它。但事实上它过去了,为什么呢?

(对于背景,上述类型旨在成为“此属性或该属性,但不是两者都类型”。请参阅Typescript 接口 - 是否需要“一个或另一个”属性?

typescript

6
推荐指数
1
解决办法
400
查看次数

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

我有以下组件:

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?: …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs union-types

6
推荐指数
1
解决办法
1146
查看次数

在打字稿中提供一种对象类型

如果我输入以下内容:

interface A {
    x: number
}
interface B {
    y: number
}

type Z = A | B;

// here it allows me to create a variable of type Z with both members of type A and B.
let z: Z = {
    x: 5,
    y: 6,
}
Run Code Online (Sandbox Code Playgroud)

我无法确保 Z 类型的对象确实包含 A 的所有成员但不包含 B 的成员(或相反)。使用 TypeScript 有可能吗?经过大量研究,我倾向于“不”的答案,但我不确定。

types object-literal typescript

5
推荐指数
1
解决办法
658
查看次数