Last active
March 24, 2023 08:57
-
-
Save naporin0624/4599f100a4e0a850571fedeca73eb62c to your computer and use it in GitHub Desktop.
再帰的に Objct を Omit する型
This file contains 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
// https://www.typescriptlang.org/play?ts=5.0.2#code/PQKhCgAIUgVALApgW0QeQA4BcCWB7AOwGdJA7BkgwDoBjPAGzwCczALhMDAlQAwZBFBkCSGSPZDiyRA6gyB9BkAiDFBDBwOAlkSMAZgENqiOElSZchEgG8okExgBckIyevWARuoDWAczwBXAgBMLVm75PJVeQsiLEZ5J2M-GyJEWk9VRgBPYNDwyN8AXwAadJtaBkZvXKiAoMgQsIIIqN8YuI8E5PLUquKTbLbIangErCKavPomAEYUyuqBk3ymACYxtMmpocYAZn7Fm1ULAldkWyVOmttt3f3GQ6jqE72DjYzO+5t7+-AsRIxNAEkCDFdhAF5IAAFRiILC4RIAHgQKHQ2HwxAAfOBXu9NGg-r8sMNIICACKIRAYNCCLDQ7RwvTELKQADaACIqNNGPSALrI4DAawAPQA-KiPpAMVgsTNcZACUSSUJybDdAiiDSGUyeowsDRlsN6TTGTRVermTNtZBdd1ehqCitKLZjab9RamFbqGyOVyTHyUSi3oKQWCIdDEeKfCZaQBpSDySAORCJPDKOCsiy+8E4KGwMPs8AZSAAMksL05XWWkG4gAjbcRSb2aACC4uT-phOnh+mR4ELgEIfQAaytxAJYMFDIgFP3QAa0YAZBkAN06Aaa8RIAzBhEgD8GQDGDNOJIBohkAbU6AaUMBZoAEJ10EpqHBigWaVkxuUhXKtk6s1q+nIjKtqvAsKknAAN00gJ2N2YAB9mnGSBANsPB6EQVQCBAyAdjoOgYPcDxEGUeREA8GCiESPZ6BgvFVEUGDbBwJx5CwbcJUJDBQxjIgAyDdIwwjaDo1jeNYETSAAApw0QAAPRRPBICpwkgXk4AzSB+MEjwSBBHB3y-MTIHDCwAAMABIDFDDJKC0yVqNo6EM0RDI1MgbZEC-RgAEoYJ43MgPCGys1pVi4wTL00UgAB9M9oRpGjEhIaTECEpyqkDQF61TSF-OPJjI3c9jOPTUNWSkgSwtk18FKET9NAuDYTHEtKMqK4qLCCkKsvCzTtMckSql0rT5GUJQ4ECOgzMgCriuscS-NJYz0ppWAusDPr+osiT0qzQLaMRZEXx8gz-NgBbgsymThJaJxaXZcVqu27KSFpNqOoACUgjwaUoe6uIu5hxpwOgbNZKbpum0qupO8LaSapw7vuwGDo+r6Ie+3y1uGoaZQ2yBrtUDC80BxExom3rIexxYLDh88aSRlGIqcZEcfJ6wLFgCiYfhzaap2yiiWqgMDqi6GqPW+nWyAA | |
/** | |
* ThemeOptions の p.color の部分だけを omit したい | |
*/ | |
interface ThemeOptions { | |
p: { | |
backgound: { | |
main: string | |
secondary: string | |
}, | |
color: { | |
main: string | |
secondary: string | |
}, | |
chart: { | |
color1: string | |
color2: string | |
color3: { | |
a: number | |
b: number | |
c: number | |
} | |
} | |
} | |
} | |
type Input = Prettiy<ThemeOptions> | |
type Output1 = DeepOmit<ThemeOptions, ["p.color"]> | |
// ^? | |
type Output2 = DeepOmit<ThemeOptions, ["p.chart.color1", "p.chart.color2", "p.chart.color3.b", "p.chart.color3.c"]> | |
// ^? | |
type Prettiy<T> = { | |
[K in keyof T]: Prettiy<T[K]> | |
} & {} | |
// color だけ消したい | |
type A = Prettiy<ThemeOptions> | |
// 簡単だけど p の知識が独立してしまっている状態 | |
type B = Prettiy<{ | |
p: Omit<ThemeOptions["p"], "chart"> | |
}> | |
type Primitive = number | string | boolean | null | undefined | symbol | Date | bigint | |
type DeepKeys<T> = { | |
[K in keyof T]: (K extends string ? T[K] extends Primitive ? K : `${K}.${DeepKeys<T[K]>}` : never) | (K & string) | |
}[keyof T] | |
type _Omit<T, Keys extends string> = Prettiy<Omit<{ | |
[K in keyof T]: T[K] extends Primitive | |
? T[K] | |
: Keys extends `${K & string}.${infer Tail}` | |
? _Omit<T[K], Tail> | |
: T[K] | |
}, Keys>> | |
type _DeepOmit<T, Keys extends string[]> = Keys extends [infer Head, ...(infer Tail)] | |
? Tail extends [string, ...string[]] | |
? _DeepOmit<_Omit<T, Head & string>, Tail> | |
: _Omit<T, Head & string> | |
: T | |
type DeepOmit<T, Keys extends DeepKeys<T>[]> = _DeepOmit<T, Keys> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment