From bbd738f572def6e4602794cf23820a0aad80983f Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Mon, 12 Sep 2022 15:34:30 +0300 Subject: [PATCH] feat(i18n): added OtherLanguageWrapExhaustive for exhaustive other languages --- packages/i18n/src/types.ts | 14 ++++++++++++++ packages/i18n/tests/types.ts | 7 +++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/i18n/src/types.ts b/packages/i18n/src/types.ts index 464a1301..c460fb2f 100644 --- a/packages/i18n/src/types.ts +++ b/packages/i18n/src/types.ts @@ -68,3 +68,17 @@ export type OtherLanguageWrap = { ? OtherLanguageWrap : never } +/** + * Wrapper type for i18n object containing strings for a language + * other than the primary one. Used to provide type safety. + * + * Unlike {@link OtherLanguageWrap}, this type requires all strings + * from the primary language to be present + */ +export type OtherLanguageWrapExhaustive = { + [key in keyof Strings]: Strings[key] extends I18nValue + ? I18nValue + : Strings[key] extends Record + ? OtherLanguageWrapExhaustive + : never +} diff --git a/packages/i18n/tests/types.ts b/packages/i18n/tests/types.ts index 56d16b3e..0893d116 100644 --- a/packages/i18n/tests/types.ts +++ b/packages/i18n/tests/types.ts @@ -3,8 +3,11 @@ // This file is never executed, only compiled import { Message } from '@mtcute/client' -import { createMtcuteI18n } from '../src' -import { OtherLanguageWrap } from '../src/types' +import { + createMtcuteI18n, + OtherLanguageWrapExhaustive, + OtherLanguageWrap, +} from '../src' const en = { basic: {