TypeScript类型定义中的符号 "ampersand (&)" 代表什么含义?
P粉668804228
P粉668804228 2023-08-22 17:25:53
[JavaScript讨论组]

在此类型定义文件的60359行,有以下声明:

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

在这个上下文中,&符号表示什么意思?

P粉668804228
P粉668804228

全部回复(1)
P粉578343994

在Typescript中的交集类型

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

例子:

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

// 类型weird是cat和dog的交集
// 它需要具有它们的所有属性的组合
type weird = dog & cat;

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

interface extaprop {
    color: string
}

type catDog = weird & extaprop; // 类型现在还添加了color属性
const weirdAnimal2: catDog = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}, color: 'red'}


// 这与联合类型不同
// 下面的类型表示猫或狗
type dogOrCat = dog | cat;
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号