0

I new new to typescript, I'm try to reading source code of Vue, but confused the grammar, what means '&' in following code, and any guys can tell me how find it the official documentation?

/**
 * 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>>>>;
Ronel Wu
  • 5
  • 2
  • Does this answer your question? [Typescript: understanding union and Intersection types](https://stackoverflow.com/questions/61370779/typescript-understanding-union-and-intersection-types) – Meirion Hughes May 18 '20 at 10:52

2 Answers2

0

It's an intersection type, it allows combining multiple types into one.

For example:

interface A {
    a: string;
}

interface B {
    b: boolean;
}

type C = A & B;

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

Type C is a combination of the properties in A and B.

Check official documentation for additional reference: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types

Gorka Hernandez
  • 3,890
  • 23
  • 29
0

The '&' is an operator used for creating Intersection Types.

A intersection type is a type that contains attributes belonging to both types in the intersection.

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

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

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

You can read more about it here:

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

and here:

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

I hope I have helped!

Leonardo da Silva
  • 1,285
  • 2
  • 10
  • 25