how to remove properties via mapped type in TypeScript

前端 未结 2 1047
有刺的猬
有刺的猬 2020-12-09 04:48

Here is the code

class A {
    x = 0;
    y = 0;
    visible = false;
    render() {

    }
}

type RemoveProperties = {
    readonly [P in keyof T         


        
2条回答
  •  一整个雨季
    2020-12-09 04:58

    You can use the same trick as the Omit type uses:

    // We take the keys of P and if T[P] is a Function we type P as P (the string literal type for the key), otherwise we type it as never. 
    // Then we index by keyof T, never will be removed from the union of types, leaving just the property keys that were not typed as never
    type JustMethodKeys = ({[P in keyof T]: T[P] extends Function ? P : never })[keyof T];  
    type JustMethods = Pick>; 
    

提交回复
热议问题