Skip to content

Instantly share code, notes, and snippets.

@fostyfost
Created June 25, 2024 15:33
Show Gist options
  • Save fostyfost/ffb282aecf0a362b36754b753c7ebf60 to your computer and use it in GitHub Desktop.
Save fostyfost/ffb282aecf0a362b36754b753c7ebf60 to your computer and use it in GitHub Desktop.
Distributive Omit
type Album = {
id: string // same per type
title: string // same per type
genre: string // different
}
type CollectorEdition = {
id: string // same per type
title: string // same per type
limitedEditionFeatures: string[] // different
}
type DigitalRelease = {
id: string // same per type
title: string // same per type
digitalFormat: string // different
}
type MusicProduct = Album | CollectorEdition | DigitalRelease
type MusicProductWithoutId = Omit<MusicProduct, "id"> // { title: string }
// Expected:
type MusicProductWithoutId1 = Omit<Album, "id"> | Omit<CollectorEdition, "id"> | Omit<DigitalRelease, "id">
// Actual:
type MusicProductWithoutId2 = {
title: string
}
type MusicProductWithoutId = DistributiveOmit<MusicProduct, "id">
// type MusicProductWithoutId = Omit<Album, "id"> | Omit<CollectorEdition, "id"> | Omit<DigitalRelease, "id">
export type DistributiveOmit<T, K extends PropertyKey> = T extends any ? Omit<T, K> : never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment