从联合中提取鉴别值的类型

时间:2020-01-13 13:35:26

标签: typescript

给出以下代码:

document.getElementById('exampleButton').innerHTML = '<img src="' + theme_directory + '/svg/icons/cursor.svg" width="32" height="32" title="">Example';

是否有任何方法可以从const disconnected = () => ({ type: 'DISCONNECTED' } as const); const connected = () => ({ type: 'CONNECTED' } as const); type Events = ReturnType<typeof disconnected | typeof connected>; 中提取类型,该类型等于联合的每个成员的Events属性中的一组值。我希望得到类似type

的信息

1 个答案:

答案 0 :(得分:3)

一种非常简单的方法是:

type Events = ReturnType<typeof disconnected | typeof connected>['type']; //'DISCONNECTED' | 'CONNECTED'