@tldraw/tlschema 4.1.0-canary.e2133d922c9e → 4.1.0-canary.e23ee15a46bc
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/dist-cjs/TLStore.js +3 -10
- package/dist-cjs/TLStore.js.map +2 -2
- package/dist-cjs/assets/TLBaseAsset.js.map +2 -2
- package/dist-cjs/assets/TLBookmarkAsset.js.map +2 -2
- package/dist-cjs/assets/TLImageAsset.js.map +2 -2
- package/dist-cjs/assets/TLVideoAsset.js.map +2 -2
- package/dist-cjs/bindings/TLArrowBinding.js.map +2 -2
- package/dist-cjs/bindings/TLBaseBinding.js.map +2 -2
- package/dist-cjs/createPresenceStateDerivation.js.map +2 -2
- package/dist-cjs/createTLSchema.js.map +2 -2
- package/dist-cjs/index.d.ts +4412 -223
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/misc/TLColor.js.map +2 -2
- package/dist-cjs/misc/TLCursor.js.map +2 -2
- package/dist-cjs/misc/TLHandle.js.map +2 -2
- package/dist-cjs/misc/TLOpacity.js.map +2 -2
- package/dist-cjs/misc/TLRichText.js.map +2 -2
- package/dist-cjs/misc/TLScribble.js.map +2 -2
- package/dist-cjs/misc/geometry-types.js.map +2 -2
- package/dist-cjs/misc/id-validator.js.map +2 -2
- package/dist-cjs/records/TLAsset.js.map +2 -2
- package/dist-cjs/records/TLBinding.js.map +2 -2
- package/dist-cjs/records/TLCamera.js.map +2 -2
- package/dist-cjs/records/TLDocument.js.map +2 -2
- package/dist-cjs/records/TLInstance.js.map +2 -2
- package/dist-cjs/records/TLPage.js.map +2 -2
- package/dist-cjs/records/TLPageState.js.map +2 -2
- package/dist-cjs/records/TLPointer.js.map +2 -2
- package/dist-cjs/records/TLPresence.js.map +2 -2
- package/dist-cjs/records/TLRecord.js.map +1 -1
- package/dist-cjs/records/TLShape.js.map +2 -2
- package/dist-cjs/recordsWithProps.js.map +2 -2
- package/dist-cjs/shapes/ShapeWithCrop.js.map +1 -1
- package/dist-cjs/shapes/TLArrowShape.js.map +2 -2
- package/dist-cjs/shapes/TLBaseShape.js.map +2 -2
- package/dist-cjs/shapes/TLBookmarkShape.js.map +2 -2
- package/dist-cjs/shapes/TLDrawShape.js.map +2 -2
- package/dist-cjs/shapes/TLEmbedShape.js.map +2 -2
- package/dist-cjs/shapes/TLFrameShape.js.map +2 -2
- package/dist-cjs/shapes/TLGeoShape.js.map +2 -2
- package/dist-cjs/shapes/TLGroupShape.js.map +2 -2
- package/dist-cjs/shapes/TLHighlightShape.js.map +2 -2
- package/dist-cjs/shapes/TLImageShape.js.map +2 -2
- package/dist-cjs/shapes/TLLineShape.js.map +2 -2
- package/dist-cjs/shapes/TLNoteShape.js.map +2 -2
- package/dist-cjs/shapes/TLTextShape.js.map +2 -2
- package/dist-cjs/shapes/TLVideoShape.js.map +2 -2
- package/dist-cjs/store-migrations.js.map +2 -2
- package/dist-cjs/styles/TLColorStyle.js.map +2 -2
- package/dist-cjs/styles/TLDashStyle.js.map +2 -2
- package/dist-cjs/styles/TLFillStyle.js.map +2 -2
- package/dist-cjs/styles/TLFontStyle.js.map +2 -2
- package/dist-cjs/styles/TLHorizontalAlignStyle.js.map +2 -2
- package/dist-cjs/styles/TLSizeStyle.js.map +2 -2
- package/dist-cjs/styles/TLTextAlignStyle.js.map +2 -2
- package/dist-cjs/styles/TLVerticalAlignStyle.js.map +2 -2
- package/dist-cjs/translations/translations.js +1 -1
- package/dist-cjs/translations/translations.js.map +2 -2
- package/dist-cjs/util-types.js.map +1 -1
- package/dist-esm/TLStore.mjs +3 -10
- package/dist-esm/TLStore.mjs.map +2 -2
- package/dist-esm/assets/TLBaseAsset.mjs.map +2 -2
- package/dist-esm/assets/TLBookmarkAsset.mjs.map +2 -2
- package/dist-esm/assets/TLImageAsset.mjs.map +2 -2
- package/dist-esm/assets/TLVideoAsset.mjs.map +2 -2
- package/dist-esm/bindings/TLArrowBinding.mjs.map +2 -2
- package/dist-esm/bindings/TLBaseBinding.mjs.map +2 -2
- package/dist-esm/createPresenceStateDerivation.mjs.map +2 -2
- package/dist-esm/createTLSchema.mjs.map +2 -2
- package/dist-esm/index.d.mts +4412 -223
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/misc/TLColor.mjs.map +2 -2
- package/dist-esm/misc/TLCursor.mjs.map +2 -2
- package/dist-esm/misc/TLHandle.mjs.map +2 -2
- package/dist-esm/misc/TLOpacity.mjs.map +2 -2
- package/dist-esm/misc/TLRichText.mjs.map +2 -2
- package/dist-esm/misc/TLScribble.mjs.map +2 -2
- package/dist-esm/misc/geometry-types.mjs.map +2 -2
- package/dist-esm/misc/id-validator.mjs.map +2 -2
- package/dist-esm/records/TLAsset.mjs.map +2 -2
- package/dist-esm/records/TLBinding.mjs.map +2 -2
- package/dist-esm/records/TLCamera.mjs.map +2 -2
- package/dist-esm/records/TLDocument.mjs.map +2 -2
- package/dist-esm/records/TLInstance.mjs.map +2 -2
- package/dist-esm/records/TLPage.mjs.map +2 -2
- package/dist-esm/records/TLPageState.mjs.map +2 -2
- package/dist-esm/records/TLPointer.mjs.map +2 -2
- package/dist-esm/records/TLPresence.mjs.map +2 -2
- package/dist-esm/records/TLShape.mjs.map +2 -2
- package/dist-esm/recordsWithProps.mjs.map +2 -2
- package/dist-esm/shapes/TLArrowShape.mjs.map +2 -2
- package/dist-esm/shapes/TLBaseShape.mjs.map +2 -2
- package/dist-esm/shapes/TLBookmarkShape.mjs.map +2 -2
- package/dist-esm/shapes/TLDrawShape.mjs.map +2 -2
- package/dist-esm/shapes/TLEmbedShape.mjs.map +2 -2
- package/dist-esm/shapes/TLFrameShape.mjs.map +2 -2
- package/dist-esm/shapes/TLGeoShape.mjs.map +2 -2
- package/dist-esm/shapes/TLGroupShape.mjs.map +2 -2
- package/dist-esm/shapes/TLHighlightShape.mjs.map +2 -2
- package/dist-esm/shapes/TLImageShape.mjs.map +2 -2
- package/dist-esm/shapes/TLLineShape.mjs.map +2 -2
- package/dist-esm/shapes/TLNoteShape.mjs.map +2 -2
- package/dist-esm/shapes/TLTextShape.mjs.map +2 -2
- package/dist-esm/shapes/TLVideoShape.mjs.map +2 -2
- package/dist-esm/store-migrations.mjs.map +2 -2
- package/dist-esm/styles/TLColorStyle.mjs.map +2 -2
- package/dist-esm/styles/TLDashStyle.mjs.map +2 -2
- package/dist-esm/styles/TLFillStyle.mjs.map +2 -2
- package/dist-esm/styles/TLFontStyle.mjs.map +2 -2
- package/dist-esm/styles/TLHorizontalAlignStyle.mjs.map +2 -2
- package/dist-esm/styles/TLSizeStyle.mjs.map +2 -2
- package/dist-esm/styles/TLTextAlignStyle.mjs.map +2 -2
- package/dist-esm/styles/TLVerticalAlignStyle.mjs.map +2 -2
- package/dist-esm/translations/translations.mjs +1 -1
- package/dist-esm/translations/translations.mjs.map +2 -2
- package/package.json +5 -5
- package/src/TLStore.test.ts +644 -0
- package/src/TLStore.ts +205 -20
- package/src/assets/TLBaseAsset.ts +90 -7
- package/src/assets/TLBookmarkAsset.test.ts +96 -0
- package/src/assets/TLBookmarkAsset.ts +52 -2
- package/src/assets/TLImageAsset.test.ts +213 -0
- package/src/assets/TLImageAsset.ts +60 -2
- package/src/assets/TLVideoAsset.test.ts +105 -0
- package/src/assets/TLVideoAsset.ts +93 -4
- package/src/bindings/TLArrowBinding.test.ts +55 -0
- package/src/bindings/TLArrowBinding.ts +132 -10
- package/src/bindings/TLBaseBinding.ts +140 -3
- package/src/createPresenceStateDerivation.test.ts +158 -0
- package/src/createPresenceStateDerivation.ts +71 -2
- package/src/createTLSchema.test.ts +181 -0
- package/src/createTLSchema.ts +164 -7
- package/src/index.ts +32 -0
- package/src/misc/TLColor.ts +50 -6
- package/src/misc/TLCursor.ts +110 -8
- package/src/misc/TLHandle.ts +82 -6
- package/src/misc/TLOpacity.ts +51 -2
- package/src/misc/TLRichText.ts +56 -3
- package/src/misc/TLScribble.ts +105 -5
- package/src/misc/geometry-types.ts +30 -2
- package/src/misc/id-validator.test.ts +50 -0
- package/src/misc/id-validator.ts +20 -1
- package/src/records/TLAsset.test.ts +234 -0
- package/src/records/TLAsset.ts +165 -8
- package/src/records/TLBinding.test.ts +22 -0
- package/src/records/TLBinding.ts +277 -11
- package/src/records/TLCamera.test.ts +19 -0
- package/src/records/TLCamera.ts +118 -7
- package/src/records/TLDocument.test.ts +35 -0
- package/src/records/TLDocument.ts +148 -8
- package/src/records/TLInstance.test.ts +201 -0
- package/src/records/TLInstance.ts +117 -9
- package/src/records/TLPage.test.ts +110 -0
- package/src/records/TLPage.ts +106 -8
- package/src/records/TLPageState.test.ts +228 -0
- package/src/records/TLPageState.ts +88 -7
- package/src/records/TLPointer.test.ts +63 -0
- package/src/records/TLPointer.ts +105 -7
- package/src/records/TLPresence.test.ts +190 -0
- package/src/records/TLPresence.ts +99 -5
- package/src/records/TLRecord.test.ts +70 -0
- package/src/records/TLRecord.ts +43 -1
- package/src/records/TLShape.test.ts +232 -0
- package/src/records/TLShape.ts +289 -12
- package/src/recordsWithProps.test.ts +188 -0
- package/src/recordsWithProps.ts +131 -2
- package/src/shapes/ShapeWithCrop.test.ts +18 -0
- package/src/shapes/ShapeWithCrop.ts +64 -2
- package/src/shapes/TLArrowShape.test.ts +505 -0
- package/src/shapes/TLArrowShape.ts +188 -10
- package/src/shapes/TLBaseShape.test.ts +142 -0
- package/src/shapes/TLBaseShape.ts +103 -4
- package/src/shapes/TLBookmarkShape.test.ts +122 -0
- package/src/shapes/TLBookmarkShape.ts +58 -4
- package/src/shapes/TLDrawShape.test.ts +177 -0
- package/src/shapes/TLDrawShape.ts +97 -6
- package/src/shapes/TLEmbedShape.test.ts +286 -0
- package/src/shapes/TLEmbedShape.ts +57 -4
- package/src/shapes/TLFrameShape.test.ts +71 -0
- package/src/shapes/TLFrameShape.ts +59 -4
- package/src/shapes/TLGeoShape.test.ts +247 -0
- package/src/shapes/TLGeoShape.ts +103 -7
- package/src/shapes/TLGroupShape.test.ts +59 -0
- package/src/shapes/TLGroupShape.ts +52 -4
- package/src/shapes/TLHighlightShape.test.ts +325 -0
- package/src/shapes/TLHighlightShape.ts +79 -4
- package/src/shapes/TLImageShape.test.ts +534 -0
- package/src/shapes/TLImageShape.ts +105 -5
- package/src/shapes/TLLineShape.test.ts +269 -0
- package/src/shapes/TLLineShape.ts +128 -8
- package/src/shapes/TLNoteShape.test.ts +1568 -0
- package/src/shapes/TLNoteShape.ts +97 -4
- package/src/shapes/TLTextShape.test.ts +407 -0
- package/src/shapes/TLTextShape.ts +94 -4
- package/src/shapes/TLVideoShape.test.ts +112 -0
- package/src/shapes/TLVideoShape.ts +99 -4
- package/src/store-migrations.test.ts +88 -0
- package/src/store-migrations.ts +47 -1
- package/src/styles/TLColorStyle.test.ts +439 -0
- package/src/styles/TLColorStyle.ts +228 -10
- package/src/styles/TLDashStyle.ts +54 -2
- package/src/styles/TLFillStyle.ts +54 -2
- package/src/styles/TLFontStyle.ts +72 -3
- package/src/styles/TLHorizontalAlignStyle.ts +55 -2
- package/src/styles/TLSizeStyle.ts +54 -2
- package/src/styles/TLTextAlignStyle.ts +52 -2
- package/src/styles/TLVerticalAlignStyle.ts +52 -2
- package/src/translations/translations.test.ts +378 -35
- package/src/translations/translations.ts +157 -10
- package/src/util-types.ts +51 -1
|
@@ -3,20 +3,102 @@ import { LANGUAGES } from './languages'
|
|
|
3
3
|
/** @public */
|
|
4
4
|
export { LANGUAGES }
|
|
5
5
|
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* A language definition object representing a supported localization in tldraw.
|
|
8
|
+
*
|
|
9
|
+
* Derived from the LANGUAGES array, this type represents a single language entry
|
|
10
|
+
* containing a locale identifier and human-readable label. The locale follows
|
|
11
|
+
* BCP 47 standards (e.g., 'en', 'fr', 'zh-CN') and the label is in the native language.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { TLLanguage } from '@tldraw/tlschema'
|
|
16
|
+
*
|
|
17
|
+
* // Using TLLanguage type
|
|
18
|
+
* const currentLanguage: TLLanguage = { locale: 'fr', label: 'Français' }
|
|
19
|
+
*
|
|
20
|
+
* // Access locale and label
|
|
21
|
+
* console.log(currentLanguage.locale) // "fr"
|
|
22
|
+
* console.log(currentLanguage.label) // "Français"
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
7
27
|
export type TLLanguage = (typeof LANGUAGES)[number]
|
|
8
|
-
// {
|
|
9
|
-
// readonly locale: string
|
|
10
|
-
// readonly label: string
|
|
11
|
-
// }
|
|
12
28
|
|
|
13
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Gets the default translation locale based on the user's browser language preferences.
|
|
31
|
+
*
|
|
32
|
+
* This function determines the best matching locale from the user's browser language
|
|
33
|
+
* settings, falling back to English if no suitable match is found. It works in both
|
|
34
|
+
* browser and server-side environments, defaulting to English on the server.
|
|
35
|
+
*
|
|
36
|
+
* The function prioritizes exact matches first, then falls back to language-only
|
|
37
|
+
* matches, and finally uses predefined regional defaults for languages like Chinese,
|
|
38
|
+
* Portuguese, Korean, and Hindi.
|
|
39
|
+
*
|
|
40
|
+
* @returns The locale identifier (e.g., 'en', 'fr', 'zh-cn') that best matches the user's preferences
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { getDefaultTranslationLocale } from '@tldraw/tlschema'
|
|
45
|
+
*
|
|
46
|
+
* // Get the user's preferred locale
|
|
47
|
+
* const locale = getDefaultTranslationLocale()
|
|
48
|
+
* console.log(locale) // e.g., "fr" or "en" or "zh-cn"
|
|
49
|
+
*
|
|
50
|
+
* // Use in localization setup
|
|
51
|
+
* const i18n = new I18n({
|
|
52
|
+
* locale,
|
|
53
|
+
* // ... other config
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* // Browser with languages: ['fr-CA', 'en-US']
|
|
60
|
+
* const locale = getDefaultTranslationLocale()
|
|
61
|
+
* console.log(locale) // "fr" (if French is supported)
|
|
62
|
+
*
|
|
63
|
+
* // Browser with languages: ['zh']
|
|
64
|
+
* const locale = getDefaultTranslationLocale()
|
|
65
|
+
* console.log(locale) // "zh-cn" (default region for Chinese)
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
14
70
|
export function getDefaultTranslationLocale(): TLLanguage['locale'] {
|
|
15
|
-
const locales =
|
|
71
|
+
const locales =
|
|
72
|
+
typeof window !== 'undefined' && window.navigator
|
|
73
|
+
? (window.navigator.languages ?? ['en'])
|
|
74
|
+
: ['en']
|
|
16
75
|
return _getDefaultTranslationLocale(locales)
|
|
17
76
|
}
|
|
18
77
|
|
|
19
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Internal function that determines the default translation locale from a list of locale preferences.
|
|
80
|
+
*
|
|
81
|
+
* This function is the core logic for locale resolution, separated from browser-specific code
|
|
82
|
+
* for easier testing and reuse. It iterates through the provided locales in priority order
|
|
83
|
+
* and returns the first supported locale found, or 'en' as the ultimate fallback.
|
|
84
|
+
*
|
|
85
|
+
* @param locales - Array of locale identifiers in preference order (e.g., from navigator.languages)
|
|
86
|
+
* @returns The best matching supported locale identifier
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
*
|
|
91
|
+
* // Test locale resolution
|
|
92
|
+
* const locale = _getDefaultTranslationLocale(['fr-CA', 'en-US', 'es'])
|
|
93
|
+
* console.log(locale) // "fr" (if French is supported)
|
|
94
|
+
*
|
|
95
|
+
* // No supported locales
|
|
96
|
+
* const fallback = _getDefaultTranslationLocale(['xx-YY', 'zz-AA'])
|
|
97
|
+
* console.log(fallback) // "en"
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
20
102
|
export function _getDefaultTranslationLocale(locales: readonly string[]): TLLanguage['locale'] {
|
|
21
103
|
for (const locale of locales) {
|
|
22
104
|
const supportedLocale = getSupportedLocale(locale)
|
|
@@ -27,7 +109,28 @@ export function _getDefaultTranslationLocale(locales: readonly string[]): TLLang
|
|
|
27
109
|
return 'en'
|
|
28
110
|
}
|
|
29
111
|
|
|
30
|
-
/**
|
|
112
|
+
/**
|
|
113
|
+
* Default regional variants for languages that have multiple regional versions.
|
|
114
|
+
*
|
|
115
|
+
* When a user's locale contains only a language code (e.g., 'zh', 'pt') but tldraw
|
|
116
|
+
* only supports region-specific variants, this mapping determines which regional
|
|
117
|
+
* variant to use as the default. This ensures users get the most appropriate
|
|
118
|
+
* localization even when their preference doesn't specify a region.
|
|
119
|
+
*
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* // User has locale preference "zh" but we only support "zh-cn" and "zh-tw"
|
|
124
|
+
* const defaultRegion = DEFAULT_LOCALE_REGIONS['zh']
|
|
125
|
+
* console.log(defaultRegion) // "zh-cn"
|
|
126
|
+
*
|
|
127
|
+
* // User has locale preference "pt" but we support "pt-br" and "pt-pt"
|
|
128
|
+
* const defaultRegion = DEFAULT_LOCALE_REGIONS['pt']
|
|
129
|
+
* console.log(defaultRegion) // "pt-br"
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
31
134
|
const DEFAULT_LOCALE_REGIONS: { [locale: string]: TLLanguage['locale'] } = {
|
|
32
135
|
zh: 'zh-cn',
|
|
33
136
|
pt: 'pt-br',
|
|
@@ -35,7 +138,51 @@ const DEFAULT_LOCALE_REGIONS: { [locale: string]: TLLanguage['locale'] } = {
|
|
|
35
138
|
hi: 'hi-in',
|
|
36
139
|
}
|
|
37
140
|
|
|
38
|
-
/**
|
|
141
|
+
/**
|
|
142
|
+
* Finds a supported locale that matches the given locale identifier.
|
|
143
|
+
*
|
|
144
|
+
* This function implements a flexible locale matching algorithm that tries multiple
|
|
145
|
+
* strategies to find the best available translation:
|
|
146
|
+
*
|
|
147
|
+
* 1. **Exact match**: Looks for an exact locale match (case-insensitive)
|
|
148
|
+
* 2. **Language-only match**: If the input has a region, tries matching just the language
|
|
149
|
+
* 3. **Default region**: If the input lacks a region, uses the default region for that language
|
|
150
|
+
* 4. **No match**: Returns null if no suitable locale is found
|
|
151
|
+
*
|
|
152
|
+
* @param locale - The locale identifier to match (e.g., 'fr-CA', 'pt', 'zh-TW')
|
|
153
|
+
* @returns The matching supported locale identifier, or null if no match is found
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* // Exact matches
|
|
158
|
+
* getSupportedLocale('fr') // "fr" (if supported)
|
|
159
|
+
* getSupportedLocale('PT-BR') // "pt-br" (case insensitive)
|
|
160
|
+
*
|
|
161
|
+
* // Language-only fallback
|
|
162
|
+
* getSupportedLocale('fr-CA') // "fr" (if we only support generic French)
|
|
163
|
+
*
|
|
164
|
+
* // Default region assignment
|
|
165
|
+
* getSupportedLocale('zh') // "zh-cn" (default Chinese region)
|
|
166
|
+
*
|
|
167
|
+
* // No match
|
|
168
|
+
* getSupportedLocale('xyz') // null
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* // Usage in locale resolution
|
|
174
|
+
* const userLocales = ['es-MX', 'en-US']
|
|
175
|
+
* for (const userLocale of userLocales) {
|
|
176
|
+
* const supported = getSupportedLocale(userLocale)
|
|
177
|
+
* if (supported) {
|
|
178
|
+
* console.log(`Using locale: ${supported}`)
|
|
179
|
+
* break
|
|
180
|
+
* }
|
|
181
|
+
* }
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* @public
|
|
185
|
+
*/
|
|
39
186
|
function getSupportedLocale(locale: string): TLLanguage['locale'] | null {
|
|
40
187
|
// If we have an exact match, return it!
|
|
41
188
|
// (e.g. if the user has 'fr' and we have 'fr')
|
package/src/util-types.ts
CHANGED
|
@@ -1,2 +1,52 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Utility type that extracts the value type from a Set type.
|
|
3
|
+
*
|
|
4
|
+
* This helper type uses conditional type inference to extract the element type
|
|
5
|
+
* from a Set. It's useful when working with sets in type definitions where you
|
|
6
|
+
* need to reference the type of elements contained within the set.
|
|
7
|
+
*
|
|
8
|
+
* The type uses TypeScript's `infer` keyword to capture the generic parameter
|
|
9
|
+
* of the Set type, making it reusable across different Set types.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { SetValue } from '@tldraw/tlschema'
|
|
14
|
+
*
|
|
15
|
+
* // Extract value type from a Set type
|
|
16
|
+
* type StringSet = Set<string>
|
|
17
|
+
* type StringValue = SetValue<StringSet> // string
|
|
18
|
+
*
|
|
19
|
+
* type NumberSet = Set<number>
|
|
20
|
+
* type NumberValue = SetValue<NumberSet> // number
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // Usage with const sets
|
|
26
|
+
* const COLORS = new Set(['red', 'green', 'blue'] as const)
|
|
27
|
+
* type ColorSet = typeof COLORS
|
|
28
|
+
* type Color = SetValue<ColorSet> // 'red' | 'green' | 'blue'
|
|
29
|
+
*
|
|
30
|
+
* // Function that accepts set values
|
|
31
|
+
* function processColor(color: SetValue<typeof COLORS>) {
|
|
32
|
+
* // color is typed as 'red' | 'green' | 'blue'
|
|
33
|
+
* console.log(`Processing color: ${color}`)
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // Complex usage with union types
|
|
40
|
+
* const UI_COLORS = new Set(['selection-stroke', 'accent', 'muted'])
|
|
41
|
+
* type UIColorSet = typeof UI_COLORS
|
|
42
|
+
* type UIColor = SetValue<UIColorSet> // 'selection-stroke' | 'accent' | 'muted'
|
|
43
|
+
*
|
|
44
|
+
* // Validate color is in set
|
|
45
|
+
* function isValidUIColor(color: string): color is UIColor {
|
|
46
|
+
* return UI_COLORS.has(color)
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
2
52
|
export type SetValue<T extends Set<any>> = T extends Set<infer U> ? U : never
|