|
| 1 | +/** |
| 2 | + * Branded/Tagged type utilities for creating nominal types. |
| 3 | + * These are used to ensure only official Clerk objects can be passed to certain props. |
| 4 | + */ |
| 5 | + |
| 6 | +declare const Tags: unique symbol; |
| 7 | + |
| 8 | +/** |
| 9 | + * Creates a branded/tagged type that prevents arbitrary objects from being assigned. |
| 10 | + * The tag exists only at the type level and has no runtime overhead. |
| 11 | + * |
| 12 | + * @example |
| 13 | + * ```typescript |
| 14 | + * type UserId = Tagged<string, 'UserId'>; |
| 15 | + * const userId: UserId = 'user_123' as UserId; |
| 16 | + * ``` |
| 17 | + */ |
| 18 | +export type Tagged<BaseType, Tag extends PropertyKey> = BaseType & { [Tags]: { [K in Tag]: void } }; |
| 19 | + |
| 20 | +/** |
| 21 | + * Branded type for Clerk JS version pinning objects. |
| 22 | + * Used with the `clerkJs` prop in ClerkProvider. |
| 23 | + * |
| 24 | + * @example |
| 25 | + * ```typescript |
| 26 | + * import { clerkJs } from '@clerk/clerk-js-pinned'; |
| 27 | + * <ClerkProvider clerkJs={clerkJs} /> |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +export type ClerkJs = Tagged< |
| 31 | + { |
| 32 | + /** |
| 33 | + * The version string of @clerk/clerk-js to load from CDN. |
| 34 | + */ |
| 35 | + version: string; |
| 36 | + }, |
| 37 | + 'ClerkJs' |
| 38 | +>; |
| 39 | + |
| 40 | +/** |
| 41 | + * Branded type for Clerk UI version pinning objects. |
| 42 | + * Carries appearance type information via phantom property for type-safe customization. |
| 43 | + * |
| 44 | + * @example |
| 45 | + * ```typescript |
| 46 | + * import { ui } from '@clerk/ui-pinned'; |
| 47 | + * <ClerkProvider ui={ui} appearance={{ ... }} /> |
| 48 | + * ``` |
| 49 | + */ |
| 50 | +export type Ui<A = any> = Tagged< |
| 51 | + { |
| 52 | + /** |
| 53 | + * The version string of @clerk/ui to load from CDN. |
| 54 | + */ |
| 55 | + version: string; |
| 56 | + /** |
| 57 | + * Optional custom URL to load @clerk/ui from. |
| 58 | + */ |
| 59 | + url?: string; |
| 60 | + /** |
| 61 | + * Phantom property for type-level appearance inference. |
| 62 | + * This property never exists at runtime. |
| 63 | + * @internal |
| 64 | + */ |
| 65 | + __appearanceType?: A; |
| 66 | + }, |
| 67 | + 'ClerkUi' |
| 68 | +>; |
| 69 | + |
| 70 | +/** |
| 71 | + * Extracts the appearance type from a Ui object. We have 3 cases: |
| 72 | + * - If the Ui type has __appearanceType with a specific type, extract it |
| 73 | + * - If __appearanceType is 'any', fallback to base Appearance type |
| 74 | + * - Otherwise, fallback to the base Appearance type |
| 75 | + */ |
| 76 | +export type ExtractAppearanceType<T, Default> = T extends { __appearanceType?: infer A } |
| 77 | + ? 0 extends 1 & A // Check if A is 'any' (this trick works because 1 & any = any, and 0 extends any) |
| 78 | + ? Default |
| 79 | + : A |
| 80 | + : Default; |
0 commit comments