Skip to content

Instantly share code, notes, and snippets.

@bfollington
Created July 16, 2019 00:12
Show Gist options
  • Save bfollington/48b7aca6a43e43fb235fc616bef27c7d to your computer and use it in GitHub Desktop.
Save bfollington/48b7aca6a43e43fb235fc616bef27c7d to your computer and use it in GitHub Desktop.
A concise, typesafe lens implementation in TypeScript
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