@teammy/server 0.2.23 → 0.2.24
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/LICENSE +21 -0
- package/README.md +11 -7
- package/dist/main.js +20331 -13329
- package/dist/native-pi-entry.js +755 -19
- package/dist/project-pi-entry.js +758 -22
- package/dist/runtime/en/shared/system.md +9 -0
- package/dist/runtime/en/shared/tools.json +22 -0
- package/dist/runtime/en/workflows/directory-analysis/prompt.md +11 -0
- package/dist/runtime/en/workflows/file-summary/prompt.md +7 -0
- package/dist/runtime/en/workflows/session-handoff/prompt.md +31 -0
- package/dist/runtime/en/workflows/session-overview/prompt.md +26 -0
- package/dist/runtime/en/workflows/task-bootstrap/prompt.md +84 -0
- package/dist/runtime/shared/system.md +1 -1
- package/dist/runtime/workflows/session-handoff/prompt.md +1 -1
- package/dist/runtime/workflows/session-overview/prompt.md +1 -1
- package/dist/web/THIRD-PARTY-LICENSES.md +2025 -0
- package/dist/web/assets/Assistant-BeWIo88A.js +1 -0
- package/dist/web/assets/{Computers-BtcIsnDD.css → Computers-BJKv9oJv.css} +1 -1
- package/dist/web/assets/Computers-CEBC9xd4.js +14 -0
- package/dist/web/assets/Conversation-BMA0djTl.js +1 -0
- package/dist/web/assets/Dashboard-DtNpBYI5.js +1 -0
- package/dist/web/assets/Files-C-00txBh.js +1 -0
- package/dist/web/assets/Help-C5yLJzSE.js +2392 -0
- package/dist/web/assets/MessageComposer-D7ml-LaD.js +1 -0
- package/dist/web/assets/MessageComposer-DqcbXBIG.css +1 -0
- package/dist/web/assets/ResourceLibrary-Ceon9f0i.js +6 -0
- package/dist/web/assets/TerminalPane-CZYb0vIG.js +22 -0
- package/dist/web/assets/WorkspaceTree-C2sVptdG.js +1 -0
- package/dist/web/assets/file-links-CQ6ShuxD.js +1 -0
- package/dist/web/assets/index-B9gF6reY.css +1 -0
- package/dist/web/assets/index-BCj5CDtq.js +40 -0
- package/dist/web/assets/project-links-BwYckk7L.js +105 -0
- package/dist/web/assets/workflow-DR-M8YZx.svg +111 -0
- package/dist/web/downloads/teammy-inbox-skill.zip +0 -0
- package/dist/web/downloads/teammy-skill.zip +0 -0
- package/dist/web/index.html +4 -4
- package/dist/web/notification-worker.js +2 -10
- package/node_modules/i18next/LICENSE +21 -0
- package/node_modules/i18next/README.md +77 -0
- package/node_modules/i18next/dist/cjs/i18next.js +2495 -0
- package/node_modules/i18next/dist/esm/i18next.js +2505 -0
- package/node_modules/i18next/dist/esm/package.json +1 -0
- package/node_modules/i18next/dist/umd/i18next.js +2501 -0
- package/node_modules/i18next/dist/umd/i18next.min.js +1 -0
- package/node_modules/i18next/i18next.js +2501 -0
- package/node_modules/i18next/i18next.min.js +1 -0
- package/node_modules/i18next/index.d.mts +71 -0
- package/node_modules/i18next/index.d.ts +739 -0
- package/node_modules/i18next/package.json +125 -0
- package/node_modules/i18next/typescript/helpers.d.ts +73 -0
- package/node_modules/i18next/typescript/options.d.ts +871 -0
- package/node_modules/i18next/typescript/t.d.ts +1449 -0
- package/node_modules/jose/LICENSE.md +21 -0
- package/node_modules/jose/README.md +150 -0
- package/node_modules/jose/dist/types/index.d.ts +55 -0
- package/node_modules/jose/dist/types/jwe/compact/decrypt.d.ts +25 -0
- package/node_modules/jose/dist/types/jwe/compact/encrypt.d.ts +40 -0
- package/node_modules/jose/dist/types/jwe/flattened/decrypt.d.ts +25 -0
- package/node_modules/jose/dist/types/jwe/flattened/encrypt.d.ts +46 -0
- package/node_modules/jose/dist/types/jwe/general/decrypt.d.ts +29 -0
- package/node_modules/jose/dist/types/jwe/general/encrypt.d.ts +51 -0
- package/node_modules/jose/dist/types/jwk/embedded.d.ts +10 -0
- package/node_modules/jose/dist/types/jwk/thumbprint.d.ts +15 -0
- package/node_modules/jose/dist/types/jwks/local.d.ts +17 -0
- package/node_modules/jose/dist/types/jwks/remote.d.ts +94 -0
- package/node_modules/jose/dist/types/jws/compact/sign.d.ts +21 -0
- package/node_modules/jose/dist/types/jws/compact/verify.d.ts +25 -0
- package/node_modules/jose/dist/types/jws/flattened/sign.d.ts +23 -0
- package/node_modules/jose/dist/types/jws/flattened/verify.d.ts +26 -0
- package/node_modules/jose/dist/types/jws/general/sign.d.ts +41 -0
- package/node_modules/jose/dist/types/jws/general/verify.d.ts +31 -0
- package/node_modules/jose/dist/types/jwt/decrypt.d.ts +38 -0
- package/node_modules/jose/dist/types/jwt/encrypt.d.ts +57 -0
- package/node_modules/jose/dist/types/jwt/sign.d.ts +21 -0
- package/node_modules/jose/dist/types/jwt/unsecured.d.ts +21 -0
- package/node_modules/jose/dist/types/jwt/verify.d.ts +36 -0
- package/node_modules/jose/dist/types/key/export.d.ts +19 -0
- package/node_modules/jose/dist/types/key/generate_key_pair.d.ts +33 -0
- package/node_modules/jose/dist/types/key/generate_secret.d.ts +30 -0
- package/node_modules/jose/dist/types/key/import.d.ts +67 -0
- package/node_modules/jose/dist/types/types.d.ts +802 -0
- package/node_modules/jose/dist/types/util/base64url.d.ts +16 -0
- package/node_modules/jose/dist/types/util/decode_jwt.d.ts +9 -0
- package/node_modules/jose/dist/types/util/decode_protected_header.d.ts +10 -0
- package/node_modules/jose/dist/types/util/errors.d.ts +182 -0
- package/node_modules/jose/dist/webapi/index.js +70 -0
- package/node_modules/jose/dist/webapi/jwe/compact/decrypt.js +7 -0
- package/node_modules/jose/dist/webapi/jwe/compact/encrypt.js +26 -0
- package/node_modules/jose/dist/webapi/jwe/flattened/decrypt.js +15 -0
- package/node_modules/jose/dist/webapi/jwe/flattened/encrypt.js +35 -0
- package/node_modules/jose/dist/webapi/jwe/general/decrypt.js +41 -0
- package/node_modules/jose/dist/webapi/jwe/general/encrypt.js +105 -0
- package/node_modules/jose/dist/webapi/jwk/embedded.js +30 -0
- package/node_modules/jose/dist/webapi/jwk/thumbprint.js +55 -0
- package/node_modules/jose/dist/webapi/jwks/local.js +60 -0
- package/node_modules/jose/dist/webapi/jwks/remote.js +103 -0
- package/node_modules/jose/dist/webapi/jws/compact/sign.js +20 -0
- package/node_modules/jose/dist/webapi/jws/compact/verify.js +8 -0
- package/node_modules/jose/dist/webapi/jws/flattened/sign.js +23 -0
- package/node_modules/jose/dist/webapi/jws/flattened/verify.js +12 -0
- package/node_modules/jose/dist/webapi/jws/general/sign.js +58 -0
- package/node_modules/jose/dist/webapi/jws/general/verify.js +42 -0
- package/node_modules/jose/dist/webapi/jwt/decrypt.js +13 -0
- package/node_modules/jose/dist/webapi/jwt/encrypt.js +42 -0
- package/node_modules/jose/dist/webapi/jwt/sign.js +19 -0
- package/node_modules/jose/dist/webapi/jwt/unsecured.js +34 -0
- package/node_modules/jose/dist/webapi/jwt/verify.js +13 -0
- package/node_modules/jose/dist/webapi/key/export.js +35 -0
- package/node_modules/jose/dist/webapi/key/generate_key_pair.js +35 -0
- package/node_modules/jose/dist/webapi/key/generate_secret.js +36 -0
- package/node_modules/jose/dist/webapi/key/import.js +53 -0
- package/node_modules/jose/dist/webapi/lib/asn1.js +138 -0
- package/node_modules/jose/dist/webapi/lib/buffer_utils.js +75 -0
- package/node_modules/jose/dist/webapi/lib/content_encryption.js +90 -0
- package/node_modules/jose/dist/webapi/lib/deflate.js +41 -0
- package/node_modules/jose/dist/webapi/lib/jwe_algorithms.js +116 -0
- package/node_modules/jose/dist/webapi/lib/jwe_decrypt.js +190 -0
- package/node_modules/jose/dist/webapi/lib/jwe_encrypt.js +75 -0
- package/node_modules/jose/dist/webapi/lib/jws_algorithms.js +74 -0
- package/node_modules/jose/dist/webapi/lib/jws_sign.js +46 -0
- package/node_modules/jose/dist/webapi/lib/jws_verify.js +89 -0
- package/node_modules/jose/dist/webapi/lib/jwt_claims_set.js +150 -0
- package/node_modules/jose/dist/webapi/lib/key.js +157 -0
- package/node_modules/jose/dist/webapi/lib/key_algorithm.js +15 -0
- package/node_modules/jose/dist/webapi/lib/key_descriptor.js +9 -0
- package/node_modules/jose/dist/webapi/lib/key_management.js +195 -0
- package/node_modules/jose/dist/webapi/lib/validate.js +122 -0
- package/node_modules/jose/dist/webapi/util/base64url.js +16 -0
- package/node_modules/jose/dist/webapi/util/decode_jwt.js +33 -0
- package/node_modules/jose/dist/webapi/util/decode_protected_header.js +19 -0
- package/node_modules/jose/dist/webapi/util/errors.js +109 -0
- package/node_modules/jose/package.json +134 -0
- package/node_modules/oauth4webapi/LICENSE.md +21 -0
- package/node_modules/oauth4webapi/README.md +104 -0
- package/node_modules/oauth4webapi/build/index.d.ts +3046 -0
- package/node_modules/oauth4webapi/build/index.js +2530 -0
- package/node_modules/oauth4webapi/build/index.js.map +1 -0
- package/node_modules/oauth4webapi/package.json +112 -0
- package/node_modules/openid-client/LICENSE.md +21 -0
- package/node_modules/openid-client/README.md +258 -0
- package/node_modules/openid-client/build/index.d.ts +2478 -0
- package/node_modules/openid-client/build/index.js +1347 -0
- package/node_modules/openid-client/build/index.js.map +1 -0
- package/node_modules/openid-client/build/passport.d.ts +258 -0
- package/node_modules/openid-client/build/passport.js +264 -0
- package/node_modules/openid-client/build/passport.js.map +1 -0
- package/node_modules/openid-client/package.json +130 -0
- package/package.json +5 -1
- package/dist/web/assets/Assistant-Dks48Y5w.js +0 -1
- package/dist/web/assets/Computers-CaCzZeCc.js +0 -13
- package/dist/web/assets/Conversation-kAMzKADw.js +0 -1
- package/dist/web/assets/Dashboard-ComNJNOg.js +0 -1
- package/dist/web/assets/Dropdown-DGFI68Vd.js +0 -55
- package/dist/web/assets/Files-Blsq_C-B.js +0 -1
- package/dist/web/assets/Help-BM23uS-t.js +0 -1079
- package/dist/web/assets/Markdown-CXnZDm5J.js +0 -29
- package/dist/web/assets/MessageComposer-BoOgfyPA.js +0 -1
- package/dist/web/assets/MessageComposer-hKEluJsT.css +0 -1
- package/dist/web/assets/ResourceLibrary-C12_47kE.js +0 -6
- package/dist/web/assets/TerminalPane-BTRM-36z.js +0 -22
- package/dist/web/assets/WorkspaceTree-Co7XG2RG.js +0 -1
- package/dist/web/assets/file-links-CoVqmrXg.js +0 -1
- package/dist/web/assets/index-CeKzFsxV.css +0 -1
- package/dist/web/assets/index-mhJ33-bH.js +0 -10
- package/dist/web/assets/task-links-BE36-8mc.js +0 -1
- /package/dist/web/assets/{Dropdown-ktgqMqAD.css → project-links-ktgqMqAD.css} +0 -0
|
@@ -0,0 +1,1449 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
$OmitArrayKeys,
|
|
3
|
+
$PreservedValue,
|
|
4
|
+
$Dictionary,
|
|
5
|
+
$SpecialObject,
|
|
6
|
+
$StringKeyPathToRecord,
|
|
7
|
+
$NoInfer,
|
|
8
|
+
$Turtles,
|
|
9
|
+
} from './helpers.js';
|
|
10
|
+
import type {
|
|
11
|
+
TypeOptions,
|
|
12
|
+
Namespace,
|
|
13
|
+
FlatNamespace,
|
|
14
|
+
DefaultNamespace,
|
|
15
|
+
TOptions,
|
|
16
|
+
TOptionsBase,
|
|
17
|
+
} from './options.js';
|
|
18
|
+
|
|
19
|
+
// Only the declarations marked `export` are public: without an export list, every declaration of a
|
|
20
|
+
// .d.ts module counts as exported, and the `_` and `$` helpers below were importable.
|
|
21
|
+
export {};
|
|
22
|
+
|
|
23
|
+
/** @todo consider to replace {} with Record<string, never> */
|
|
24
|
+
|
|
25
|
+
// Type Options
|
|
26
|
+
type _ReturnObjects = TypeOptions['returnObjects'];
|
|
27
|
+
type _ReturnEmptyString = TypeOptions['returnEmptyString'];
|
|
28
|
+
type _ReturnNull = TypeOptions['returnNull'];
|
|
29
|
+
// `keySeparator: false`: flat keys only (a `false` here made `nestedfalsec` keys and split keys at the
|
|
30
|
+
// text "false")
|
|
31
|
+
type _KeySeparator = Exclude<TypeOptions['keySeparator'], false>;
|
|
32
|
+
// what getFixedT() joins a key prefix and a key with: '.' when keySeparator is false
|
|
33
|
+
type _KeyPrefixSeparator = TypeOptions['keySeparator'] extends false
|
|
34
|
+
? '.'
|
|
35
|
+
: TypeOptions['keySeparator'];
|
|
36
|
+
// `nsSeparator: false`: no `ns:key` keys (a `false` here made `nsfalsekey` keys and split keys containing "false")
|
|
37
|
+
type _NsSeparator = Exclude<TypeOptions['nsSeparator'], false>;
|
|
38
|
+
type _PluralSeparator = TypeOptions['pluralSeparator'];
|
|
39
|
+
type _ContextSeparator = TypeOptions['contextSeparator'];
|
|
40
|
+
type _FallbackNamespace = TypeOptions['fallbackNS'];
|
|
41
|
+
type _Resources = TypeOptions['resources'];
|
|
42
|
+
type _InterpolationPrefix = TypeOptions['interpolationPrefix'];
|
|
43
|
+
type _InterpolationSuffix = TypeOptions['interpolationSuffix'];
|
|
44
|
+
type _InterpolationFormatSeparator = TypeOptions['interpolationFormatSeparator'];
|
|
45
|
+
type _DefaultVariables = TypeOptions['defaultVariables'];
|
|
46
|
+
type _UnescapePrefix = TypeOptions['unescapePrefix'];
|
|
47
|
+
type _UnescapeSuffix = TypeOptions['unescapeSuffix'];
|
|
48
|
+
type _StrictKeyChecks = TypeOptions['strictKeyChecks'];
|
|
49
|
+
type _StrictInterpolationChecks = TypeOptions['strictInterpolationChecks'];
|
|
50
|
+
type _EnableSelector = TypeOptions['enableSelector'];
|
|
51
|
+
type _ParseInterpolation = TypeOptions['parseInterpolation'];
|
|
52
|
+
type _InterpolationFormatTypeMap = TypeOptions['interpolationFormatTypeMap'];
|
|
53
|
+
|
|
54
|
+
type $IsResourcesDefined = [keyof _Resources] extends [never] ? false : true;
|
|
55
|
+
type $ValueIfResourcesDefined<Value, Fallback> = $IsResourcesDefined extends true
|
|
56
|
+
? Value
|
|
57
|
+
: Fallback;
|
|
58
|
+
type $FirstNamespace<Ns extends Namespace> = Ns extends readonly any[] ? Ns[0] : Ns;
|
|
59
|
+
|
|
60
|
+
export type Resources = $ValueIfResourcesDefined<_Resources, $Dictionary<string>>;
|
|
61
|
+
|
|
62
|
+
export type PluralSuffix = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
63
|
+
|
|
64
|
+
export type WithOrWithoutPlural<Key> =
|
|
65
|
+
Key extends `${infer KeyWithoutOrdinalPlural}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
66
|
+
? KeyWithoutOrdinalPlural | Key
|
|
67
|
+
: Key extends `${infer KeyWithoutPlural}${_PluralSeparator}${PluralSuffix}`
|
|
68
|
+
? KeyWithoutPlural | Key
|
|
69
|
+
: Key;
|
|
70
|
+
|
|
71
|
+
export type JoinKeys<K1, K2> = `${K1 & string}${_KeySeparator}${K2 & string}`;
|
|
72
|
+
export type AppendNamespace<Ns, Keys> = `${Ns & string}${_NsSeparator}${Keys & string}`;
|
|
73
|
+
|
|
74
|
+
export type TrimSpaces<
|
|
75
|
+
T extends string,
|
|
76
|
+
Acc extends string = '',
|
|
77
|
+
> = T extends `${infer Char}${infer Rest}`
|
|
78
|
+
? Char extends ' '
|
|
79
|
+
? TrimSpaces<Rest, Acc>
|
|
80
|
+
: TrimSpaces<Rest, `${Acc}${Char}`>
|
|
81
|
+
: T extends ''
|
|
82
|
+
? Acc
|
|
83
|
+
: never;
|
|
84
|
+
|
|
85
|
+
export interface Branded<Ns extends Namespace> {
|
|
86
|
+
$TFunctionBrand: $IsResourcesDefined extends true
|
|
87
|
+
? `${Ns extends readonly any[] ? Ns[0] : Ns}`
|
|
88
|
+
: never;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* `keyof Res`, built once per resource object. TypeScript does not cache
|
|
93
|
+
* `keyof` of an object type, so `keyof Res` inside a conditional that runs per
|
|
94
|
+
* key (or per `t()` call) rebuilt the union of all keys every time: quadratic
|
|
95
|
+
* in the namespace size. Conditional type instantiations are cached.
|
|
96
|
+
*/
|
|
97
|
+
type _KeysOf<Res> = [Res] extends [unknown] ? keyof Res : never;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* `Res[Key & keyof Res]`: a literal key is looked up directly instead of being
|
|
101
|
+
* intersected with every key; a wide key (`string`, `${string}_suffix` from a
|
|
102
|
+
* string context) still matches all keys it covers.
|
|
103
|
+
*/
|
|
104
|
+
type _ValueOf<Res, Key> =
|
|
105
|
+
Key extends _KeysOf<Res>
|
|
106
|
+
? Res[Key]
|
|
107
|
+
: _IsLiteral<Key & PropertyKey> extends true
|
|
108
|
+
? never
|
|
109
|
+
: Res[Key & _KeysOf<Res>];
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A resource value to walk into: an object with an index signature or an array (as before), or
|
|
113
|
+
* else a named interface, which has no index signature. Functions, `Date` and `object` stay leaves.
|
|
114
|
+
*/
|
|
115
|
+
type _IsBranch<Value> = [Value] extends [$Dictionary | readonly unknown[]]
|
|
116
|
+
? true
|
|
117
|
+
: [Value] extends [string | ((...args: any[]) => any) | Date]
|
|
118
|
+
? false
|
|
119
|
+
: [Value] extends [object]
|
|
120
|
+
? object extends Value
|
|
121
|
+
? false
|
|
122
|
+
: true
|
|
123
|
+
: false;
|
|
124
|
+
|
|
125
|
+
/** `true` without strictNullChecks, where `undefined` passes `extends string`. */
|
|
126
|
+
type _NoStrictNullChecks = undefined extends string ? true : false;
|
|
127
|
+
|
|
128
|
+
/** ****************************************************
|
|
129
|
+
* Build all keys and key prefixes based on Resources *
|
|
130
|
+
***************************************************** */
|
|
131
|
+
// Do NOT add a `[Res] extends [never]` guard here: it defers these into
|
|
132
|
+
// conditional types, so `KeyPrefix<Ns>` no longer resolves to a literal union
|
|
133
|
+
// and `keyPrefix` inference widens to the whole namespace, polluting `t()`
|
|
134
|
+
// return types with sibling keys (#2434 regression). Guard `never` in the merge.
|
|
135
|
+
export type KeysBuilderWithReturnObjects<Res, Key = keyof Res> =
|
|
136
|
+
Key extends _KeysOf<Res>
|
|
137
|
+
? _IsBranch<Res[Key]> extends true
|
|
138
|
+
? | JoinKeys<Key, WithOrWithoutPlural<keyof $OmitArrayKeys<Res[Key]>>>
|
|
139
|
+
| JoinKeys<Key, KeysBuilderWithReturnObjects<Res[Key]>>
|
|
140
|
+
: never
|
|
141
|
+
: never;
|
|
142
|
+
|
|
143
|
+
export type KeysBuilderWithoutReturnObjects<Res, Key = keyof $OmitArrayKeys<Res>> =
|
|
144
|
+
Key extends _KeysOf<Res>
|
|
145
|
+
? _IsBranch<Res[Key]> extends true
|
|
146
|
+
? JoinKeys<Key, KeysBuilderWithoutReturnObjects<Res[Key]>>
|
|
147
|
+
: Key
|
|
148
|
+
: never;
|
|
149
|
+
|
|
150
|
+
export type KeysBuilder<Res, WithReturnObjects> = $IsResourcesDefined extends true
|
|
151
|
+
? WithReturnObjects extends true
|
|
152
|
+
? keyof Res | KeysBuilderWithReturnObjects<Res>
|
|
153
|
+
: KeysBuilderWithoutReturnObjects<Res>
|
|
154
|
+
: string;
|
|
155
|
+
|
|
156
|
+
export type KeysWithReturnObjects = {
|
|
157
|
+
[Ns in FlatNamespace]: WithOrWithoutPlural<KeysBuilder<Resources[Ns], true>>;
|
|
158
|
+
};
|
|
159
|
+
export type KeysWithoutReturnObjects = {
|
|
160
|
+
[Ns in FlatNamespace]: WithOrWithoutPlural<KeysBuilder<Resources[Ns], false>>;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export type ResourceKeys<WithReturnObjects = _ReturnObjects> = WithReturnObjects extends true
|
|
164
|
+
? KeysWithReturnObjects
|
|
165
|
+
: KeysWithoutReturnObjects;
|
|
166
|
+
|
|
167
|
+
/** **********************************************************************
|
|
168
|
+
* Parse t function keys based on the namespace, options and key prefix *
|
|
169
|
+
*********************************************************************** */
|
|
170
|
+
export type KeysByTOptions<TOpt extends TOptions> = TOpt['returnObjects'] extends true
|
|
171
|
+
? ResourceKeys<true>
|
|
172
|
+
: ResourceKeys;
|
|
173
|
+
|
|
174
|
+
export type NsByTOptions<Ns extends Namespace, TOpt extends TOptions> = TOpt['ns'] extends Namespace
|
|
175
|
+
? _NoStrictNullChecks extends true
|
|
176
|
+
? // without strictNullChecks an absent `ns` reads as the whole `Namespace` type
|
|
177
|
+
Namespace extends TOpt['ns']
|
|
178
|
+
? Ns
|
|
179
|
+
: TOpt['ns']
|
|
180
|
+
: TOpt['ns']
|
|
181
|
+
: Ns;
|
|
182
|
+
|
|
183
|
+
type _ParseKeysByKeyPrefix<Keys, KPrefix> = KPrefix extends string
|
|
184
|
+
? Keys extends `${KPrefix}${_KeyPrefixSeparator}${infer Key}`
|
|
185
|
+
? Key
|
|
186
|
+
: never
|
|
187
|
+
: Keys;
|
|
188
|
+
|
|
189
|
+
// Without strictNullChecks `undefined extends string` is true, and every key would need an
|
|
190
|
+
// `undefined.` prefix, so a missing prefix is checked first there; strict mode is unchanged.
|
|
191
|
+
export type ParseKeysByKeyPrefix<Keys, KPrefix> = _NoStrictNullChecks extends true
|
|
192
|
+
? [KPrefix] extends [undefined]
|
|
193
|
+
? Keys
|
|
194
|
+
: _ParseKeysByKeyPrefix<Keys, KPrefix>
|
|
195
|
+
: _ParseKeysByKeyPrefix<Keys, KPrefix>;
|
|
196
|
+
|
|
197
|
+
export type ParseKeysByNamespaces<
|
|
198
|
+
Ns extends Namespace,
|
|
199
|
+
Keys,
|
|
200
|
+
> = Ns extends readonly (infer UnionNsps)[]
|
|
201
|
+
? UnionNsps extends keyof Keys
|
|
202
|
+
? AppendNamespace<UnionNsps, Keys[UnionNsps]>
|
|
203
|
+
: never
|
|
204
|
+
: never;
|
|
205
|
+
|
|
206
|
+
export type ParseKeysByFallbackNs<Keys extends $Dictionary> = _FallbackNamespace extends false
|
|
207
|
+
? never
|
|
208
|
+
: _FallbackNamespace extends (infer UnionFallbackNs extends string)[]
|
|
209
|
+
? Keys[UnionFallbackNs]
|
|
210
|
+
: Keys[_FallbackNamespace & string];
|
|
211
|
+
|
|
212
|
+
type _FilterKeysByContext<Keys, Context> = Context extends string
|
|
213
|
+
? Keys extends
|
|
214
|
+
| `${infer Prefix}${_ContextSeparator}${Context}${_PluralSeparator}${PluralSuffix}`
|
|
215
|
+
| `${infer Prefix}${_ContextSeparator}${Context}`
|
|
216
|
+
? Prefix
|
|
217
|
+
: never
|
|
218
|
+
: Keys;
|
|
219
|
+
|
|
220
|
+
export type FilterKeysByContext<Keys, Context> = _NoStrictNullChecks extends true // see ParseKeysByKeyPrefix
|
|
221
|
+
? [Context] extends [undefined]
|
|
222
|
+
? Keys
|
|
223
|
+
: _FilterKeysByContext<Keys, Context>
|
|
224
|
+
: _FilterKeysByContext<Keys, Context>;
|
|
225
|
+
|
|
226
|
+
export type ParseKeys<
|
|
227
|
+
Ns extends Namespace = DefaultNamespace,
|
|
228
|
+
TOpt extends TOptions = {},
|
|
229
|
+
KPrefix = undefined,
|
|
230
|
+
Keys extends $Dictionary = KeysByTOptions<TOpt>,
|
|
231
|
+
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
|
|
232
|
+
Context extends TOpt['context'] = TOpt['context'],
|
|
233
|
+
> = $IsResourcesDefined extends true
|
|
234
|
+
? FilterKeysByContext<
|
|
235
|
+
| ParseKeysByKeyPrefix<Keys[$FirstNamespace<ActualNS>], KPrefix>
|
|
236
|
+
| ParseKeysByNamespaces<ActualNS, Keys>
|
|
237
|
+
| ParseKeysByFallbackNs<Keys>,
|
|
238
|
+
Context
|
|
239
|
+
>
|
|
240
|
+
: string;
|
|
241
|
+
|
|
242
|
+
/** *******************************************************
|
|
243
|
+
* Parse t function return type and interpolation values *
|
|
244
|
+
******************************************************** */
|
|
245
|
+
export type ParseActualValue<Ret> =
|
|
246
|
+
Ret extends `${_UnescapePrefix}${infer ActualValue}${_UnescapeSuffix}`
|
|
247
|
+
? TrimSpaces<ActualValue>
|
|
248
|
+
: Ret;
|
|
249
|
+
|
|
250
|
+
/** Parses interpolation entries as `[variableName, formatSpecifier | never]` tuples. */
|
|
251
|
+
export type ParseInterpolationEntries<Ret> = [_ParseInterpolation] extends [false | 'icu' | 'mf2']
|
|
252
|
+
? never
|
|
253
|
+
: Ret extends `${string}${_InterpolationPrefix}${infer Value}${_InterpolationSuffix}${infer Rest}`
|
|
254
|
+
? | (Value extends `${infer ActualValue}${_InterpolationFormatSeparator}${infer Format}`
|
|
255
|
+
? [ParseActualValue<ActualValue>, TrimSpaces<Format>]
|
|
256
|
+
: [ParseActualValue<Value>, never])
|
|
257
|
+
| ParseInterpolationEntries<Rest>
|
|
258
|
+
: never;
|
|
259
|
+
|
|
260
|
+
/** Extracts just the variable names (kept for backward compat with ParseInterpolationValues usage). */
|
|
261
|
+
export type ParseInterpolationValues<Ret> = ParseInterpolationEntries<Ret>[0];
|
|
262
|
+
|
|
263
|
+
/** Built-in i18next formatter name → value type mapping. */
|
|
264
|
+
type _BuiltInFormatTypeMap = {
|
|
265
|
+
number: number;
|
|
266
|
+
currency: number;
|
|
267
|
+
datetime: Date;
|
|
268
|
+
relativetime: number;
|
|
269
|
+
list: readonly string[];
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
/** Strips inline formatting options, e.g. `currency(EUR)` → `currency`. */
|
|
273
|
+
type _StripFormatOptions<F> = F extends `${infer Name}(${string})` ? Name : F;
|
|
274
|
+
|
|
275
|
+
/** Resolves the type for a single interpolation entry based on name and format. */
|
|
276
|
+
type _ResolveEntryType<Name extends string, Format> = [Format] extends [never]
|
|
277
|
+
? Name extends 'count'
|
|
278
|
+
? number
|
|
279
|
+
: string | number
|
|
280
|
+
: Format extends keyof _InterpolationFormatTypeMap
|
|
281
|
+
? _InterpolationFormatTypeMap[Format]
|
|
282
|
+
: _StripFormatOptions<Format> extends keyof _InterpolationFormatTypeMap
|
|
283
|
+
? _InterpolationFormatTypeMap[_StripFormatOptions<Format> & keyof _InterpolationFormatTypeMap]
|
|
284
|
+
: Format extends keyof _BuiltInFormatTypeMap
|
|
285
|
+
? _BuiltInFormatTypeMap[Format]
|
|
286
|
+
: _StripFormatOptions<Format> extends keyof _BuiltInFormatTypeMap
|
|
287
|
+
? _BuiltInFormatTypeMap[_StripFormatOptions<Format> & keyof _BuiltInFormatTypeMap]
|
|
288
|
+
: string;
|
|
289
|
+
|
|
290
|
+
/** Local union-to-intersection (not exported from helpers). */
|
|
291
|
+
type _UnionToIntersection<T> = (T extends unknown ? (k: T) => void : never) extends (
|
|
292
|
+
k: infer I,
|
|
293
|
+
) => void
|
|
294
|
+
? I
|
|
295
|
+
: never;
|
|
296
|
+
|
|
297
|
+
/** Builds a per-entry typed record from parsed interpolation entries and intersects them. */
|
|
298
|
+
type _InterpolationMapFromEntries<E> = _UnionToIntersection<
|
|
299
|
+
E extends [infer Name extends string, infer Format]
|
|
300
|
+
? $StringKeyPathToRecord<Name, _ResolveEntryType<Name, Format>>
|
|
301
|
+
: never
|
|
302
|
+
>;
|
|
303
|
+
|
|
304
|
+
/** *********************************************************************
|
|
305
|
+
* ICU MessageFormat arguments (`parseInterpolation: 'icu'`) *
|
|
306
|
+
*********************************************************************** */
|
|
307
|
+
type _IcuWhitespace = ' ' | '\n' | '\t' | '\r';
|
|
308
|
+
type _IcuTrim<S> = S extends `${_IcuWhitespace}${infer R}`
|
|
309
|
+
? _IcuTrim<R>
|
|
310
|
+
: S extends `${infer R}${_IcuWhitespace}`
|
|
311
|
+
? _IcuTrim<R>
|
|
312
|
+
: S;
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Drops ICU apostrophe quoting as intl-messageformat (i18next-icu) reads it:
|
|
316
|
+
* `''` is a literal apostrophe, `'{...}'` (also `'}'`, `'<'`, `'>'`, `'#'`) is
|
|
317
|
+
* literal text up to the next single apostrophe.
|
|
318
|
+
* Simplification: `'#` quotes everywhere here, at runtime only inside plurals.
|
|
319
|
+
*/
|
|
320
|
+
type _IcuStripQuotes<S, Acc extends string = ''> = S extends `${infer L}'${infer R}`
|
|
321
|
+
? R extends `'${infer R2}`
|
|
322
|
+
? _IcuStripQuotes<R2, `${Acc}${L}`>
|
|
323
|
+
: R extends `${'{' | '}' | '<' | '>' | '#'}${string}`
|
|
324
|
+
? _IcuStripQuotes<_IcuSkipQuoted<R>, `${Acc}${L}`>
|
|
325
|
+
: _IcuStripQuotes<R, `${Acc}${L}'`>
|
|
326
|
+
: `${Acc}${S & string}`;
|
|
327
|
+
type _IcuSkipQuoted<S> = S extends `${string}'${infer R}`
|
|
328
|
+
? R extends `'${infer R2}`
|
|
329
|
+
? _IcuSkipQuoted<R2>
|
|
330
|
+
: R
|
|
331
|
+
: '';
|
|
332
|
+
|
|
333
|
+
/** Reads up to the `}` closing an already opened `{`; returns `[content, rest]`. */
|
|
334
|
+
type _IcuBlock<
|
|
335
|
+
S,
|
|
336
|
+
Acc extends string = '',
|
|
337
|
+
Depth extends 0[] = [],
|
|
338
|
+
> = S extends `${infer L}}${infer R}`
|
|
339
|
+
? L extends `${infer L2}{${infer R2}`
|
|
340
|
+
? _IcuBlock<`${R2}}${R}`, `${Acc}${L2}{`, [...Depth, 0]>
|
|
341
|
+
: Depth extends [0, ...infer D extends 0[]]
|
|
342
|
+
? _IcuBlock<R, `${Acc}${L}}`, D>
|
|
343
|
+
: [`${Acc}${L}`, R]
|
|
344
|
+
: never;
|
|
345
|
+
|
|
346
|
+
/** `[name, type]` entries of every argument in a message, nested cases included. */
|
|
347
|
+
type _IcuEntries<S, Acc = never> = S extends `${string}{${infer R}`
|
|
348
|
+
? _IcuBlock<R> extends [infer Arg, infer Rest]
|
|
349
|
+
? _IcuEntries<Rest, Acc | _IcuArg<Arg>>
|
|
350
|
+
: Acc
|
|
351
|
+
: Acc;
|
|
352
|
+
|
|
353
|
+
/** Walks plural/select cases: `[selector keys, entries of all case messages]`. */
|
|
354
|
+
type _IcuCases<S, Keys = never, Acc = never> = S extends `${infer L}{${infer R}`
|
|
355
|
+
? _IcuBlock<R> extends [infer Msg, infer Rest]
|
|
356
|
+
? _IcuCases<Rest, Keys | _IcuTrim<L>, Acc | _IcuEntries<Msg>>
|
|
357
|
+
: [Keys, Acc]
|
|
358
|
+
: [Keys, Acc];
|
|
359
|
+
|
|
360
|
+
type _IcuArg<A> = A extends `${infer Name},${infer Type},${infer Cases}`
|
|
361
|
+
? _IcuTrim<Type> extends 'plural' | 'selectordinal'
|
|
362
|
+
? [_IcuTrim<Name>, _IcuFormatType<_IcuTrim<Type>>] | _IcuCases<Cases>[1]
|
|
363
|
+
: _IcuTrim<Type> extends 'select'
|
|
364
|
+
? _IcuCases<Cases> extends [infer Keys, infer E]
|
|
365
|
+
? [_IcuTrim<Name>, 'other' extends Keys ? Exclude<Keys, 'other'> | (string & {}) : Keys] | E
|
|
366
|
+
: never
|
|
367
|
+
: [_IcuTrim<Name>, _IcuFormatType<_IcuTrim<Type>>] // `{n, number, ::currency/EUR}`
|
|
368
|
+
: A extends `${infer Name},${infer Type}`
|
|
369
|
+
? [_IcuTrim<Name>, _IcuFormatType<_IcuTrim<Type>>]
|
|
370
|
+
: [_IcuTrim<A>, _IcuFormatType<never>];
|
|
371
|
+
|
|
372
|
+
/** `interpolationFormatTypeMap` wins, then ICU's built-in argument types. */
|
|
373
|
+
type _IcuFormatType<F> = [F] extends [never]
|
|
374
|
+
? string | number
|
|
375
|
+
: F extends keyof _InterpolationFormatTypeMap
|
|
376
|
+
? _InterpolationFormatTypeMap[F]
|
|
377
|
+
: F extends 'number' | 'plural' | 'selectordinal'
|
|
378
|
+
? number
|
|
379
|
+
: F extends 'date' | 'time'
|
|
380
|
+
? Date
|
|
381
|
+
: string | number;
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Merges `[name, type]` entries into one record: a name used twice gets both
|
|
385
|
+
* types, a dotted MF2 name (`$user.name`) becomes a nested object.
|
|
386
|
+
*/
|
|
387
|
+
type _ArgsFromEntries<E> =
|
|
388
|
+
_UnionToIntersection<
|
|
389
|
+
E extends [infer Name extends string, infer Type]
|
|
390
|
+
? Name extends `${string}.${string}`
|
|
391
|
+
? $StringKeyPathToRecord<Name, Type>
|
|
392
|
+
: { [K in Name]: Type }
|
|
393
|
+
: never
|
|
394
|
+
> extends infer M
|
|
395
|
+
? [E] extends [never]
|
|
396
|
+
? never
|
|
397
|
+
: { [K in keyof M]: M[K] }
|
|
398
|
+
: never;
|
|
399
|
+
|
|
400
|
+
type _IcuArgsMap<Ret> = Ret extends string
|
|
401
|
+
? string extends Ret
|
|
402
|
+
? never
|
|
403
|
+
: _ArgsFromEntries<_IcuEntries<_IcuStripQuotes<Ret>>>
|
|
404
|
+
: never;
|
|
405
|
+
|
|
406
|
+
/** *********************************************************************
|
|
407
|
+
* MessageFormat 2 variables (`parseInterpolation: 'mf2'`) *
|
|
408
|
+
*********************************************************************** */
|
|
409
|
+
/** Drops `\`-escapes (`\{`, `\}`, `\|`, `\\`), so they are neither braces nor quotes. */
|
|
410
|
+
type _Mf2StripEscapes<S, Acc extends string = ''> = S extends `${infer L}\\${string}${infer R}`
|
|
411
|
+
? _Mf2StripEscapes<R, `${Acc}${L}`>
|
|
412
|
+
: `${Acc}${S & string}`;
|
|
413
|
+
|
|
414
|
+
/** Reads an expression up to its `}`, skipping `|quoted literals|`; returns `[body, rest]`. */
|
|
415
|
+
type _Mf2Expression<S, Acc extends string = ''> = S extends `${infer B}}${infer Rest}`
|
|
416
|
+
? B extends `${infer Before}|${infer Lit}`
|
|
417
|
+
? `${Lit}}${Rest}` extends `${string}|${infer After}`
|
|
418
|
+
? _Mf2Expression<After, `${Acc}${Before} `>
|
|
419
|
+
: never
|
|
420
|
+
: [`${Acc}${B}`, Rest]
|
|
421
|
+
: never;
|
|
422
|
+
|
|
423
|
+
/** `[name, type]` entries of every variable in a message's expressions; `{{` opens a quoted pattern. */
|
|
424
|
+
type _Mf2Entries<S, Acc = never> = S extends `${string}{${infer R}`
|
|
425
|
+
? R extends `{${infer Pattern}`
|
|
426
|
+
? _Mf2Entries<Pattern, Acc>
|
|
427
|
+
: _Mf2Expression<R> extends [infer Body, infer Rest]
|
|
428
|
+
? _Mf2Entries<Rest, Acc | _Mf2ExprEntries<_Mf2Spaces<Body>>>
|
|
429
|
+
: Acc
|
|
430
|
+
: Acc;
|
|
431
|
+
|
|
432
|
+
type _Mf2Space<S, C extends string> = S extends `${infer L}${C}${infer R}`
|
|
433
|
+
? _Mf2Space<`${L} ${R}`, C>
|
|
434
|
+
: S;
|
|
435
|
+
type _Mf2Spaces<S> = _IcuTrim<_Mf2Space<_Mf2Space<_Mf2Space<S, '\n'>, '\t'>, '\r'>>;
|
|
436
|
+
type _Mf2Name<S> = S extends `${infer N} ${string}`
|
|
437
|
+
? _Mf2Name<N>
|
|
438
|
+
: S extends `${infer N}/${string}`
|
|
439
|
+
? N
|
|
440
|
+
: S;
|
|
441
|
+
|
|
442
|
+
/** `{$x :fn opt=$y}`: `$x` typed by its function, option and markup variables `string | number`. */
|
|
443
|
+
type _Mf2ExprEntries<Body> = Body extends `$${infer R}`
|
|
444
|
+
? R extends `${infer Name} ${infer Tail}`
|
|
445
|
+
? | [Name, _Mf2FunctionType<_IcuTrim<Tail> extends `:${infer F}` ? _Mf2Name<F> : never>]
|
|
446
|
+
| _Mf2Vars<Tail>
|
|
447
|
+
: [_Mf2Name<R>, _Mf2FunctionType<never>]
|
|
448
|
+
: _Mf2Vars<Body>;
|
|
449
|
+
|
|
450
|
+
/** Every `$name` in a string, typed `string | number`. */
|
|
451
|
+
type _Mf2Vars<S, Acc = never> = S extends `${string}$${infer R}`
|
|
452
|
+
? _Mf2Vars<R, Acc | [_Mf2Name<R>, string | number]>
|
|
453
|
+
: Acc;
|
|
454
|
+
|
|
455
|
+
/** `interpolationFormatTypeMap` wins, then messageformat's built-in functions. */
|
|
456
|
+
type _Mf2FunctionType<F> = [F] extends [never]
|
|
457
|
+
? string | number
|
|
458
|
+
: F extends keyof _InterpolationFormatTypeMap
|
|
459
|
+
? _InterpolationFormatTypeMap[F]
|
|
460
|
+
: F extends 'number' | 'integer' | 'currency' | 'percent' | 'offset' | 'unit' | 'math'
|
|
461
|
+
? number
|
|
462
|
+
: F extends 'datetime' | 'date' | 'time'
|
|
463
|
+
? Date
|
|
464
|
+
: F extends 'string'
|
|
465
|
+
? string
|
|
466
|
+
: string | number;
|
|
467
|
+
|
|
468
|
+
/** Names declared by `.local $name = ...` (local, not passed in). */
|
|
469
|
+
type _Mf2Locals<S, Acc = never> = S extends `${string}.local${infer R}`
|
|
470
|
+
? _Mf2Locals<R, Acc | (_IcuTrim<R> extends `$${infer N}=${string}` ? _IcuTrim<N> : never)>
|
|
471
|
+
: Acc;
|
|
472
|
+
/** `.match $a $b`: selectors, normally also declared by `.input` or `.local`. */
|
|
473
|
+
type _Mf2Selectors<S> = S extends `${string}.match${infer R}`
|
|
474
|
+
? _Mf2Vars<_Mf2Spaces<R extends `${infer Sel}{${string}` ? Sel : R>>
|
|
475
|
+
: never;
|
|
476
|
+
|
|
477
|
+
type _Mf2ArgsMap<Ret> = Ret extends string
|
|
478
|
+
? string extends Ret
|
|
479
|
+
? never
|
|
480
|
+
: _Mf2StripEscapes<Ret> extends infer S
|
|
481
|
+
? _ArgsFromEntries<
|
|
482
|
+
Exclude<
|
|
483
|
+
_Mf2Entries<S> | _Mf2Selectors<S>,
|
|
484
|
+
[_IcuTrim<S> extends `.${string}` ? _Mf2Locals<S> : never, unknown]
|
|
485
|
+
>
|
|
486
|
+
>
|
|
487
|
+
: never
|
|
488
|
+
: never;
|
|
489
|
+
|
|
490
|
+
/** The variables declared in `defaultVariables` are optional: they are filled in at runtime. */
|
|
491
|
+
type _WithDefaultVariables<Map> = [keyof _DefaultVariables] extends [never]
|
|
492
|
+
? Map
|
|
493
|
+
: Omit<Map, keyof _DefaultVariables> & Partial<Pick<Map, keyof _DefaultVariables & keyof Map>>;
|
|
494
|
+
|
|
495
|
+
export type InterpolationMap<Ret> = _WithDefaultVariables<
|
|
496
|
+
[_ParseInterpolation] extends ['icu']
|
|
497
|
+
? $PreservedValue<_IcuArgsMap<Ret>, Record<string, unknown>>
|
|
498
|
+
: [_ParseInterpolation] extends ['mf2']
|
|
499
|
+
? $PreservedValue<_Mf2ArgsMap<Ret>, Record<string, unknown>>
|
|
500
|
+
: $PreservedValue<
|
|
501
|
+
_InterpolationMapFromEntries<ParseInterpolationEntries<Ret>>,
|
|
502
|
+
Record<string, unknown>
|
|
503
|
+
>
|
|
504
|
+
>;
|
|
505
|
+
|
|
506
|
+
export type ParseTReturnPlural<
|
|
507
|
+
Res,
|
|
508
|
+
Key,
|
|
509
|
+
KeyWithPlural = `${Key & string}${_PluralSeparator}${PluralSuffix}`,
|
|
510
|
+
> = _ValueOf<Res, KeyWithPlural | Key>;
|
|
511
|
+
|
|
512
|
+
export type ParseTReturnPluralOrdinal<
|
|
513
|
+
Res,
|
|
514
|
+
Key,
|
|
515
|
+
KeyWithOrdinalPlural = `${Key &
|
|
516
|
+
string}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`,
|
|
517
|
+
> = _ValueOf<Res, KeyWithOrdinalPlural | Key>;
|
|
518
|
+
|
|
519
|
+
export type ParseTReturnWithFallback<Key, Val> = Val extends ''
|
|
520
|
+
? _ReturnEmptyString extends true
|
|
521
|
+
? ''
|
|
522
|
+
: Key
|
|
523
|
+
: Val extends null
|
|
524
|
+
? _ReturnNull extends true
|
|
525
|
+
? null
|
|
526
|
+
: Key
|
|
527
|
+
: Val;
|
|
528
|
+
|
|
529
|
+
export type ParseTReturn<Key, Res, TOpt extends TOptions = {}> = ParseTReturnWithFallback<
|
|
530
|
+
Key,
|
|
531
|
+
Key extends `${infer K1}${_KeySeparator}${infer RestKey}`
|
|
532
|
+
? ParseTReturn<RestKey, _ValueOf<Res, K1>, TOpt>
|
|
533
|
+
: // Process plurals only if count is provided inside options
|
|
534
|
+
TOpt['count'] extends number
|
|
535
|
+
? TOpt['ordinal'] extends boolean
|
|
536
|
+
? ParseTReturnPluralOrdinal<Res, Key>
|
|
537
|
+
: ParseTReturnPlural<Res, Key>
|
|
538
|
+
: // otherwise access plain key without adding plural and ordinal suffixes
|
|
539
|
+
Res extends readonly unknown[]
|
|
540
|
+
? Key extends `${infer NKey extends number}`
|
|
541
|
+
? Res[NKey]
|
|
542
|
+
: never
|
|
543
|
+
: _ValueOf<Res, Key>
|
|
544
|
+
>;
|
|
545
|
+
|
|
546
|
+
export type TReturnOptionalNull = _ReturnNull extends true ? null : never;
|
|
547
|
+
export type TReturnOptionalObjects<TOpt extends { returnObjects?: unknown }> =
|
|
548
|
+
_ReturnObjects extends true
|
|
549
|
+
? $SpecialObject | string
|
|
550
|
+
: TOpt['returnObjects'] extends true
|
|
551
|
+
? $SpecialObject
|
|
552
|
+
: string;
|
|
553
|
+
export type DefaultTReturn<TOpt extends { returnObjects?: unknown }> =
|
|
554
|
+
TReturnOptionalObjects<TOpt> | TReturnOptionalNull;
|
|
555
|
+
|
|
556
|
+
type _KeyWithContext<Key, Context> = Context extends string
|
|
557
|
+
? `${Key & string}${_ContextSeparator}${Context}`
|
|
558
|
+
: Key;
|
|
559
|
+
|
|
560
|
+
export type KeyWithContext<Key, TOpt extends TOptions> = _NoStrictNullChecks extends true // see ParseKeysByKeyPrefix
|
|
561
|
+
? [TOpt['context']] extends [undefined]
|
|
562
|
+
? Key
|
|
563
|
+
: _KeyWithContext<Key, TOpt['context']>
|
|
564
|
+
: TOpt['context'] extends string
|
|
565
|
+
? `${Key & string}${_ContextSeparator}${TOpt['context']}`
|
|
566
|
+
: Key;
|
|
567
|
+
|
|
568
|
+
export type ContextOfKey<
|
|
569
|
+
Key extends string,
|
|
570
|
+
Ns extends Namespace = DefaultNamespace,
|
|
571
|
+
TOpt extends TOptions = {},
|
|
572
|
+
KPrefix = undefined,
|
|
573
|
+
Keys extends $Dictionary = KeysByTOptions<TOpt>,
|
|
574
|
+
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
|
|
575
|
+
ActualKeys =
|
|
576
|
+
| ParseKeysByKeyPrefix<Keys[$FirstNamespace<ActualNS>], KPrefix>
|
|
577
|
+
| ParseKeysByNamespaces<ActualNS, Keys>
|
|
578
|
+
| ParseKeysByFallbackNs<Keys>,
|
|
579
|
+
> = $IsResourcesDefined extends true
|
|
580
|
+
? Key extends ActualKeys
|
|
581
|
+
? string
|
|
582
|
+
: ActualKeys extends
|
|
583
|
+
| `${Key}${_ContextSeparator}${infer Context}${_PluralSeparator}${PluralSuffix}`
|
|
584
|
+
| `${Key}${_ContextSeparator}${infer Context}`
|
|
585
|
+
? Context
|
|
586
|
+
: never
|
|
587
|
+
: string;
|
|
588
|
+
|
|
589
|
+
// helper that maps the configured fallbackNS value to the matching resources slice
|
|
590
|
+
export type FallbackResourcesOf<FallbackNS, R> = FallbackNS extends readonly (infer FN)[]
|
|
591
|
+
? R[FN & keyof R]
|
|
592
|
+
: [FallbackNS] extends [false]
|
|
593
|
+
? never
|
|
594
|
+
: R[Extract<FallbackNS, keyof R> & keyof R];
|
|
595
|
+
|
|
596
|
+
/* reuse the parse helpers as top-level aliases (no nested type declarations) */
|
|
597
|
+
type _PrimaryParse<
|
|
598
|
+
ActualKey,
|
|
599
|
+
PrimaryNS extends keyof Resources,
|
|
600
|
+
TOpt extends TOptions,
|
|
601
|
+
> = ParseTReturn<ActualKey, Resources[PrimaryNS], TOpt>;
|
|
602
|
+
|
|
603
|
+
type _FallbackParse<ActualKey, FallbackNS, TOpt extends TOptions> = [
|
|
604
|
+
FallbackResourcesOf<FallbackNS, Resources>,
|
|
605
|
+
] extends [never]
|
|
606
|
+
? never
|
|
607
|
+
: ParseTReturn<ActualKey, FallbackResourcesOf<FallbackNS, Resources>, TOpt>;
|
|
608
|
+
|
|
609
|
+
export type TFunctionReturn<
|
|
610
|
+
Ns extends Namespace,
|
|
611
|
+
Key,
|
|
612
|
+
TOpt extends TOptions,
|
|
613
|
+
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
|
|
614
|
+
ActualKey = KeyWithContext<Key, TOpt>,
|
|
615
|
+
// a key without a value, e.g. a plural key without `count`
|
|
616
|
+
NotFound = DefaultTReturn<TOpt>,
|
|
617
|
+
> = $IsResourcesDefined extends true
|
|
618
|
+
? ActualKey extends `${infer Nsp}${_NsSeparator}${infer RestKey}`
|
|
619
|
+
? [ParseTReturn<RestKey, Resources[Nsp & keyof Resources], TOpt>] extends [never]
|
|
620
|
+
? NotFound
|
|
621
|
+
: ParseTReturn<RestKey, Resources[Nsp & keyof Resources], TOpt>
|
|
622
|
+
: $FirstNamespace<ActualNS> extends infer PrimaryNS
|
|
623
|
+
? [PrimaryNS] extends [keyof Resources]
|
|
624
|
+
? [_PrimaryParse<ActualKey, PrimaryNS & keyof Resources, TOpt>] extends [never]
|
|
625
|
+
? [_FallbackParse<ActualKey, _FallbackNamespace, TOpt>] extends [never]
|
|
626
|
+
? NotFound
|
|
627
|
+
: _FallbackParse<ActualKey, _FallbackNamespace, TOpt>
|
|
628
|
+
: _PrimaryParse<ActualKey, PrimaryNS & keyof Resources, TOpt>
|
|
629
|
+
: never
|
|
630
|
+
: never
|
|
631
|
+
: DefaultTReturn<TOpt>;
|
|
632
|
+
|
|
633
|
+
export type TFunctionDetailedResult<T = string, TOpt extends TOptions = {}> = {
|
|
634
|
+
/**
|
|
635
|
+
* The plain used key
|
|
636
|
+
*/
|
|
637
|
+
usedKey: string;
|
|
638
|
+
/**
|
|
639
|
+
* The translation result.
|
|
640
|
+
*/
|
|
641
|
+
res: T;
|
|
642
|
+
/**
|
|
643
|
+
* The key with context / plural
|
|
644
|
+
*/
|
|
645
|
+
exactUsedKey: string;
|
|
646
|
+
/**
|
|
647
|
+
* The used language for this translation.
|
|
648
|
+
*/
|
|
649
|
+
usedLng: string;
|
|
650
|
+
/**
|
|
651
|
+
* The used namespace for this translation.
|
|
652
|
+
*/
|
|
653
|
+
usedNS: string;
|
|
654
|
+
/**
|
|
655
|
+
* The parameters used for interpolation.
|
|
656
|
+
*/
|
|
657
|
+
usedParams: InterpolationMap<T> & { count?: TOpt['count'] };
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
export type TFunctionProcessReturnValue<Ret, DefaultValue> = Ret extends
|
|
661
|
+
string | $SpecialObject | null
|
|
662
|
+
? Ret
|
|
663
|
+
: [DefaultValue] extends [never]
|
|
664
|
+
? Ret
|
|
665
|
+
: DefaultValue;
|
|
666
|
+
|
|
667
|
+
export type TFunctionReturnOptionalDetails<
|
|
668
|
+
Ret,
|
|
669
|
+
TOpt extends TOptions,
|
|
670
|
+
> = TOpt['returnDetails'] extends true ? TFunctionDetailedResult<Ret, TOpt> : Ret;
|
|
671
|
+
|
|
672
|
+
type _AppendKeyPrefix<Key, KPrefix> = KPrefix extends string
|
|
673
|
+
? `${KPrefix}${_KeyPrefixSeparator}${Key & string}`
|
|
674
|
+
: Key;
|
|
675
|
+
|
|
676
|
+
export type AppendKeyPrefix<Key, KPrefix> = _NoStrictNullChecks extends true // see ParseKeysByKeyPrefix
|
|
677
|
+
? [KPrefix] extends [undefined]
|
|
678
|
+
? Key
|
|
679
|
+
: _AppendKeyPrefix<Key, KPrefix>
|
|
680
|
+
: _AppendKeyPrefix<Key, KPrefix>;
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Resolves the effective key prefix by preferring a per-call `keyPrefix` from
|
|
684
|
+
* options over the interface-level `KPrefix` (set via getFixedT's 3rd argument).
|
|
685
|
+
*/
|
|
686
|
+
export type EffectiveKPrefix<KPrefix, TOpt> = TOpt extends { keyPrefix: infer OptKP extends string }
|
|
687
|
+
? OptKP
|
|
688
|
+
: KPrefix;
|
|
689
|
+
|
|
690
|
+
/** `parseInterpolation: 'icu'` or `'mf2'`: messages are typed by their arguments. */
|
|
691
|
+
type _ParsesMessageFormat = [_ParseInterpolation] extends ['icu'] | ['mf2'] ? true : false;
|
|
692
|
+
/**
|
|
693
|
+
* Whether `t()` options are required for a message with variables: always under
|
|
694
|
+
* ICU / MF2 (on a missing one, i18next-icu renders the raw message and
|
|
695
|
+
* i18next-mf2 a `{$name}` fallback), and with `strictInterpolationChecks: true`
|
|
696
|
+
* for `{{variables}}` (i18next renders the raw placeholder).
|
|
697
|
+
*/
|
|
698
|
+
type _RequiresMessageOptions = _ParsesMessageFormat extends true
|
|
699
|
+
? true
|
|
700
|
+
: [_ParseInterpolation] extends [true]
|
|
701
|
+
? _StrictInterpolationChecks extends true
|
|
702
|
+
? true
|
|
703
|
+
: false
|
|
704
|
+
: false;
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* `t()` options are optional, except for a message with variables when
|
|
708
|
+
* `_RequiresMessageOptions` is true. The conditional resolves eagerly; a generic
|
|
709
|
+
* one here would break inference.
|
|
710
|
+
*/
|
|
711
|
+
type _KeyAndOptions<Key, Ret, Opts> = _RequiresMessageOptions extends true
|
|
712
|
+
? [key: Key | Key[], options: Opts] | [key: (Key | Key[]) & _IfOptionalOptions<Ret>]
|
|
713
|
+
: [key: Key | Key[], options?: Opts];
|
|
714
|
+
/**
|
|
715
|
+
* `t(key, defaultValue)` under `strictKeyChecks`: the variables are required only
|
|
716
|
+
* with `strictInterpolationChecks`, else `unknown` (the call stays as before).
|
|
717
|
+
*/
|
|
718
|
+
type _IfOptionalDefaultOptions<Ret> = _StrictInterpolationChecks extends true
|
|
719
|
+
? _IfOptionalOptions<Ret>
|
|
720
|
+
: unknown;
|
|
721
|
+
/** `unknown` when options may be left out, else a type naming the missing arguments. */
|
|
722
|
+
type _IfOptionalOptions<Ret> = _RequiresMessageOptions extends true
|
|
723
|
+
? {} extends InterpolationMap<Ret>
|
|
724
|
+
? unknown
|
|
725
|
+
: { 'pass these arguments as t() options': InterpolationMap<Ret> }
|
|
726
|
+
: unknown;
|
|
727
|
+
/**
|
|
728
|
+
* What a call without options is checked against: `Ret`, unless a key without a
|
|
729
|
+
* value of its own (a plural key without `count`) widened it to `string`, which
|
|
730
|
+
* hid the variables of every other key in a union. Only with
|
|
731
|
+
* `strictInterpolationChecks`: without it (ICU and MF2 check their arguments
|
|
732
|
+
* regardless of the flag) the types are the v26 ones.
|
|
733
|
+
*/
|
|
734
|
+
type _CheckedReturn<
|
|
735
|
+
Ret,
|
|
736
|
+
Ns extends Namespace,
|
|
737
|
+
Key,
|
|
738
|
+
TOpt extends TOptions,
|
|
739
|
+
> = _StrictInterpolationChecks extends true
|
|
740
|
+
? string extends Ret
|
|
741
|
+
? _CheckedValue<Ns, Key, TOpt>
|
|
742
|
+
: Ret
|
|
743
|
+
: Ret;
|
|
744
|
+
/**
|
|
745
|
+
* Per key: its value, or the plural forms of a key without one; `never` (not
|
|
746
|
+
* `string`) for a key without either. The first lookup is the one `Ret` made
|
|
747
|
+
* (cached), so only a key without a value is looked up again.
|
|
748
|
+
*/
|
|
749
|
+
type _CheckedValue<Ns extends Namespace, Key, TOpt extends TOptions> = Key extends unknown
|
|
750
|
+
? TFunctionReturn<
|
|
751
|
+
Ns,
|
|
752
|
+
Key,
|
|
753
|
+
TOpt,
|
|
754
|
+
NsByTOptions<Ns, TOpt>,
|
|
755
|
+
KeyWithContext<Key, TOpt>,
|
|
756
|
+
never
|
|
757
|
+
> extends infer V
|
|
758
|
+
? [V] extends [never]
|
|
759
|
+
? _ValueWithCount<NsByTOptions<Ns, TOpt>, KeyWithContext<Key, TOpt>>
|
|
760
|
+
: V
|
|
761
|
+
: never
|
|
762
|
+
: never;
|
|
763
|
+
/** The plural forms of a key, ordinal ones too, `never` for a key without them. */
|
|
764
|
+
type _ValueWithCount<
|
|
765
|
+
Ns extends Namespace,
|
|
766
|
+
Key,
|
|
767
|
+
K = Key | `${Key & string}${_PluralSeparator}ordinal`,
|
|
768
|
+
> = TFunctionReturn<Ns, K, { count: number }, Ns, K, never>;
|
|
769
|
+
/**
|
|
770
|
+
* The variables a call with options must pass. With `strictInterpolationChecks`
|
|
771
|
+
* those of `_CheckedReturn`, so a plural key without `count` needs the variables
|
|
772
|
+
* of its forms, `count` included; without the flag those of `Ret`, as in v26.
|
|
773
|
+
*/
|
|
774
|
+
type _OptionsMap<
|
|
775
|
+
Ret,
|
|
776
|
+
Ns extends Namespace,
|
|
777
|
+
Key,
|
|
778
|
+
TOpt extends TOptions,
|
|
779
|
+
> = _StrictInterpolationChecks extends true
|
|
780
|
+
? InterpolationMap<$NoInfer<_CheckedReturn<Ret, Ns, Key, TOpt>>>
|
|
781
|
+
: InterpolationMap<Ret>;
|
|
782
|
+
/** Options parameter of a selector overload: required for a message with arguments. */
|
|
783
|
+
type _SelectorOptionsArg<Ret, Opts> =
|
|
784
|
+
unknown extends _IfOptionalOptions<Ret> ? [options?: Opts] : [options: Opts];
|
|
785
|
+
/** Union of the values an array of selectors resolves to (plural brands kept). */
|
|
786
|
+
type _SelectorsReturn<Fns extends readonly unknown[]> = Fns[number] extends (
|
|
787
|
+
...args: any[]
|
|
788
|
+
) => infer R
|
|
789
|
+
? R
|
|
790
|
+
: never;
|
|
791
|
+
/** Whether any member of the union is a plural group (`false` for `any`). */
|
|
792
|
+
type _HasPlural<R> = 0 extends 1 & R
|
|
793
|
+
? false
|
|
794
|
+
: [Extract<R, PluralValue<string>>] extends [never]
|
|
795
|
+
? false
|
|
796
|
+
: true;
|
|
797
|
+
/** `{ count: number }` when any member of the union is a plural group. */
|
|
798
|
+
type _CountIfPlural<R> = _HasPlural<R> extends true ? { count: number } : {};
|
|
799
|
+
/** Options of a selector-array overload: required for variables or a plural member. */
|
|
800
|
+
type _SelectorArrayOptionsArg<R, Opts> =
|
|
801
|
+
_HasPlural<R> extends true
|
|
802
|
+
? [options: Opts & { count: number } & InterpolationMap<DeepUnwrapPlural<R>>]
|
|
803
|
+
: _SelectorOptionsArg<DeepUnwrapPlural<R>, Opts & InterpolationMap<DeepUnwrapPlural<R>>>;
|
|
804
|
+
|
|
805
|
+
/** ************************
|
|
806
|
+
* T function declaration *
|
|
807
|
+
************************* */
|
|
808
|
+
|
|
809
|
+
export interface TFunctionStrict<
|
|
810
|
+
Ns extends Namespace = DefaultNamespace,
|
|
811
|
+
KPrefix = undefined,
|
|
812
|
+
> extends Branded<Ns> {
|
|
813
|
+
<
|
|
814
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
815
|
+
const TOpt extends TOptions,
|
|
816
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
817
|
+
>(
|
|
818
|
+
key: Key | Key[],
|
|
819
|
+
options: TOpt &
|
|
820
|
+
_OptionsMap<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt> & {
|
|
821
|
+
context?: Key extends string
|
|
822
|
+
? unknown extends TOpt['context']
|
|
823
|
+
? TOpt['context']
|
|
824
|
+
: ContextOfKey<Key, Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>>
|
|
825
|
+
: never;
|
|
826
|
+
},
|
|
827
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
828
|
+
<
|
|
829
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
830
|
+
const TOpt extends TOptions,
|
|
831
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
832
|
+
>(
|
|
833
|
+
key: (Key | Key[]) &
|
|
834
|
+
_IfOptionalOptions<
|
|
835
|
+
$NoInfer<
|
|
836
|
+
_CheckedReturn<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>
|
|
837
|
+
>
|
|
838
|
+
>,
|
|
839
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
840
|
+
<
|
|
841
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
842
|
+
const TOpt extends TOptions,
|
|
843
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
844
|
+
>(
|
|
845
|
+
key: Key | Key[],
|
|
846
|
+
defaultValue: string,
|
|
847
|
+
options: TOpt &
|
|
848
|
+
_OptionsMap<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt> & {
|
|
849
|
+
context?: Key extends string
|
|
850
|
+
? unknown extends TOpt['context']
|
|
851
|
+
? TOpt['context']
|
|
852
|
+
: ContextOfKey<Key, Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>>
|
|
853
|
+
: never;
|
|
854
|
+
},
|
|
855
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
856
|
+
<
|
|
857
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
858
|
+
const TOpt extends TOptions,
|
|
859
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
860
|
+
>(
|
|
861
|
+
key: (Key | Key[]) &
|
|
862
|
+
_IfOptionalDefaultOptions<
|
|
863
|
+
$NoInfer<
|
|
864
|
+
_CheckedReturn<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>
|
|
865
|
+
>
|
|
866
|
+
>,
|
|
867
|
+
defaultValue: string,
|
|
868
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
export interface TFunctionNonStrict<
|
|
872
|
+
Ns extends Namespace = DefaultNamespace,
|
|
873
|
+
KPrefix = undefined,
|
|
874
|
+
> extends Branded<Ns> {
|
|
875
|
+
<
|
|
876
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
877
|
+
const TOpt extends TOptions,
|
|
878
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
879
|
+
const ActualOptions extends Omit<TOpt, 'context'> &
|
|
880
|
+
_OptionsMap<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt> & {
|
|
881
|
+
context?: Key extends string
|
|
882
|
+
? unknown extends TOpt['context']
|
|
883
|
+
? TOpt['context']
|
|
884
|
+
: ContextOfKey<Key, Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>>
|
|
885
|
+
: never;
|
|
886
|
+
} = TOpt &
|
|
887
|
+
_OptionsMap<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt> & {
|
|
888
|
+
context?: Key extends string
|
|
889
|
+
? unknown extends TOpt['context']
|
|
890
|
+
? TOpt['context']
|
|
891
|
+
: ContextOfKey<Key, Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>>
|
|
892
|
+
: never;
|
|
893
|
+
},
|
|
894
|
+
DefaultValue extends string = never,
|
|
895
|
+
>(
|
|
896
|
+
// `[key, defaultValue, options?]` is written as two tuples: on a failed call,
|
|
897
|
+
// TypeScript elaborates the error against the last of the equally matching
|
|
898
|
+
// members, and TypeScript 7 sorts a 3-element tuple last, so a wrong options
|
|
899
|
+
// object was reported as "not assignable to type 'string'" instead of the
|
|
900
|
+
// missing or mistyped argument.
|
|
901
|
+
...args:
|
|
902
|
+
| [key: string | string[], options: TOpt & $Dictionary & { defaultValue: DefaultValue }]
|
|
903
|
+
| [key: string | string[], defaultValue: DefaultValue]
|
|
904
|
+
| [key: string | string[], defaultValue: DefaultValue, options: TOpt & $Dictionary]
|
|
905
|
+
| _KeyAndOptions<
|
|
906
|
+
Key,
|
|
907
|
+
$NoInfer<
|
|
908
|
+
_CheckedReturn<Ret, Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>
|
|
909
|
+
>,
|
|
910
|
+
ActualOptions
|
|
911
|
+
>
|
|
912
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, DefaultValue>, TOpt>;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
export type TFunctionSignature<
|
|
916
|
+
Ns extends Namespace = DefaultNamespace,
|
|
917
|
+
KPrefix = undefined,
|
|
918
|
+
> = _EnableSelector extends true | 'optimize' | 'strict'
|
|
919
|
+
? TFunctionSelector<Ns, KPrefix, GetSource<Ns, KPrefix>>
|
|
920
|
+
: _StrictKeyChecks extends true
|
|
921
|
+
? TFunctionStrict<Ns, KPrefix>
|
|
922
|
+
: TFunctionNonStrict<Ns, KPrefix>;
|
|
923
|
+
|
|
924
|
+
export interface TFunction<
|
|
925
|
+
Ns extends Namespace = DefaultNamespace,
|
|
926
|
+
KPrefix = undefined,
|
|
927
|
+
> extends TFunctionSignature<Ns, KPrefix> {}
|
|
928
|
+
|
|
929
|
+
export type KeyPrefix<Ns extends Namespace> =
|
|
930
|
+
| (TypeOptions['keySeparator'] extends false
|
|
931
|
+
? _DotPrefixes<ResourceKeys<true>[$FirstNamespace<Ns>]>
|
|
932
|
+
: ResourceKeys<true>[$FirstNamespace<Ns>])
|
|
933
|
+
| undefined;
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* With `keySeparator: false` a key prefix is joined with `.` and the result is looked up as a flat
|
|
937
|
+
* key, so the prefixes are the parts of the flat keys before a `.`: `'a'` and `'a.b'` for `'a.b.c'`.
|
|
938
|
+
*/
|
|
939
|
+
type _DotPrefixes<Keys> = string extends Keys ? string : _DotPrefixesOf<Keys>;
|
|
940
|
+
type _DotPrefixesOf<Key> = Key extends `${infer Head}.${infer Tail}`
|
|
941
|
+
? Head | `${Head}.${_DotPrefixesOf<Tail>}`
|
|
942
|
+
: never;
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* The keys of `ParseKeys<Ns, {}, KPrefix>` that `t()` takes without options: their string has no
|
|
946
|
+
* variables, and a plural key is left out when its forms take `count`. For a wrapper that passes a
|
|
947
|
+
* key on to `t()`, which then compiles with `strictInterpolationChecks` (since v26.7.0):
|
|
948
|
+
*
|
|
949
|
+
* @example
|
|
950
|
+
* ```ts
|
|
951
|
+
* import type { ParseKeysWithoutVariables } from 'i18next';
|
|
952
|
+
*
|
|
953
|
+
* const label = (key: ParseKeysWithoutVariables<'common'>) => t(key);
|
|
954
|
+
* ```
|
|
955
|
+
*/
|
|
956
|
+
export type ParseKeysWithoutVariables<
|
|
957
|
+
Ns extends Namespace = DefaultNamespace,
|
|
958
|
+
KPrefix = undefined,
|
|
959
|
+
> = _KeysWithoutVariables<ParseKeys<Ns, {}, KPrefix>, Ns, KPrefix>;
|
|
960
|
+
|
|
961
|
+
// a plural key without a value of its own is checked against its plural forms
|
|
962
|
+
type _KeysWithoutVariables<Key, Ns extends Namespace, KPrefix> = Key extends string
|
|
963
|
+
? {} extends InterpolationMap<_CheckedValue<Ns, AppendKeyPrefix<Key, KPrefix>, {}>>
|
|
964
|
+
? Key
|
|
965
|
+
: never
|
|
966
|
+
: never;
|
|
967
|
+
|
|
968
|
+
/**
|
|
969
|
+
* Strict variant: every namespace (primary included) is exposed under its own
|
|
970
|
+
* key on `$`. The flattened-primary form (`Resources[Ns[0]] &`) is dropped, so
|
|
971
|
+
* `$.foo` no longer typechecks when `foo` is a key in the primary namespace —
|
|
972
|
+
* callers must write `$.primary.foo`. Applied uniformly to single- and
|
|
973
|
+
* multi-ns hooks under `enableSelector: 'strict'`.
|
|
974
|
+
*/
|
|
975
|
+
type _NsResourceStrict<Ns extends Namespace> = Ns extends readonly any[]
|
|
976
|
+
? PickNamespaces<Resources, Ns[number]>
|
|
977
|
+
: PickNamespaces<Resources, Ns & keyof Resources>;
|
|
978
|
+
|
|
979
|
+
/** The raw (unfiltered) resource object for a given namespace. */
|
|
980
|
+
export type NsResource<Ns extends Namespace> = [_EnableSelector] extends ['strict']
|
|
981
|
+
? _NsResourceStrict<Ns>
|
|
982
|
+
: Ns extends readonly [keyof Resources, any, ...any]
|
|
983
|
+
? Resources[Ns[0]] & PickNamespaces<Resources, Ns[number]>
|
|
984
|
+
: Resources[$FirstNamespace<Ns>];
|
|
985
|
+
|
|
986
|
+
/** A selector function that can be used as `keyPrefix` to scope `t()` to a sub-tree of the resource. */
|
|
987
|
+
export type KeyPrefixSelector<Ns extends Namespace> = (src: NsResource<Ns>) => object;
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* The type of a selector function accepted by `t()` for a given namespace and optional key prefix.
|
|
991
|
+
* Use this instead of `Parameters<TFunction<Ns>>[0]` for a stable, readable type.
|
|
992
|
+
*
|
|
993
|
+
* @example
|
|
994
|
+
* ```ts
|
|
995
|
+
* import type { SelectorParam } from 'i18next';
|
|
996
|
+
*
|
|
997
|
+
* interface CmpProps {
|
|
998
|
+
* i18nKey: SelectorParam<'myNamespace'>;
|
|
999
|
+
* }
|
|
1000
|
+
* ```
|
|
1001
|
+
*/
|
|
1002
|
+
export type SelectorParam<Ns extends Namespace = DefaultNamespace, KPrefix = undefined> = (
|
|
1003
|
+
src: Select<GetSource<Ns, KPrefix>, undefined>,
|
|
1004
|
+
) => string;
|
|
1005
|
+
|
|
1006
|
+
/// ////////////// ///
|
|
1007
|
+
/// ↆ selector ↆ ///
|
|
1008
|
+
/// ////////////// ///
|
|
1009
|
+
|
|
1010
|
+
declare const $PluralBrand: unique symbol;
|
|
1011
|
+
/** Marks a value as coming from a plural-form key, requiring `count` in the selector options. */
|
|
1012
|
+
export type PluralValue<T extends string> = T & { readonly [$PluralBrand]: typeof $PluralBrand };
|
|
1013
|
+
|
|
1014
|
+
declare const $SelectorKeyBrand: unique symbol;
|
|
1015
|
+
/**
|
|
1016
|
+
* A branded string produced by {@link keyFromSelector}.
|
|
1017
|
+
* Can be passed directly to `t()` when the selector API is enabled.
|
|
1018
|
+
*/
|
|
1019
|
+
export type SelectorKey = string & { readonly [$SelectorKeyBrand]: typeof $SelectorKeyBrand };
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Type-safe signature for {@link keyFromSelector}.
|
|
1023
|
+
* Constrains the selector callback against the default namespace's resources
|
|
1024
|
+
* (same source that `t()` uses for selectors without explicit `ns`).
|
|
1025
|
+
* When resources are not defined, accepts any selector.
|
|
1026
|
+
*/
|
|
1027
|
+
export type KeyFromSelectorFn = <
|
|
1028
|
+
Ns extends Namespace = DefaultNamespace,
|
|
1029
|
+
KPrefix extends KeyPrefix<Ns> = undefined,
|
|
1030
|
+
>(
|
|
1031
|
+
selector: (src: Select<GetSource<Ns, KPrefix>, undefined>) => any,
|
|
1032
|
+
opts?: { ns?: Ns; keyPrefix?: KPrefix },
|
|
1033
|
+
) => SelectorKey;
|
|
1034
|
+
|
|
1035
|
+
/** Recursively strips the {@link PluralValue} brand from a type (handles nested objects for `returnObjects`). */
|
|
1036
|
+
export type DeepUnwrapPlural<T> =
|
|
1037
|
+
T extends PluralValue<infer U>
|
|
1038
|
+
? U
|
|
1039
|
+
: T extends readonly any[]
|
|
1040
|
+
? { [I in keyof T]: DeepUnwrapPlural<T[I]> }
|
|
1041
|
+
: T extends object
|
|
1042
|
+
? { [K in keyof T]: DeepUnwrapPlural<T[K]> }
|
|
1043
|
+
: T;
|
|
1044
|
+
|
|
1045
|
+
export type NsArg<Ns extends Namespace> = Ns[number] | readonly Ns[number][];
|
|
1046
|
+
|
|
1047
|
+
export interface TFunctionSelector<Ns extends Namespace, KPrefix, Source> extends Branded<Ns> {
|
|
1048
|
+
// ── Pre-computed key(s) from keyFromSelector ────────────────────────────────
|
|
1049
|
+
// Accepts a branded `SelectorKey` (or array of them) produced by `keyFromSelector`.
|
|
1050
|
+
// Return-type precision is traded for the flexibility of decoupled key creation.
|
|
1051
|
+
// Placed first so that the more general selector overloads (below) are last —
|
|
1052
|
+
// TypeScript's `Parameters<>` utility resolves against the last call signature.
|
|
1053
|
+
<const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>>(
|
|
1054
|
+
key: SelectorKey | SelectorKey[],
|
|
1055
|
+
...args: [options?: Opts & $Dictionary]
|
|
1056
|
+
): DefaultTReturn<Opts>;
|
|
1057
|
+
|
|
1058
|
+
// ── Array of selectors with explicit `ns` ───────────────────────────────────
|
|
1059
|
+
<
|
|
1060
|
+
Target extends ConstrainTarget<Opts>,
|
|
1061
|
+
const NewNs extends NsArg<Ns> & Namespace,
|
|
1062
|
+
const Opts extends SelectorOptions<NewNs>,
|
|
1063
|
+
NewSrc extends GetSource<NewNs, KPrefix>,
|
|
1064
|
+
>(
|
|
1065
|
+
selectors: readonly SelectorFn<NewSrc, ApplyTarget<Target, Opts>, Opts>[],
|
|
1066
|
+
options: Opts & InterpolationMap<Target> & { ns: NewNs } & _CountIfPlural<Target>,
|
|
1067
|
+
): SelectorReturn<Target, Opts>;
|
|
1068
|
+
|
|
1069
|
+
// ── Single selector with explicit `ns` ──────────────────────────────────────
|
|
1070
|
+
<
|
|
1071
|
+
Target extends ConstrainTarget<Opts>,
|
|
1072
|
+
const NewNs extends NsArg<Ns> & Namespace,
|
|
1073
|
+
const Opts extends SelectorOptions<NewNs>,
|
|
1074
|
+
NewSrc extends GetSource<NewNs, KPrefix>,
|
|
1075
|
+
>(
|
|
1076
|
+
selector: SelectorFn<NewSrc, ApplyTarget<Target, Opts>, Opts>,
|
|
1077
|
+
options: Opts &
|
|
1078
|
+
InterpolationMap<DeepUnwrapPlural<Target>> & {
|
|
1079
|
+
ns: NewNs;
|
|
1080
|
+
} & (Target extends PluralValue<string> ? { count: number } : {}),
|
|
1081
|
+
): SelectorReturn<Target, Opts>;
|
|
1082
|
+
|
|
1083
|
+
// ── Array of selectors with defaultValue — preserves literal type of DV ─────
|
|
1084
|
+
// Same TypeScript limitation as the single-selector defaultValue overload below:
|
|
1085
|
+
// `const Opts` loses the `defaultValue` literal when inferred through the
|
|
1086
|
+
// conditional rest tuple of the general array overload. A plural member
|
|
1087
|
+
// requires `count`, as in the single-selector overload.
|
|
1088
|
+
<
|
|
1089
|
+
const Fns extends readonly ((src: Select<Source, Opts['context']>) => string | object)[],
|
|
1090
|
+
const DV extends string,
|
|
1091
|
+
const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>,
|
|
1092
|
+
>(
|
|
1093
|
+
selectors: Fns,
|
|
1094
|
+
options: Opts & { defaultValue: DV } & _CountIfPlural<_SelectorsReturn<Fns>> &
|
|
1095
|
+
InterpolationMap<DeepUnwrapPlural<_SelectorsReturn<Fns>>>,
|
|
1096
|
+
): SelectorReturn<_SelectorsReturn<Fns>, Opts, DV>;
|
|
1097
|
+
|
|
1098
|
+
// ── Array of selectors with default `ns` ─────────────────────────────────────
|
|
1099
|
+
// Captures the selector tuple as `const Fns` so TypeScript preserves each
|
|
1100
|
+
// element's exact return type. The union of return types is then extracted
|
|
1101
|
+
// via a distributive `infer`, which correctly handles mixed PluralValue<T> and
|
|
1102
|
+
// plain-string callbacks that would otherwise cause TypeScript to "lock in" the
|
|
1103
|
+
// type from the first element.
|
|
1104
|
+
<
|
|
1105
|
+
const Fns extends readonly ((src: Select<Source, Opts['context']>) => string | object)[],
|
|
1106
|
+
const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>,
|
|
1107
|
+
>(
|
|
1108
|
+
selectors: Fns,
|
|
1109
|
+
...args: _SelectorArrayOptionsArg<_SelectorsReturn<Fns>, Opts>
|
|
1110
|
+
): SelectorReturn<_SelectorsReturn<Fns>, Opts>;
|
|
1111
|
+
|
|
1112
|
+
// ── Single selector with context + returnObjects ────────────────────────────
|
|
1113
|
+
// More specific than the general context overload below. Captures `Context` as
|
|
1114
|
+
// a const generic and uses `const Fn` + `ReturnType<Fn>` so FilterKeys sees the
|
|
1115
|
+
// concrete context and the callback's object/array shape is preserved (#2398).
|
|
1116
|
+
// Without this, `ConstrainTarget` / `ApplyTarget` collapse to `unknown` when
|
|
1117
|
+
// `returnObjects: true`.
|
|
1118
|
+
<
|
|
1119
|
+
const Context extends string,
|
|
1120
|
+
const Fn extends (src: Select<Source, Context>) => any,
|
|
1121
|
+
const Opts extends SelectorOptions<Ns[number]> & {
|
|
1122
|
+
context: Context;
|
|
1123
|
+
returnObjects: true;
|
|
1124
|
+
} = SelectorOptions<Ns[number]> & { context: Context; returnObjects: true },
|
|
1125
|
+
>(
|
|
1126
|
+
selector: Fn,
|
|
1127
|
+
options: Opts & InterpolationMap<DeepUnwrapPlural<ReturnType<Fn>>>,
|
|
1128
|
+
): SelectorReturn<ReturnType<Fn>, Opts>;
|
|
1129
|
+
|
|
1130
|
+
// ── Single selector with context — bypasses count enforcement ────────────────
|
|
1131
|
+
// When `context` is present in options, `Target` is derived from the
|
|
1132
|
+
// context-filtered source (third mapped type of FilterKeys), which does NOT
|
|
1133
|
+
// apply the PluralValue brand. A separate overload avoids the circular
|
|
1134
|
+
// inference that would otherwise occur when `Opts['context']` sits inside the
|
|
1135
|
+
// conditional rest tuple of the overload below.
|
|
1136
|
+
<
|
|
1137
|
+
Target extends ConstrainTarget<Opts>,
|
|
1138
|
+
const NewNs extends NsArg<Ns> = Ns[number],
|
|
1139
|
+
const Opts extends SelectorOptions<NewNs> & { context: string } = SelectorOptions<NewNs> & {
|
|
1140
|
+
context: string;
|
|
1141
|
+
},
|
|
1142
|
+
>(
|
|
1143
|
+
selector: SelectorFn<Source, ApplyTarget<Target, Opts>, Opts>,
|
|
1144
|
+
options: Opts & InterpolationMap<Target>,
|
|
1145
|
+
): SelectorReturn<Target, Opts>;
|
|
1146
|
+
|
|
1147
|
+
// ── Single selector with defaultValue — preserves literal type of DV ────────
|
|
1148
|
+
// `const Opts` loses literal precision for `defaultValue` when inferred
|
|
1149
|
+
// through a conditional rest tuple (TypeScript limitation). This dedicated
|
|
1150
|
+
// overload captures `DV` from a regular (non-conditional) parameter position,
|
|
1151
|
+
// preserving its literal type. Count enforcement for plural keys is achieved
|
|
1152
|
+
// via a conditional intersection on the options parameter.
|
|
1153
|
+
<
|
|
1154
|
+
const Fn extends (src: Select<Source, undefined>) => ConstrainTarget<Opts>,
|
|
1155
|
+
const DV extends string,
|
|
1156
|
+
const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>,
|
|
1157
|
+
>(
|
|
1158
|
+
selector: Fn,
|
|
1159
|
+
options: Opts & { defaultValue: DV } & (ReturnType<Fn> extends PluralValue<string>
|
|
1160
|
+
? { count: number }
|
|
1161
|
+
: {}) &
|
|
1162
|
+
InterpolationMap<DeepUnwrapPlural<ReturnType<Fn>>>,
|
|
1163
|
+
): SelectorReturn<ReturnType<Fn>, Opts, DV>;
|
|
1164
|
+
|
|
1165
|
+
// ── Single selector without context — enforces count for plural keys ──────────
|
|
1166
|
+
// Uses `const Fn` to capture the selector's exact return type independently of
|
|
1167
|
+
// `Opts`, breaking the circular dependency between `Target` inference and the
|
|
1168
|
+
// conditional rest tuple. `ReturnType<Fn>` drives both the plural check and
|
|
1169
|
+
// the return type; `Opts` is inferred later from the resolved rest args.
|
|
1170
|
+
<
|
|
1171
|
+
const Fn extends (src: Select<Source, undefined>) => ConstrainTarget<Opts>,
|
|
1172
|
+
const Opts extends SelectorOptions<Ns[number]> = SelectorOptions<Ns[number]>,
|
|
1173
|
+
>(
|
|
1174
|
+
selector: Fn,
|
|
1175
|
+
...args: ReturnType<Fn> extends PluralValue<string>
|
|
1176
|
+
? [options: Opts & { count: number } & InterpolationMap<DeepUnwrapPlural<ReturnType<Fn>>>]
|
|
1177
|
+
: _SelectorOptionsArg<ReturnType<Fn>, Opts & InterpolationMap<ReturnType<Fn>>>
|
|
1178
|
+
): SelectorReturn<ReturnType<Fn>, Opts>;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
export interface SelectorOptions<Ns = Namespace>
|
|
1182
|
+
extends Omit<TOptionsBase, 'ns' | 'nsSeparator'>, $Dictionary {
|
|
1183
|
+
ns?: Ns;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
export type SelectorReturn<
|
|
1187
|
+
Target,
|
|
1188
|
+
Opts extends { defaultValue?: unknown; returnObjects?: boolean },
|
|
1189
|
+
DV = Opts['defaultValue'],
|
|
1190
|
+
> = $IsResourcesDefined extends true
|
|
1191
|
+
? TFunctionReturnOptionalDetails<ProcessReturnValue<DeepUnwrapPlural<Target>, DV>, Opts>
|
|
1192
|
+
: DefaultTReturn<Opts>;
|
|
1193
|
+
|
|
1194
|
+
/** Public for react-i18next's `Trans` types (since v26.7.0). */
|
|
1195
|
+
export interface SelectorFn<Source, Target, Opts extends SelectorOptions<unknown>> {
|
|
1196
|
+
(translations: Select<Source, Opts['context']>): Target;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
export type ApplyKeyPrefix<
|
|
1200
|
+
T extends [any],
|
|
1201
|
+
KPrefix,
|
|
1202
|
+
> = KPrefix extends `${infer Head}${_KeyPrefixSeparator}${infer Tail}`
|
|
1203
|
+
? ApplyKeyPrefix<[T[0][Head]], Tail>
|
|
1204
|
+
: T[0][KPrefix & string];
|
|
1205
|
+
|
|
1206
|
+
/** Public for react-i18next's `Trans` types (since v26.7.0). */
|
|
1207
|
+
export type ApplyTarget<
|
|
1208
|
+
Target,
|
|
1209
|
+
Opts extends { returnObjects?: unknown },
|
|
1210
|
+
> = Opts['returnObjects'] extends true ? unknown : Target;
|
|
1211
|
+
|
|
1212
|
+
/** Public for react-i18next's `Trans` types (since v26.7.0). */
|
|
1213
|
+
export type ConstrainTarget<Opts extends SelectorOptions<any>> = _ReturnObjects extends true
|
|
1214
|
+
? unknown
|
|
1215
|
+
: Opts['returnObjects'] extends true
|
|
1216
|
+
? unknown
|
|
1217
|
+
: $IsResourcesDefined extends false
|
|
1218
|
+
? unknown
|
|
1219
|
+
: string;
|
|
1220
|
+
|
|
1221
|
+
export type ProcessReturnValue<Target, DefaultValue> = $Turtles extends Target
|
|
1222
|
+
? string
|
|
1223
|
+
: [DefaultValue] extends [never]
|
|
1224
|
+
? Target
|
|
1225
|
+
: unknown extends DefaultValue
|
|
1226
|
+
? Target
|
|
1227
|
+
: Target | DefaultValue;
|
|
1228
|
+
|
|
1229
|
+
export type PickNamespaces<T, K extends keyof any> = {
|
|
1230
|
+
[P in K as P extends keyof T ? P : never]: T[P & keyof T];
|
|
1231
|
+
};
|
|
1232
|
+
|
|
1233
|
+
/** Extracts the sub-tree returned by a selector function used as keyPrefix. */
|
|
1234
|
+
export type SelectorReturnSource<KPrefix, Fallback> = KPrefix extends (...args: any[]) => infer R
|
|
1235
|
+
? R extends object
|
|
1236
|
+
? R
|
|
1237
|
+
: Fallback
|
|
1238
|
+
: Fallback;
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* No key prefix: `undefined`, or a union with `undefined`. Without strictNullChecks
|
|
1242
|
+
* `undefined extends 'a'` is true, so there only `undefined` itself counts.
|
|
1243
|
+
*/
|
|
1244
|
+
type _NoKeyPrefix<KPrefix> = _NoStrictNullChecks extends true
|
|
1245
|
+
? [KPrefix] extends [undefined]
|
|
1246
|
+
? true
|
|
1247
|
+
: false
|
|
1248
|
+
: undefined extends KPrefix
|
|
1249
|
+
? true
|
|
1250
|
+
: false;
|
|
1251
|
+
|
|
1252
|
+
/** Public for react-i18next's `Trans` types (since v26.7.0). */
|
|
1253
|
+
export type GetSource<Ns extends Namespace, KPrefix, Res = NsResource<Ns>> = KPrefix extends (
|
|
1254
|
+
...args: any[]
|
|
1255
|
+
) => any
|
|
1256
|
+
? SelectorReturnSource<KPrefix, Res>
|
|
1257
|
+
: KPrefix extends keyof Res
|
|
1258
|
+
? Res[KPrefix]
|
|
1259
|
+
: _NoKeyPrefix<KPrefix> extends true
|
|
1260
|
+
? Res
|
|
1261
|
+
: ApplyKeyPrefix<[Res], KPrefix>;
|
|
1262
|
+
|
|
1263
|
+
export type Select<T, Context> = $IsResourcesDefined extends false
|
|
1264
|
+
? $Turtles
|
|
1265
|
+
: [_EnableSelector] extends ['optimize']
|
|
1266
|
+
? T
|
|
1267
|
+
: FilterKeys<T, Context>;
|
|
1268
|
+
|
|
1269
|
+
/**
|
|
1270
|
+
* Context-variant keys that are actually present (not JSON-union phantoms typed
|
|
1271
|
+
* as optional `undefined`, e.g. `transKey1_withContext?: undefined`).
|
|
1272
|
+
*/
|
|
1273
|
+
type _DefinedContextKeys<T, Pattern extends string> = {
|
|
1274
|
+
[P in keyof T as P extends Pattern ? ([T[P]] extends [undefined] ? never : P) : never]: true;
|
|
1275
|
+
};
|
|
1276
|
+
|
|
1277
|
+
/**
|
|
1278
|
+
* Keys of T whose value is not `undefined` (JSON-union phantoms are typed as
|
|
1279
|
+
* optional `undefined`), built once per object.
|
|
1280
|
+
*/
|
|
1281
|
+
type _DefinedKeys<T> = [T] extends [unknown]
|
|
1282
|
+
? keyof { [P in keyof T as [T[P]] extends [undefined] ? never : P]: true }
|
|
1283
|
+
: never;
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* For every defined key, the prefixes K before each context separator, as
|
|
1287
|
+
* `[all, bases]`: `all` has every K with a defined key `${K}${_ContextSeparator}...`,
|
|
1288
|
+
* `bases` only those where the rest is not just a plural suffix. Jumps from one
|
|
1289
|
+
* separator occurrence to the next (so the recursion depth is the number of
|
|
1290
|
+
* separators, not the key length, which can be a whole sentence) and resumes one
|
|
1291
|
+
* character after the match, so overlapping occurrences (`a___b` with `__`) count.
|
|
1292
|
+
*/
|
|
1293
|
+
type _ContextPrefixesOf<
|
|
1294
|
+
P,
|
|
1295
|
+
Head extends string = '',
|
|
1296
|
+
All = never,
|
|
1297
|
+
Bases = never,
|
|
1298
|
+
> = P extends `${infer B}${_ContextSeparator}${string}`
|
|
1299
|
+
? P extends `${B}${infer FromSep}`
|
|
1300
|
+
? FromSep extends `${infer C}${infer Rest}`
|
|
1301
|
+
? _ContextPrefixesOf<
|
|
1302
|
+
Rest,
|
|
1303
|
+
`${Head}${B}${C}`,
|
|
1304
|
+
All | `${Head}${B}`,
|
|
1305
|
+
FromSep extends
|
|
1306
|
+
| `${_PluralSeparator}${PluralSuffix}`
|
|
1307
|
+
| `${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
1308
|
+
? Bases
|
|
1309
|
+
: Bases | `${Head}${B}`
|
|
1310
|
+
>
|
|
1311
|
+
: [All, Bases]
|
|
1312
|
+
: [All, Bases]
|
|
1313
|
+
: [All, Bases];
|
|
1314
|
+
|
|
1315
|
+
/**
|
|
1316
|
+
* The context prefixes of an object, built once per object (conditional type
|
|
1317
|
+
* instantiations are cached): scanning all keys for every key was quadratic
|
|
1318
|
+
* in the object size, TS2589 at about 1,000 keys with context variants.
|
|
1319
|
+
*/
|
|
1320
|
+
type _ContextPrefixes<T> = [T] extends [unknown]
|
|
1321
|
+
? _ContextPrefixesOf<_DefinedKeys<T> & string>
|
|
1322
|
+
: never;
|
|
1323
|
+
type _AllContextPrefixes<T> = [T] extends [unknown] ? _ContextPrefixes<T>[0] : never;
|
|
1324
|
+
type _ContextBases<T> = [T] extends [unknown] ? _ContextPrefixes<T>[1] : never;
|
|
1325
|
+
|
|
1326
|
+
/** Whether a string type is a single literal (not `string` or a `${string}` pattern). */
|
|
1327
|
+
type _IsLiteral<S extends PropertyKey> = {} extends Record<S, unknown> ? false : true;
|
|
1328
|
+
|
|
1329
|
+
/**
|
|
1330
|
+
* Whether T has a defined key matching Pattern: literal keys are looked up, a
|
|
1331
|
+
* pattern with a wide member scans the keys.
|
|
1332
|
+
*/
|
|
1333
|
+
type _HasDefinedKey<T, Pattern extends string> = [
|
|
1334
|
+
false extends (Pattern extends unknown ? _IsLiteral<Pattern> : never)
|
|
1335
|
+
? keyof _DefinedContextKeys<T, Pattern>
|
|
1336
|
+
: Extract<Pattern, _DefinedKeys<T>>,
|
|
1337
|
+
] extends [never]
|
|
1338
|
+
? false
|
|
1339
|
+
: true;
|
|
1340
|
+
|
|
1341
|
+
type _HasContextVariant<T, K extends string, Context> = string extends Context
|
|
1342
|
+
? _IsLiteral<K> extends true
|
|
1343
|
+
? K extends _AllContextPrefixes<T>
|
|
1344
|
+
? true
|
|
1345
|
+
: false
|
|
1346
|
+
: _HasDefinedKey<T, `${K}${_ContextSeparator}${string}`>
|
|
1347
|
+
: _HasDefinedKey<
|
|
1348
|
+
T,
|
|
1349
|
+
| `${K}${_ContextSeparator}${Context & string}`
|
|
1350
|
+
| `${K}${_ContextSeparator}${Context & string}${_PluralSeparator}${PluralSuffix}`
|
|
1351
|
+
>;
|
|
1352
|
+
|
|
1353
|
+
/** Checks whether key K has **any** context variant in T (excluding pure plural suffixes). */
|
|
1354
|
+
type _IsContextualKey<T, K extends string> =
|
|
1355
|
+
_IsLiteral<K> extends true
|
|
1356
|
+
? K extends _ContextBases<T>
|
|
1357
|
+
? true
|
|
1358
|
+
: false
|
|
1359
|
+
: [
|
|
1360
|
+
Exclude<
|
|
1361
|
+
keyof _DefinedContextKeys<T, `${K}${_ContextSeparator}${string}`>,
|
|
1362
|
+
| `${K}${_PluralSeparator}${PluralSuffix}`
|
|
1363
|
+
| `${K}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
1364
|
+
>,
|
|
1365
|
+
] extends [never]
|
|
1366
|
+
? false
|
|
1367
|
+
: true;
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* Distributes {@link FilterKeysObject} over unions so heterogeneous JSON array
|
|
1371
|
+
* element types (each object literal in the array) are filtered independently,
|
|
1372
|
+
* then re-unified. Without distribution, `keyof (A | B)` is only the common keys
|
|
1373
|
+
* and context variants that exist on a single element are invisible, which
|
|
1374
|
+
* produced partial element unions for `returnObjects` + `context` (#2398).
|
|
1375
|
+
*
|
|
1376
|
+
* The array check itself must distribute too (`T extends`, not `[T] extends`):
|
|
1377
|
+
* comparing two `TFunction`s over a generic namespace otherwise relates the
|
|
1378
|
+
* array branch through `keyof (readonly any[] & Source)`, whose constraint is
|
|
1379
|
+
* the intersection of every namespace's keys, a cross product that fails with
|
|
1380
|
+
* TS2590 once there are many wide namespaces.
|
|
1381
|
+
*/
|
|
1382
|
+
export type FilterKeys<T, Context> = T extends readonly any[]
|
|
1383
|
+
? 0 extends 1 & T
|
|
1384
|
+
? FilterKeysObject<T, Context> // `any` reaches both branches; keep one result
|
|
1385
|
+
: { [I in keyof T]: FilterKeys<T[I], Context> }
|
|
1386
|
+
: FilterKeysObject<T, Context>;
|
|
1387
|
+
|
|
1388
|
+
export type FilterKeysObject<T, Context> = {
|
|
1389
|
+
// Mapped type 1: object-valued keys (recurse) + plain leaf keys (non-plural, non-context)
|
|
1390
|
+
[
|
|
1391
|
+
K in keyof T as [keyof T[K]] extends [never]
|
|
1392
|
+
? never // nothing to select (empty object, null, undefined, optional or unknown)
|
|
1393
|
+
: T[K] extends object
|
|
1394
|
+
? K
|
|
1395
|
+
: [Context] extends [string]
|
|
1396
|
+
? K extends
|
|
1397
|
+
| `${string}${_ContextSeparator}${Context}`
|
|
1398
|
+
| `${string}${_ContextSeparator}${Context}${_PluralSeparator}${PluralSuffix}`
|
|
1399
|
+
? never // context keys handled by mapped type 3
|
|
1400
|
+
: K extends `${string}${_PluralSeparator}${PluralSuffix}`
|
|
1401
|
+
? never // plural keys handled by mapped type 2
|
|
1402
|
+
: K extends string
|
|
1403
|
+
? _HasContextVariant<T, K, Context> extends true
|
|
1404
|
+
? never // context variant exists, drop base key (type 3 handles it)
|
|
1405
|
+
: _IsContextualKey<T, K> extends true
|
|
1406
|
+
? never // key has context variants but not for this context
|
|
1407
|
+
: K // no context variants at all, keep base key
|
|
1408
|
+
: K
|
|
1409
|
+
: K extends `${string}${_PluralSeparator}${PluralSuffix}`
|
|
1410
|
+
? never
|
|
1411
|
+
: K
|
|
1412
|
+
]: T[K] extends object ? FilterKeys<T[K], Context> : T[K];
|
|
1413
|
+
} & {
|
|
1414
|
+
// Mapped type 2: plural collapsing (active regardless of context)
|
|
1415
|
+
[
|
|
1416
|
+
K in keyof T as T[K] extends object
|
|
1417
|
+
? never
|
|
1418
|
+
: [Context] extends [string]
|
|
1419
|
+
? K extends
|
|
1420
|
+
| `${string}${_ContextSeparator}${Context}`
|
|
1421
|
+
| `${string}${_ContextSeparator}${Context}${_PluralSeparator}${PluralSuffix}`
|
|
1422
|
+
? never // context keys handled by mapped type 3
|
|
1423
|
+
: K extends
|
|
1424
|
+
| `${infer Prefix}${_PluralSeparator}${PluralSuffix}`
|
|
1425
|
+
| `${infer Prefix}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
1426
|
+
? Prefix
|
|
1427
|
+
: never
|
|
1428
|
+
: K extends
|
|
1429
|
+
| `${infer Prefix}${_PluralSeparator}${PluralSuffix}`
|
|
1430
|
+
| `${infer Prefix}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
1431
|
+
? Prefix
|
|
1432
|
+
: never
|
|
1433
|
+
]: T[K] extends object ? FilterKeys<T[K], Context> : PluralValue<T[K] & string>;
|
|
1434
|
+
} & {
|
|
1435
|
+
// Mapped type 3: context key collapsing (skip JSON-union phantoms typed as `undefined`)
|
|
1436
|
+
[
|
|
1437
|
+
K in keyof T as T[K] extends object
|
|
1438
|
+
? never
|
|
1439
|
+
: [keyof T[K]] extends [never]
|
|
1440
|
+
? never
|
|
1441
|
+
: [Context] extends [string]
|
|
1442
|
+
? K extends
|
|
1443
|
+
| `${infer Prefix}${_ContextSeparator}${Context}`
|
|
1444
|
+
| `${infer Prefix}${_ContextSeparator}${Context}${_PluralSeparator}${PluralSuffix}`
|
|
1445
|
+
? Prefix
|
|
1446
|
+
: never
|
|
1447
|
+
: never
|
|
1448
|
+
]: T[K] extends object ? FilterKeys<T[K], Context> : T[K];
|
|
1449
|
+
};
|