Created
April 15, 2025 13:36
-
-
Save paleite/984962a5469725f52a5ac671b3385ace to your computer and use it in GitHub Desktop.
Exactly matching keys in object
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
// #region Ensure CharacterData has the exact same keys as characterProperties | |
type TypeCheck<T extends true> = T; | |
// KeyDifference will be `never` when CharacterData's keys exactly match those | |
// of characterPropertiesMap, otherwise it will include the mismatching keys | |
type KeyDifference = Exclude< | |
keyof typeof characterPropertiesMap | keyof CharacterData, | |
keyof typeof characterPropertiesMap & keyof CharacterData | |
>; | |
// Typescript will throw a compile error with the mismatching keys (if any) | |
export type AssertExactKeys = TypeCheck< | |
KeyDifference extends never | |
? true | |
: { | |
error: "Keys don't match exactly"; | |
missingInCharacterDataType: Exclude< | |
keyof typeof characterPropertiesMap, | |
keyof CharacterData | |
>; | |
extraInCharacterDataType: Exclude< | |
keyof CharacterData, | |
keyof typeof characterPropertiesMap | |
>; | |
} | |
>; | |
// #endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment