https://zhuanlan.zhihu.com/p/136254808

    1. // 这里定义一个工具类型,简化代码
    2. type ReplaceValByOwnKey<T, S extends any> = { [P in keyof T]: S[P] };
    3. // shift action
    4. type ShiftAction<T extends any[]> = ((...args: T) => any) extends ((arg1: any, ...rest: infer R) => any) ? R : never;
    5. // unshift action
    6. type UnshiftAction<T extends any[], A> = ((args1: A, ...rest: T) => any) extends ((...args: infer R) => any) ? R : never;
    7. // pop action
    8. type PopAction<T extends any[]> = ReplaceValByOwnKey<ShiftAction<T>, T>;
    9. // push action
    10. type PushAction<T extends any[], E> = ReplaceValByOwnKey<UnshiftAction<T, any>, T & { [k: string]: E }>;
    11. // test ...
    12. type tuple = ['vue', 'react', 'angular'];
    13. type resultWithShiftAction = ShiftAction<tuple>; // ["react", "angular"]
    14. type resultWithUnshiftAction = UnshiftAction<tuple, 'jquery'>; // ["jquery", "vue", "react", "angular"]
    15. type resultWithPopAction = PopAction<tuple>; // ["vue", "react"]
    16. type resultWithPushAction = PushAction<tuple, 'jquery'>; // ["vue", "react", "angular", "jquery"]