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
Add an Override helper-type to your library of utility types.
π Motivating Example
My use-case is a domain model with a matching JSON payload, but the JSON payload has some differences.
Upon loading the "packed" JSON payload, I will unpack it to the proper domain model - here, I am reusing the proper model type, replacing the properties that are "packed" into tuples.
The & union type operator doesn't work for this, because it performs declaration merging by default, and (from what I could find) there is no way to opt-out of that.
Example:
interfaceModel{foo: string;bar: string[];baz: number;items: Item[]}interfaceItem{a: string;b: string;c: string;}typeItemJSON=[string,string,string];// JSON payload is "packed" to save bandwidthtypeModelJSON=Override<Model,{items: ItemJSON[];}>;functionunpack(json: ModelJSON): Model{return{
...json,items: json.items.map(array=>({a: array[0],b: array[1],c: array[2],})),}}
The proposed Override type: (reuses the Pick and Exclude helpers to reduce verbosity)
/** * Override and replace the properties of T with with those in type U. */typeOverride<T,U>=Pick<T,Exclude<keyofT,keyofU>>&U;
π» Use Cases
The example above is the use-case.
For now, I am manually adding this type to my projects - but it seems like a common enough requirement that this might be worth considering as a standard utility type?
(I have a branch here, which includes a simple test - I would have just submitted a PR, but your PR policy says not to do that without first getting it on the roadmap, so... let's see what you think? π)
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
override type
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Add an
Override
helper-type to your library of utility types.π Motivating Example
My use-case is a domain model with a matching JSON payload, but the JSON payload has some differences.
Upon loading the "packed" JSON payload, I will unpack it to the proper domain model - here, I am reusing the proper model type, replacing the properties that are "packed" into tuples.
The
&
union type operator doesn't work for this, because it performs declaration merging by default, and (from what I could find) there is no way to opt-out of that.Example:
The proposed
Override
type: (reuses thePick
andExclude
helpers to reduce verbosity)π» Use Cases
The example above is the use-case.
For now, I am manually adding this type to my projects - but it seems like a common enough requirement that this might be worth considering as a standard utility type?
(I have a branch here, which includes a simple test - I would have just submitted a PR, but your PR policy says not to do that without first getting it on the roadmap, so... let's see what you think? π)
The text was updated successfully, but these errors were encountered: