Skip to content

Instantly share code, notes, and snippets.

@devdoomari3
Created July 17, 2020 07:00
Show Gist options
  • Save devdoomari3/9da3e87097591b6d3ccb31773710c5ac to your computer and use it in GitHub Desktop.
Save devdoomari3/9da3e87097591b6d3ccb31773710c5ac to your computer and use it in GitHub Desktop.
typescript type-level example: check keys match
import { Equals } from 'typelevel-ts';
export const __KEYS_MATCH_OK_STRING = 'Keys Match!'
export type isMatch<TType> = TType extends 'T' ?
typeof __KEYS_MATCH_OK_STRING : never;
export type RequireSameKeys<
A extends object,
B extends object
> = {
isMatch: isMatch<Equals<keyof A, keyof B>>;
A?: keyof A;
B?: keyof B;
}
export type Example1 = {
a: string;
b: number;
}
export type Example2 = {
a: boolean;
b: boolean;
}
export type Example3 = {
a: boolean;
}
export const KeyTest1: RequireSameKeys<Example1, Example2> = {
isMatch: 'Keys Match!',
}
export const KeyTest2: RequireSameKeys<Example2, Example3> = {
// @ts-expect-error
isMatch: 'Keys Match!',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment