问题
After asking this question and trying out this suggested solution, I have the following generic entity type:
type Entity<'a> = {
Id : Guid
Data : 'a
}
Now, before I expose this in an API, I want to flatten it, so that the Id
and the properties in Data
can be serialized next to each-other. I tried this, but it doesn't compile:
let flatten<'a> (v : Entity<'a>) = { v.Data with Id = v.Id }
What I had hoped to achieve, was to return a record type such that if 'a
is e.g. { foo : string; bar : int }
, then flatten { Id = someGuid; Data = { foo : "baz"; bar : 42 } }
would return { Id = someGuid; foo = "baz"; bar = 42 }
(or equivalent).
Is there a way to accomplish this, or am I trying to make the type system too dynamic?
来源:https://stackoverflow.com/questions/39306148/create-new-record-type-inline-by-extending-existing-record-with-new-member