【问题标题】:What means '&' of statement 'export type T1 = object & ComponentOptions<T1, T2>' in Typescript?Typescript中语句'export type T1 = object and Component Options<T1,T2>'的'&'是什么意思?
【发布时间】:2020-05-18 10:36:27
【问题描述】:

我是typescript的新手,我正在尝试阅读Vue的源代码,但是语法很混乱,下面代码中的'&'是什么意思,任何人都可以告诉我如何在官方文档中找到它?

/**
 * This type should be used when an array of strings is used for a component's `props` value.
 */
export type ThisTypedComponentOptionsWithArrayProps<V extends Vue, Data, Methods, Computed, PropNames extends string> =
  object &
  ComponentOptions<V, DataDef<Data, Record<PropNames, any>, V>, Methods, Computed, PropNames[], Record<PropNames, any>> &
  ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Record<PropNames, any>>>>;

【问题讨论】:

标签: typescript vue.js


【解决方案1】:

“&”是用于创建交集类型的运算符。

交集类型是包含属于交集中两种类型的属性的类型。

type A = {
  foo: string,
  bar: number,
}

type B = {
  baz: boolean,
  foo: string,
}

type C = A & B;
// type C = { foo: string, bar: number, baz: boolean }

您可以在此处阅读更多信息:

https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types

这里:

https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases

希望对我有所帮助!

【讨论】:

  • 我的错,我描述了联合类型。但我现在修好了。感谢您指出这一点
【解决方案2】:

它是一种交集类型,它允许将多种类型组合为一个。

例如:

interface A {
    a: string;
}

interface B {
    b: boolean;
}

type C = A & B;

const value: C = {
    a: 'a',
    b: true
};

类型 C 是 A 和 B 中属性的组合。

查看官方文档以获取更多参考: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2018-10-25
    • 2010-12-18
    • 2015-11-11
    • 2018-02-10
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多