Created
March 11, 2024 23:30
-
-
Save joemaffei/f85c91befd407ffd642be5cc95b740d1 to your computer and use it in GitHub Desktop.
Type-safe key mirror
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
function keyMirror<Obj>(...args: (keyof Obj)[]): Readonly<{ [Key in keyof Obj]: Key }> { | |
let obj = {}; | |
for (let arg of args) { | |
Object.defineProperty(obj, arg, { | |
configurable: false, | |
enumerable: true, | |
value: arg, | |
writable: false, | |
}); | |
} | |
return obj as Readonly<{ [Key in keyof Obj]: Key }>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: