Created
July 17, 2020 07:00
-
-
Save devdoomari3/9da3e87097591b6d3ccb31773710c5ac to your computer and use it in GitHub Desktop.
typescript type-level example: check keys match
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
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