how to remove properties via mapped type in TypeScript

TS 4.1

You can use as clauses in mapped types to filter out properties in one go:

type Methods<T> = { [P in keyof T as T[P] extends Function ? P : never]: T[P] };
type A_Methods = Methods<A>;  // { render: () => void; }

When the type specified in an as clause resolves to never, no property is generated for that key. Thus, an as clause can be used as a filter [.]

Further info: Announcing TypeScript 4.1 – Key Remapping in Mapped Types

Playground

Leave a Comment