Created
July 16, 2019 00:12
-
-
Save bfollington/48b7aca6a43e43fb235fc616bef27c7d to your computer and use it in GitHub Desktop.
A concise, typesafe lens implementation in TypeScript
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
interface INamed<T> { | |
name: T | |
} | |
export const Name = { | |
get<T>(named: INamed<T>) { | |
return named.name | |
}, | |
set<T>(named: INamed<T>, value: T) { | |
return { | |
...named, | |
name: value, | |
} | |
}, | |
} | |
const person = { | |
name: 'Ben', | |
age: 26, | |
} | |
console.log(Name.get(person)) | |
console.log(Name.get(Name.set(person, 'Ian'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment