I'm getting some data that has a metadata field in it that I'd like stripped out. It comes to me in typed variables like so:
type MainRecord = {
id: string
fieldOne: string
related: RelatedRecord[]
__typename: string
}
type RelatedRecord = {
id: string
fieldTwo: string
__typename: string
}
I'm using Formik to update the records but I don't want the __typename
fields included when I create the initial values for the form because trying to send the data to the server with the metadata included causes syntax errors for the query when I do. I have separate types for the Formik forms like so:
type MainFields = {
id: string
fieldOne: string
related: RelatedFields[]
}
type RelatedFields = {
id: string
fieldTwo: string
}
My solution was to try to cast the MainRecord
data as a MainFields
object which seems like its working because fields.__typename
is not a valid property and neither is fields.related[0].__typename
.
const record: MainRecord = {
id: "10",
fieldOne: "Foo",
__typename: "Main",
related: [
{
id: "20",
fieldTwo: "Bar",
__typename: "Related"
},
{
id: "21",
fieldTwo: "Baz",
__typename: "Related"
},
]
}
const fields: MainFields = record // or "as MainFields"
However when you log the fields
variable, the metadata is still there and it shouldn't be.
console.log("fields: ", fields);
I guess I could strip them out programmatically by hand but there's got to be a better way. Any ideas? I'm sure there's a typescript way of accomplishing this but I just don't know it.