首页 > 其他分享 >TypeScript singleton types All In One

TypeScript singleton types All In One

时间:2023-02-17 14:13:51浏览次数:37  
标签:www TypeScript singleton literal xgqfrms types

TypeScript singleton types All In One

单例类型 / 字面量类型

singleton types / literal types

  1. enum member types

  2. numeric literal types

  3. string literal types

type EventName = 'click' | 'scroll' | 'mousemove';

function handleEvent(ele: Element, event: EventName) {
    // do something
}

handleEvent(document.getElementById('btn') as HTMLElement, 'scroll');
// ✅
handleEvent(document.getElementById('btn') as HTMLElement, 'dblclick');
// ❌ Argument of type '"dblclick"' is not assignable to parameter of type 'EventName'.(2345)

(

标签:www,TypeScript,singleton,literal,xgqfrms,types
From: https://www.cnblogs.com/xgqfrms/p/17129918.html

相关文章