Skip to content

Instantly share code, notes, and snippets.

@davidwallacejackson
Created August 9, 2019 18:08
Show Gist options
  • Save davidwallacejackson/aa6bb2fe0cc468aa8b9af8f28f064f4e to your computer and use it in GitHub Desktop.
Save davidwallacejackson/aa6bb2fe0cc468aa8b9af8f28f064f4e to your computer and use it in GitHub Desktop.
interface Serialized {
a: string,
b: string,
c: string | string[],
}
interface Deserialized {
field: number, //parsed from a
otherField: string, // parsed from b
yetAnotherField: string[] // parsed from c
}
interface Field<K extends keyof Deserialized, V = K[V]> {
parse: (serialized: Serialized) => V,
render: (value: V) => Partial<Serialized>,
}
const fields = {
field: {
parse: (serialized) => Number.parseFloat(serialized.a),
render: (value) => ({ a: value.toString() }),
},
// ... etc for otherField and yetAnotherField
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment