Created
May 2, 2023 08:46
-
-
Save codeAdrian/ff888cd5491da4a40a5b0a5413b6da30 to your computer and use it in GitHub Desktop.
This file contains 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
interface User { | |
firstName: string; | |
middleName?: string; | |
lastName: string; | |
email: string; | |
balance: number; | |
currency: string; | |
locale: string; | |
} | |
interface UserProxy extends User { | |
fullName: string; | |
balance: string; | |
} | |
const adapter = (user: User) => { | |
// Redefined properties | |
const balance = user.balance as unknown as string; | |
// New properties | |
const newProps = { | |
fullName: "", | |
}; | |
return { ...user, ...newProps, balance }; | |
}; | |
const handleGet = (obj: UserProxy, prop: string | symbol) => { | |
/* ... */ | |
}; | |
export const createUserDataProxy = (user: User): UserProxy => { | |
const proxy = new Proxy(adapter(user), { | |
get: handleGet, | |
}); | |
return proxy; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment