-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalizedUnit.test.ts
41 lines (34 loc) · 1.58 KB
/
LocalizedUnit.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { expect } from '@std/expect';
import { describe, it } from '@std/testing/bdd';
import type { LocalizedUnit } from './LocalizedUnit.ts';
type SupportedLocales = 'de' | 'en' | 'ja';
// The expected usage is that each app or library defines its own supported locales, and a base type that is "an item localized into the languages of the supported locales". So this just simulates that for this test.
type Localization = LocalizedUnit<SupportedLocales>;
describe('I18nItem', () =>
{
it('should have as-you-type error in TypeScript when a localization is missing', () =>
{
const good: Localization = {
de: 'Roboter',
en: 'robot',
ja: 'ロボット',
};
// @ts-expect-error Type '{ en: string; }' is missing the following properties from type 'Readonly<Record<SupportedLocales, string>>': de, ja
const _bad: Localization = {
en:
'Whoops, this item is missing a localization for "de" and "ja" so it should be an error in the editor, and the build should fail.',
};
expect(good.en).toEqual('robot');
});
it('should have as-you-type error in TypeScript when an illegal locale is used', () =>
{
// @ts-expect-error Type '{ de: string; en: string; ja: string; _metadata: string; }' is not assignable to type 'never'
const _bad: LocalizedUnit<'de' | 'en' | 'ja' | '_metadata'> = {
de: 'Roboter',
en: 'robot',
ja: 'ロボット',
_metadata:
'This should be an error in the editor, and the build should fail. The `_metadata` key is allowed as a locale because it has special meaning internally.',
};
});
});