[Typescript] Get class properties type in union

For example there is a clas:

export class ModifierState {
        /**
         * Returns the modifier state applicable to the keyboard event given.
         * @param event The keyboard event to read.
         * @returns The current state of keyboard modifiers.
         */
        static fromKeyboardEvent(event: KeyboardEvent): ModifierState;

        /**
         * Whether shift is currently pressed.
         */
        shift: boolean;

        /**
         * Whether ctrl is currently pressed.
         */
        ctrl: boolean;

        /**
         * Whether alt is currently pressed.
         */
        alt: boolean;

        /**
         * Whether meta (apple key) is currently pressed.
         */
        meta: boolean;

        /**
         * Whether hyper (windows key) is currently pressed.
         */
        hyper: boolean;
    }
}

I want to get properties in union: 

'alt' | 'ctrl' | 'hyper' | 'meta' | 'shift'
 
Solution:
type Properties<T extends {new (...args: any[]): any}> = keyof InstanceType<T>
type ModifierProperties = Properties<typeof ModifierState>
 
posted @ 2022-10-13 18:13  Zhentiw  阅读(31)  评论(0编辑  收藏  举报