Skip to content

Instantly share code, notes, and snippets.

// see https://repl.it/repls/AnimatedPerfectMinimalsystem
// 1 Transform the type to flag all the undesired keys as 'never'
type FlagExcludedType<Base, Type> = { [Key in keyof Base]: Base[Key] extends Type ? never : Key };
// 2 Get the keys that are not flagged as 'never'
type AllowedNames<Base, Type> = FlagExcludedType<Base, Type>[keyof Base];
// 3 Use this with a simple Pick to get the right interface, excluding the undesired type
type OmitType<Base, Type> = Pick<Base, AllowedNames<Base, Type>>;
// see https://repl.it/repls/TemporalFrigidScientificcomputing
type DataPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
type DataPropertiesOnly<T> = {
[P in DataPropertyNames<T>]?: T[P] extends object ? DTO<T[P]> : T[P]
};
export type DTO<T> = DataPropertiesOnly<T>;