Skip to content

Commit

Permalink
Fix i18n type issue with multiple data content collections (#2611)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
HiDeoo and delucis authored Nov 19, 2024
1 parent 9a31980 commit 6059d96
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-walls-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes a UI string type issue in projects with multiple data content collections.
6 changes: 5 additions & 1 deletion packages/starlight/utils/createTranslationSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function buildResources<T extends Record<string, string | undefined>>(
return { [I18nextNamespace]: dictionary as BuiltInStrings & T };
}

export type I18nKeys = UserI18nKeys | keyof StarlightApp.I18n;
// `keyof BuiltInStrings` and `UserI18nKeys` may contain some identical keys, e.g. the built-in UI
// strings. We let TypeScript merge them into a single union type so that plugins with a TypeScript
// configuration preventing `UserI18nKeys` to be properly inferred can still get auto-completion
// for built-in UI strings.
export type I18nKeys = keyof BuiltInStrings | UserI18nKeys | keyof StarlightApp.I18n;

export type I18nT = TFunction<'starlight', undefined> & {
all: () => UserI18nSchema;
Expand Down
7 changes: 6 additions & 1 deletion packages/starlight/utils/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import type { i18nSchemaOutput } from '../schemas/i18n';
import { createTranslationSystem } from './createTranslationSystem';
import type { RemoveIndexSignature } from './types';

// @ts-ignore - This may be a type error in projects without an i18n collection and running
// `tsc --noEmit` in their project. Note that it is not possible to inline this type in
// `UserI18nSchema` because this would break types for users having multiple data collections.
type i18nCollection = CollectionEntry<'i18n'>;

export type UserI18nSchema = 'i18n' extends DataCollectionKey
? CollectionEntry<'i18n'> extends { data: infer T }
? i18nCollection extends { data: infer T }
? i18nSchemaOutput & T
: i18nSchemaOutput
: i18nSchemaOutput;
Expand Down

0 comments on commit 6059d96

Please sign in to comment.