You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I observed some weird behavior while working on my app, so I tried to reduce the problem to a minimal example.
typea= {
title: string,
ingredients: array<string>,
}
typeb= {
...a,
parsedIngredients: array<string>,
}
// This worksleta: a= {
title: "Pizza",
ingredients: ["1 EL Oil", "1 EL flour"],
}
// EXPECTED: This should compileletb: b= {
// ERROR: This has type `a`, but it's expected to have type `b`...a,
parsedIngredients: ["Oil", "Flour"],
}
I don't know if I misunderstood the record type spread feature, but IMO from an intuition perspective I'd expect value b to initialize just fine, since we are essentially spreading in all the values that a has, plus the parsedIngredients value that's required from b.
Does this work as expected?
The text was updated successfully, but these errors were encountered:
ryyppy
changed the title
Record spread from smaller to bigger record type doesn't work
Record spread from smaller to bigger record type set doesn't work
Dec 3, 2023
cc @cristianoc who can explain this better than me, but this is the current state:
The new record type spreads is only at the type level. This example is a runtime thing
Can't go from a to b here for a because it's not a subtype (missing required parsedIngredients)
So that's the current state. Nothing exists to handle what you're asking about essentially.
I do agree with you however that this type of functionality would feel natural. I guess it could be the equivalent of inlining all of the fields and values of a into b in the compiler at the time of spreading. Looking at it, it definitively feels like this is something that'd be good if one could do.
I observed some weird behavior while working on my app, so I tried to reduce the problem to a minimal example.
I don't know if I misunderstood the record type spread feature, but IMO from an intuition perspective I'd expect value
b
to initialize just fine, since we are essentially spreading in all the values thata
has, plus theparsedIngredients
value that's required fromb
.Does this work as expected?
The text was updated successfully, but these errors were encountered: