Created
May 2, 2023 08:49
-
-
Save codeAdrian/61823ec2b4bc66c9ee3b1146e9d0b50a 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; | |
balanceFormatted: string; // New property, no longer redefined | |
} | |
const adapter = (user: User) => { | |
// New properties | |
const newProps = { | |
fullName: "", | |
balanceFormatted: "" | |
}; | |
return { ...user, ...newProps }; | |
}; | |
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