Created
June 25, 2024 15:33
-
-
Save fostyfost/ffb282aecf0a362b36754b753c7ebf60 to your computer and use it in GitHub Desktop.
Distributive Omit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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