Skip to content

Commit

Permalink
Add TranslationKeys type (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed May 25, 2023
1 parent 76e7f1a commit 09b7179
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .changeset/olive-crews-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@vocab/core': minor
---

Add `TranslationKeys` type

The `TranslationKeys` type can be used to get a string literal union of all the translation keys used in a translation file.

**EXAMPLE USAGE**

```jsonc
// .vocab/en.translations.json
{
"Hello": {
"message": "Hello"
},
"Goodbye": {
"message": "Goodbye"
}
}
```

After running `vocab compile`:

```tsx
import type { TranslationKeys } from '@vocab/core';
import translations from './.vocab';

// 'Hello' | 'Goodbye'
type Keys = TranslationKeys<typeof translations>;
```
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export type TranslationFile<
load: (language: Language) => Promise<void>;
};

export type TranslationKeys<
Translations extends TranslationFile<any, ParsedFormatFnByKey>,
> = keyof Awaited<ReturnType<Translations['getMessages']>>;

export interface LanguageTarget {
// The name or tag of a language
name: LanguageName;
Expand Down

0 comments on commit 09b7179

Please sign in to comment.