Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

typescript怎么往MouseEvent类型里面增加一个属性

如图,怎么往MouseEvent类型里面增加一个属性?我想在函数里面给ev增加一个b属性,值为'10',这样写报错了,应该怎么写呢
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

利用 TS 会合并同名类型的特性:

interface MouseEvent {
    b?: string;
}

document.addEventListener('click', function(ev) {
    ev.b = '10';
    console.log(ev.b);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...