Yuzhe's Blog

yuzhes

Public Type

Public Type

Problem Link

Problem

Remove the key-value pairs that start with an underscore (_) from an object type.

type T = PublicType<{ _name: string; _age: number; email: string }>
// { email: string }

Solution

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

How it works:

  1. Use a mapped type with key remapping (as).
  2. For each key K, check if it matches the template literal `_${string}`.
  3. If it starts with _, remap to never — which removes the key from the output.
  4. Otherwise keep the key as-is.

Key Takeaways