Last active
January 21, 2021 15:55
-
-
Save EricRabil/baacd491dbd2ab2c2916643d15f18155 to your computer and use it in GitHub Desktop.
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
| Object.assign(typeof window === 'object' ? window : typeof global === 'object' ? global : typeof globalThis === 'object' ? globalThis : {}, { | |
| hasKeys: function<Keys extends string[], Obj extends object>(obj: Obj, keys: Keys): obj is Obj & { | |
| [key in ValueOf<Keys>]: unknown | |
| } { | |
| return keys.every(key => key in obj) | |
| } | |
| }) |
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
| /** | |
| * This file augments global types to make more sense and be more practical | |
| */ | |
| /** | |
| * I think it only makes sense Object.keys() returns an array of keyof object | |
| */ | |
| interface ObjectConstructor { | |
| keys<T>(obj: T): Array<keyof T> | |
| } | |
| /** | |
| * I think it only makes sense that you can check any string against an array of string literals | |
| * | |
| * Normally, if an array resolves to something like | |
| * | |
| * const x: ("rawr" | "mami")[] | |
| * | |
| * x.includes("notrawr") will not work | |
| * | |
| * That doesn't make sense. bye. | |
| */ | |
| interface Array<T> { | |
| includes(searchElement: T extends string ? string : T, fromIndex?: number): boolean | |
| } | |
| interface ReadonlyArray<T> { | |
| includes(searchElement: T extends string ? string : T, fromIndex?: number): boolean; | |
| } | |
| /** | |
| * You should have a type-safe way of checking if an object has many keys | |
| */ | |
| type ValueOf<T> = T extends any[] ? T[number] : T[keyof T] | |
| function hasKeys<Keys extends string[], Obj extends object>(obj: Obj, keys: Keys): obj is Obj & { | |
| [key in ValueOf<Keys>]: unknown | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment