What does the ampersand (&) mean in a TypeScript type definition?

时间:2016-07-11 23:12:42

标签: types typescript ampersand

On line 60359 of this type definition file, there is the following declaration:

type ActivatedEventHandler = (ev: Windows.ApplicationModel.Activation.IActivatedEventArgs & WinRTEvent<any>) => void;

What does the & sigil mean in this context?

2 个答案:

答案 0 :(得分:30)

答案 1 :(得分:4)

Typescript 中的交点类型

  • 在类型上下文中的 TS 中的 & 表示交叉类型。
  • 它将 2 个对象类型的所有属性合并在一起并创建一个新类型

示例:

type dog = {age: number, woof: Function};
type cat = {age: number, meow: Function};

// Type weird is an intersection of cat and dog
// it needs to have all properties of them combined
type weird = dog & cat;

const weirdAnimal: weird = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}}

interface extaprop {
    color: string
}

type catDog = weird & extaprop; // type now also has added color
const weirdAnimal2: catDog = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}, color: 'red'}


// This is different form a union type
// The type below means either a cat OR a dog
type dogOrCat = dog | cat;