Created
May 18, 2025 16:51
-
-
Save mikr13/5e3cc4ae43784b04f0d9c639e09f440e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type User = { | |
| id: string; | |
| username: string; | |
| email: string; | |
| password: string; | |
| isActive: boolean; | |
| roles: ("admin" | "user" | "guest")[]; | |
| }; | |
| type Profile = { | |
| userId: string; | |
| bio?: string; | |
| avatarUrl?: string; | |
| }; | |
| type Session = { | |
| sessionId: string; | |
| userId: string; | |
| expiresAt: Date; | |
| }; | |
| type RedactedUser = Omit<User, "password">; | |
| type Combined = RedactedUser & { | |
| profile?: Omit<Profile, "userId">; | |
| session?: Session; | |
| status: "online" | "offline" | "away"; | |
| }; | |
| // Intermediate type will show nested intersections and unions as-is | |
| type Intermediate = Combined; | |
| // Prettify flattens type display in editors | |
| type Prettify<T> = { | |
| [K in keyof T]: T[K]; | |
| } & {}; | |
| type Flattened = Prettify<Intermediate>; | |
| // Example usage | |
| const userWithSession: Flattened = { | |
| id: "u123", | |
| username: "alice", | |
| email: "alice@example.com", | |
| isActive: true, | |
| roles: ["admin", "user"], | |
| profile: { | |
| bio: "Software engineer", | |
| avatarUrl: "https://example.com/avatar.png" | |
| }, | |
| session: { | |
| sessionId: "s456", | |
| userId: "u123", | |
| expiresAt: new Date() | |
| }, | |
| status: "online" | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment