Skip to content

Instantly share code, notes, and snippets.

@paleite
Created April 15, 2025 13:36
Show Gist options
  • Save paleite/984962a5469725f52a5ac671b3385ace to your computer and use it in GitHub Desktop.
Save paleite/984962a5469725f52a5ac671b3385ace to your computer and use it in GitHub Desktop.
Exactly matching keys in object
// #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