28333-medium-public-type

Back

type PublicType<T extends object> = {
  [k in keyof T as k extends `_${any}` ? never : k]: T[k]
}

Solution by wendao-liu #35155

type PublicType<T extends object> = {[K in keyof T as K extends `_${string}` ? never : K]: T[K]}

Solution by Keith-Web3 #34849

// your answers
type PublicType<T extends object> = { 
  [K in keyof T as K extends `_${infer L}` ? never : K]: T[K]
}

Solution by gobielJonathan #34485

type PublicType<T extends object> = {
  [K in keyof T as `${K & string}` extends `_${string}` ? never : K]: T[K]
}

Solution by ouzexi #34169

type PublicType<T extends object> = {
  [Key in keyof T as Key extends `_${infer R}` ? never : Key]: T[Key]
}

Solution by drylint #34144

  type PublicType<T extends object> = {
    [P in keyof T as P extends `_${any}` ? never : P]: T[P]
  }

Solution by sunupupup #33488

type PublicType<T extends object> =
  {
    [K in keyof T as K extends `_${string}` ? never : K]: T[K]
  }

Playground

Solution by teamchong #32996

type RemovePrivateFields<T extends Record<string, unknown>> = {
    [K in keyof T as K extends `_${infer Rest}` ? never : K]: T[K]
}

type Obj = {
    username: string
    _password: string
}

type Result = RemovePrivateFields<Obj>

Solution by ZhulinskiiDanil #32711

type PublicType<T extends object> = {
  [P in keyof T as P extends `_${string}` ? never : P]: T[P];
};

Solution by Vampirelee #32587

type PublicType<T extends object> = Omit<T, `_${string}`>;

Solution by StavNoyAkur8 #31449

type PublicType<T extends object> = Omit<T, `_${string}`>;

Solution by bigcreate #31190

type PublicType<T extends object> =
  keyof T extends infer K extends keyof T
    ? Omit<T, K extends K
      ? K extends `_${infer _}`
        ? K
        : never
      : never>
    : never

Solution by milletlovemouse #31037

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${string}` ? never : K]: T[K];
};

Solution by leejaehyup #30944

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${string}` ? never : K]: T[K]
}

Solution by dreamluo-plus #30703

type PublicType<T extends object> = {
  [P in keyof T as P extends `_${string}` ? never : P]: T[P]
}

Solution by zhangqiangzgz #30290

type PublicType<T extends object> = {
    [Key in keyof T as Key extends `_${string}` ? never : Key ]:T[Key]
}

Solution by idebbarh #30174

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${string}`? never:K]:T[K]
}

Solution by jiangshanmeta #29711

type PublicType<T extends object> = {
  [P in keyof T as P extends `_${infer _}` ? never : P]: T[P]
}

Solution by jazelly #29625

// your answers

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${string}` ? never : K] : T[K]
}

Solution by kerolossamir165 #29369

type PublicType<T extends object> = { [k in keyof T as k extends `_${infer L}` ? never: k]: T[k] }

Solution by MrRENGE #29281

type PublicType<T extends object> = {
  [Key in keyof T as Key extends `_${string}` ? never : Key]: T[Key]
}

Solution by RainbowIsPerfect #29030

type PublicType<T extends object> = {
  [P in keyof T as Exclude<P, `_${string}`>]: T[P] extends Record<
    keyof any,
    any
  >
    ? PublicType<T[P]>
    : T[P];
};

Solution by DoubleWoodLin #28932

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${infer _}` ? never : K]: T[K];
};

Solution by JohnLi1999 #28902

type PublicType<T extends object, Key extends keyof T = keyof T, OmitKey
  = Key extends Key ?
  `${Key & string}` extends `_${infer Word}` ?
  Word extends any ?
  Key : never : never : never>
  = Omit<T, OmitKey & Key>

Solution by EGyoung #28815

遍历对象的每个 Key,然后通过模板字符串语法来判断,过滤掉不符合要求的 key

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${infer R}` ? never : K]: T[K]
}

Solution by linjunc #28603

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${string}` ? never : K]: T[K];
};

Solution by jakub791 #28474

type PublicType<T extends object> = {
  [K in keyof T as K extends `_${string}` ? never : K]: T[K]
}

Solution by XkSuperCool #28452

type PublicType<T extends object> = { [P in keyof T as P extends `_${any}` ? never : P]: T[P] };

Solution by E-uler #28419

type PublicType<T extends object> = {
  [p in keyof T as p extends `_${string}` ? never : p]: T[p]; 
}

Solution by omittee #28403