Closed kimamula closed 1 month ago
Hey @kimamula, sorry for the slow response. Here's the full snippet:
type Input = { /* ... */ };
type Output = { /* ... */ };
interface VariableMap {
[name: string]: Input | Output;
}
type NamedVariable = (Input | Output) & { name: string };
This came pretty directly from a use case at my work. We had a tool that worked with CSV data. There were two types of data, inputs and outputs. Internally we removed the name
column and constructed that VariableMap
. But to output CSV again, we needed to add the name
column back.
Hopefully that makes a little more sense. Perhaps not the most useful or widely applicable technique. But hey, the book only claimed that it "can sometimes be useful." :)
Thank you for the explanation! Yes, that makes sense.
Hi Dan, thank you for the great book!
In Item 13, the
NamedVariable
type is used as an example to explain how extending union types can be useful. But I don't get it. Could you show me some concrete use cases in which this type is useful? Thanks in advance!