|
| 1 | +/** |
| 2 | + * Examples demonstrating template literal types for case conversions |
| 3 | + * These provide compile-time type transformations for better type safety |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + camelCase, |
| 8 | + kebabCase, |
| 9 | + snakeCase, |
| 10 | + pascalCase, |
| 11 | + constantCase, |
| 12 | + dotCase, |
| 13 | + pathCase, |
| 14 | + sentenceCase, |
| 15 | + titleCase, |
| 16 | +} from "../src/index.js"; |
| 17 | + |
| 18 | +import type { |
| 19 | + CamelCase, |
| 20 | + KebabCase, |
| 21 | + SnakeCase, |
| 22 | + PascalCase, |
| 23 | + ConstantCase, |
| 24 | + DotCase, |
| 25 | + PathCase, |
| 26 | + SentenceCase, |
| 27 | + TitleCase, |
| 28 | +} from "../src/types/string-literals.js"; |
| 29 | + |
| 30 | +// ============================================ |
| 31 | +// 1. Basic Usage - Literal strings get literal types |
| 32 | +// ============================================ |
| 33 | + |
| 34 | +// When you pass a literal string, you get the exact transformed type back |
| 35 | +const apiEndpoint = kebabCase("getUserProfile"); |
| 36 | +// Type: "get-user-profile" (not just string!) |
| 37 | + |
| 38 | +const dbColumn = snakeCase("firstName"); |
| 39 | +// Type: "first_name" |
| 40 | + |
| 41 | +const componentName = pascalCase("user-avatar"); |
| 42 | +// Type: "UserAvatar" |
| 43 | + |
| 44 | +const envVar = constantCase("apiKey"); |
| 45 | +// Type: "API_KEY" |
| 46 | + |
| 47 | +// ============================================ |
| 48 | +// 2. Type-safe API Routes |
| 49 | +// ============================================ |
| 50 | + |
| 51 | +// Define your routes with literal types |
| 52 | +type ApiRoutes = "user-profile" | "user-settings" | "admin-panel"; |
| 53 | + |
| 54 | +// Convert to camelCase for JavaScript method names |
| 55 | +type MethodNames = { |
| 56 | + [K in ApiRoutes as `fetch${PascalCase<K>}`]: () => Promise<void>; |
| 57 | +}; |
| 58 | + |
| 59 | +// This creates an interface with: |
| 60 | +// fetchUserProfile(): Promise<void> |
| 61 | +// fetchUserSettings(): Promise<void> |
| 62 | +// fetchAdminPanel(): Promise<void> |
| 63 | + |
| 64 | +// ============================================ |
| 65 | +// 3. Configuration Objects |
| 66 | +// ============================================ |
| 67 | + |
| 68 | +// Convert between different naming conventions in configs |
| 69 | +const config = { |
| 70 | + "api-base-url": "https://api.example.com", |
| 71 | + "max-retries": 3, |
| 72 | + "timeout-ms": 5000, |
| 73 | +} as const; |
| 74 | + |
| 75 | +// Transform keys to camelCase for JavaScript usage |
| 76 | +type ConfigCamelCase = { |
| 77 | + [K in keyof typeof config as CamelCase<K>]: (typeof config)[K]; |
| 78 | +}; |
| 79 | + |
| 80 | +// Now you have type-safe camelCase keys: |
| 81 | +// apiBaseUrl: "https://api.example.com" |
| 82 | +// maxRetries: 3 |
| 83 | +// timeoutMs: 5000 |
| 84 | + |
| 85 | +// ============================================ |
| 86 | +// 4. Database Schema Mapping |
| 87 | +// ============================================ |
| 88 | + |
| 89 | +// Map JavaScript property names to database columns |
| 90 | +class User { |
| 91 | + firstName: string = ""; |
| 92 | + lastName: string = ""; |
| 93 | + emailAddress: string = ""; |
| 94 | + |
| 95 | + // Get the database column name with compile-time safety |
| 96 | + getColumn<T extends keyof this & string>(prop: T): SnakeCase<T> { |
| 97 | + return snakeCase(prop) as SnakeCase<T>; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const user = new User(); |
| 102 | +const column = user.getColumn("firstName"); |
| 103 | +// Type: "first_name" |
| 104 | + |
| 105 | +// ============================================ |
| 106 | +// 5. Event Handler Names |
| 107 | +// ============================================ |
| 108 | + |
| 109 | +// Convert event names to handler method names |
| 110 | +type EventName = "user-clicked" | "form-submitted" | "data-loaded"; |
| 111 | + |
| 112 | +type HandlerName<T extends EventName> = `on${PascalCase<T>}`; |
| 113 | + |
| 114 | +function createHandler<T extends EventName>(event: T): HandlerName<T> { |
| 115 | + return `on${pascalCase(event)}` as HandlerName<T>; |
| 116 | +} |
| 117 | + |
| 118 | +const clickHandler = createHandler("user-clicked"); |
| 119 | +// Type: "onUserClicked" |
| 120 | + |
| 121 | +// ============================================ |
| 122 | +// 6. CSS Class Names |
| 123 | +// ============================================ |
| 124 | + |
| 125 | +// Convert component names to BEM-style CSS classes |
| 126 | +type ComponentName = "UserProfile" | "NavigationMenu" | "SearchBar"; |
| 127 | + |
| 128 | +function getCssClass<T extends ComponentName>(component: T): KebabCase<T> { |
| 129 | + return kebabCase(component) as KebabCase<T>; |
| 130 | +} |
| 131 | + |
| 132 | +const cssClass = getCssClass("UserProfile"); |
| 133 | +// Type: "user-profile" |
| 134 | + |
| 135 | +// ============================================ |
| 136 | +// 7. Backward Compatibility |
| 137 | +// ============================================ |
| 138 | + |
| 139 | +// Runtime strings still work exactly as before |
| 140 | +function processUserInput(input: string): string { |
| 141 | + // Returns regular string type for runtime values |
| 142 | + return camelCase(input); |
| 143 | +} |
| 144 | + |
| 145 | +const userInput = "some-runtime-value"; |
| 146 | +const result = camelCase(userInput); |
| 147 | +// Type: string (not a literal type) |
| 148 | + |
| 149 | +// ============================================ |
| 150 | +// 8. Chaining Transformations |
| 151 | +// ============================================ |
| 152 | + |
| 153 | +// Chain multiple transformations with type safety |
| 154 | +const original = "hello-world" as const; |
| 155 | + |
| 156 | +const camel = camelCase(original); |
| 157 | +// Type: "helloWorld" |
| 158 | + |
| 159 | +const pascal = pascalCase(camel); |
| 160 | +// Type: "HelloWorld" |
| 161 | + |
| 162 | +const constant = constantCase(pascal); |
| 163 | +// Type: "HELLO_WORLD" |
| 164 | + |
| 165 | +const sentence = sentenceCase(constant); |
| 166 | +// Type: "Hello world" |
| 167 | + |
| 168 | +// ============================================ |
| 169 | +// 9. Template Literal Composition |
| 170 | +// ============================================ |
| 171 | + |
| 172 | +const prefix = "get" as const; |
| 173 | +const entity = "user-profile" as const; |
| 174 | +const methodName = camelCase(`${prefix}-${entity}`); |
| 175 | +// Type: "getUserProfile" |
| 176 | + |
| 177 | +// ============================================ |
| 178 | +// 10. Type Guards with Literal Types |
| 179 | +// ============================================ |
| 180 | + |
| 181 | +type KebabString = `${string}-${string}`; |
| 182 | + |
| 183 | +function isKebabCase(str: string): str is KebabString { |
| 184 | + return str.includes("-") && str === str.toLowerCase(); |
| 185 | +} |
| 186 | + |
| 187 | +function processKebab(str: string) { |
| 188 | + if (isKebabCase(str)) { |
| 189 | + // TypeScript knows str matches kebab pattern |
| 190 | + const camelVersion = camelCase(str); |
| 191 | + // Gets more precise type inference |
| 192 | + return camelVersion; |
| 193 | + } |
| 194 | + return str; |
| 195 | +} |
| 196 | + |
| 197 | +// ============================================ |
| 198 | +// Export for demonstration |
| 199 | +// ============================================ |
| 200 | + |
| 201 | +export { |
| 202 | + apiEndpoint, |
| 203 | + dbColumn, |
| 204 | + componentName, |
| 205 | + envVar, |
| 206 | + clickHandler, |
| 207 | + cssClass, |
| 208 | + methodName, |
| 209 | +}; |
0 commit comments