@sveltekit-i18n/base 3.1.0-next.1 → 3.1.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -4
- package/dist/types.d.ts +45 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -312,18 +312,29 @@ preprocess: 'full' // 'full' | 'preserveArrays' | 'none' | custom function
|
|
|
312
312
|
|
|
313
313
|
### `schema`
|
|
314
314
|
|
|
315
|
-
A map of translation key to the payload its message expects (`never` for a message that takes none).
|
|
315
|
+
A map of translation key to the payload its message expects (`never` for a message that takes none). It types `t`/`l` — keys autocomplete, an unknown key is a type error, and the payload argument is checked. Register it once for the app, in a global script, and every instance whose config states no `schema` is typed by it:
|
|
316
316
|
|
|
317
317
|
```typescript
|
|
318
|
-
|
|
318
|
+
// src/i18n-schema.d.ts — no top-level import or export
|
|
319
|
+
interface TranslationSchema {
|
|
319
320
|
'common.greeting': { name: string };
|
|
320
321
|
'common.farewell': never;
|
|
321
|
-
}
|
|
322
|
+
}
|
|
322
323
|
|
|
324
|
+
declare namespace SvelteKitI18n {
|
|
325
|
+
interface Register {
|
|
326
|
+
schema: TranslationSchema;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Or state it per instance — only its type is read, so the value can stay empty at runtime, and a stated schema wins over the registry:
|
|
332
|
+
|
|
333
|
+
```typescript
|
|
323
334
|
const i18n = new I18n({ ...config, schema: {} as TranslationSchema });
|
|
324
335
|
```
|
|
325
336
|
|
|
326
|
-
Hand-write it for a small set of messages, or
|
|
337
|
+
Hand-write it for a small set of messages, or generate it — [@sveltekit-i18n/typegen](https://github.com/sveltekit-i18n/typegen), a separate package, generates one. A schema whose keys are not a closed set (`schema: {}`) types nothing and keeps the registry out, so keys stay plain strings. A library never registers. The registry needs base 3.1 or newer. See [`schema`](./docs/README.md#schema) for the full rules.
|
|
327
338
|
|
|
328
339
|
### `cache`
|
|
329
340
|
|
package/dist/types.d.ts
CHANGED
|
@@ -129,21 +129,21 @@ export declare namespace Config {
|
|
|
129
129
|
parser: Parser.T<P, O>;
|
|
130
130
|
/**
|
|
131
131
|
* A key schema — a map of translation key to the payload its message
|
|
132
|
-
* expects (`never` for a message without parameters).
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* typed by
|
|
132
|
+
* expects (`never` for a message without parameters). It types `t`/`l`:
|
|
133
|
+
* keys autocomplete and a wrong payload is a type error. Left out, the
|
|
134
|
+
* schema the app registers in `SvelteKitI18n.Register` types the
|
|
135
|
+
* instance; stated, it wins over the registry. Only its TYPE is read, so
|
|
136
|
+
* the value may be empty at runtime — as long as it is TYPED, e.g.
|
|
137
|
+
* `{} as TranslationSchema`. A schema whose keys are not a closed set (an
|
|
138
|
+
* open index signature, or no keys at all, as in `schema: {}`) types
|
|
139
|
+
* nothing and keeps the registry out: keys stay plain strings. Read at
|
|
140
|
+
* construction time only: a later `loadConfig()` cannot retype the
|
|
141
|
+
* instance, and an extension typed by a fixed return type erases the
|
|
142
|
+
* instance's type parameters, while one typed by an `Extension.Operator`
|
|
143
|
+
* (`Extension.Generic`) keeps them.
|
|
142
144
|
*
|
|
143
145
|
* @example
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* const i18n = new I18n({ ...config, schema });
|
|
146
|
+
* const i18n = new I18n({ ...config, schema: {} as TranslationSchema });
|
|
147
147
|
*/
|
|
148
148
|
schema?: S;
|
|
149
149
|
/**
|
|
@@ -510,6 +510,19 @@ export declare namespace Parser {
|
|
|
510
510
|
*/
|
|
511
511
|
type ExtractParamsFactory<O = unknown> = (options?: O) => ExtractParams;
|
|
512
512
|
}
|
|
513
|
+
declare global {
|
|
514
|
+
namespace SvelteKitI18n {
|
|
515
|
+
/**
|
|
516
|
+
* The app's type registry, filled by a generated global script
|
|
517
|
+
* (`interface Register { schema: TranslationSchema }`). Its `schema` types
|
|
518
|
+
* every instance whose config states none. Every copy of the core declares
|
|
519
|
+
* it empty, and a library never registers: the registry covers the whole
|
|
520
|
+
* program.
|
|
521
|
+
*/
|
|
522
|
+
interface Register {
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
513
526
|
export declare namespace Schema {
|
|
514
527
|
/**
|
|
515
528
|
* A schema types calls only when its keys form a specific, closed set. An
|
|
@@ -518,10 +531,25 @@ export declare namespace Schema {
|
|
|
518
531
|
* they degrade to no schema at all.
|
|
519
532
|
*/
|
|
520
533
|
type HasClosedKeys<S> = [keyof S & string] extends [never] ? false : string extends keyof S ? false : true;
|
|
521
|
-
/**
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
534
|
+
/** `S` when its keys are a closed set, `never` otherwise. A union schema is taken whole. */
|
|
535
|
+
type Closed<S> = [S] extends [object] ? (HasClosedKeys<S> extends true ? S : never) : never;
|
|
536
|
+
/** What a config's `schema` slot holds; `unknown` when it is absent or typed `any`. */
|
|
537
|
+
type Slot<C> = C extends {
|
|
538
|
+
schema?: infer S;
|
|
539
|
+
} ? S : unknown;
|
|
540
|
+
/** The key schema `SvelteKitI18n.Register` carries; `never` when it carries none to use. */
|
|
541
|
+
export type Registered = SvelteKitI18n.Register extends {
|
|
542
|
+
schema: infer S;
|
|
543
|
+
} ? Closed<S> : never;
|
|
544
|
+
/**
|
|
545
|
+
* The key schema a config types its instance with; `never` when there is
|
|
546
|
+
* none to use. A slot the config states decides — a closed schema is that
|
|
547
|
+
* schema, one without a closed key set (`{}`) opts out to plain string keys
|
|
548
|
+
* — while an absent slot, or one typed `any` (a plain `Config.T`
|
|
549
|
+
* annotation), reads the registry. A union of configs yields the union of
|
|
550
|
+
* their schemas.
|
|
551
|
+
*/
|
|
552
|
+
export type FromConfig<C> = C extends unknown ? (unknown extends Slot<C> ? Registered : Closed<Slot<C>>) : never;
|
|
525
553
|
/**
|
|
526
554
|
* The key schema an instance was built with; `never` when it carries none.
|
|
527
555
|
* The counterpart of `FromConfig` for code handed a constructed surface
|
package/package.json
CHANGED