Last active
March 7, 2019 03:49
-
-
Save luggage66/feb1dc4dddc8f38bf01f051378a055b6 to your computer and use it in GitHub Desktop.
TS to change a type based on a "config type"
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
// original code by MadaraUchiha | |
// Just defines named types we can reference by key. Never used as a real value type. | |
interface Operations<T> { | |
'delete': never; | |
'nullable': T | null; | |
'box': {value: T}; | |
}; | |
type MemberConfig<T> = keyof Operations<T>; | |
type ObjectConfig<T> = { | |
[K in keyof T]?: MemberConfig<T[K]> | |
}; | |
type OmitNevers<T> = Pick<T, {[K in keyof T]: T[K] extends never ? never : K}[keyof T]>; | |
type ApplyConfig<T, C extends ObjectConfig<T>> = OmitNevers<{ | |
[K in keyof T]: C[K] extends MemberConfig<T[K]> ? Operations<T[K]>[C[K]] : T[K] | |
}>; | |
interface OriginalType { | |
foo: string; | |
bar: boolean; | |
baz: number; | |
bux: Date; | |
} | |
// The money shot | |
type NewType = ApplyConfig<OriginalType, { | |
foo: 'delete'; | |
bar: 'box'; | |
baz: 'nullable'; | |
// don't specify bux, it should pass through unchanged. | |
}>; | |
declare const x: NewType; | |
const a = x.foo; // error, we deleted foo | |
const b = x.bar.value; | |
const c = x.baz.toFixed(2); // error, might be calling .toFixed() on null | |
const d = x.bux; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment