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
Forgive my abuse of the issue types, I'm not sure if this is a feature request, perhaps there is a clean way to handle it.
I would like to define a custom Semigroup for merging structs. To preserve a sane typescript interface I'd like the input type to my merging function to be:
I can define a Semigroup that does what I want by replacing the optional fields with suitable zero values using Option for token.
Below is a solution that first normalise the input values into a type that is easier to work with in fp-ts, however it involves additional boilerplate which is a bit of a shame. Namely:
I cannot easily infer PartialOptions from Options (with a nod to io-ts I would like to define my 'runtime types' once and infer the compile-time type via dependent typeof types.
I could automate the optionify function a bit but with the primitive I know about/understand it would be more work than just defining it explicitly.
This ticket looks like it is dealing with just the same flavour of problem: #1636
Here's a test case demonstrating what I'm thinking of:
describe('fp-ts',()=>{test('Merging an options type',()=>{// Helper type for inferring the type implied by a SemigrouptypeSemigroupType<T>=TextendsS.Semigroup<infer U> ? U : never;// Define a merging semigroup using Option for nullable valuesconstOptionsSemigroup=S.struct({baseUrl: S.last<string>(),token: O.getMonoid(S.last<string>()),extraHeaders: R.getUnionSemigroup(S.last<string>()),// Just for funnumber: N.SemigroupSum,});typeOptions=SemigroupType<typeofOptionsSemigroup>;constoptionsMerge=S.concatAll(OptionsSemigroup);// We could derive this from Options, but I don't know if a generic way to handle omitted values as zero types.// the amount of boilerplate in the current approaches makes this cleantypePartialOptions={baseUrl: string;token?: string;extraHeaders?: Record<string,string>;number?: number;};functionoptionify({ baseUrl, token, extraHeaders ={}, number =0}: PartialOptions): Options{return{
baseUrl,token: O.fromNullable(token),
extraHeaders,
number,};}// Here's what the outside world should seefunctionmergeOptions(base: PartialOptions, ...options: PartialOptions[]){returnoptionsMerge(optionify(base))(options.map(optionify));}// Here's the example usageconstbase: Options={baseUrl: 'bar',};constresult=mergeOptions(base,{number: 4,baseUrl: 'fff',extraHeaders: {foo: 'bar',bang: 'boo'}},{number: 6,baseUrl: 'fff',token: O.some('asdsadsadasd')},{number: 8,baseUrl: 'fff',token: 'oooooo',extraHeaders: {newKey: 'blah'}},{number: 10,baseUrl: 'http://example.com',extraHeaders: {foo: 'barbarbar'}},);console.log(result);expect(result).toStrictEqual({baseUrl: 'http://example.com',token: O.some('oooooo'),extraHeaders: {foo: 'barbarbar',bang: 'boo',newKey: 'blah'},number: 28,});});});
If this is a poor fit for the issues section here then my apologies and I will close it.
The text was updated successfully, but these errors were encountered:
silasdavis
changed the title
[Question (feature?)] Clean way to define a Semigroup over a partially partial type
[Question (feature?)] Clean way to define a Semigroup over a (partially) partial type
Aug 11, 2023
Forgive my abuse of the issue types, I'm not sure if this is a feature request, perhaps there is a clean way to handle it.
I would like to define a custom
Semigroup
for merging structs. To preserve a sane typescript interface I'd like the input type to my merging function to be:I can define a
Semigroup
that does what I want by replacing the optional fields with suitable zero values usingOption
fortoken
.Below is a solution that first normalise the input values into a type that is easier to work with in fp-ts, however it involves additional boilerplate which is a bit of a shame. Namely:
PartialOptions
fromOptions
(with a nod to io-ts I would like to define my 'runtime types' once and infer the compile-time type via dependenttypeof
types.optionify
function a bit but with the primitive I know about/understand it would be more work than just defining it explicitly.This ticket looks like it is dealing with just the same flavour of problem: #1636
Here's a test case demonstrating what I'm thinking of:
If this is a poor fit for the issues section here then my apologies and I will close it.
The text was updated successfully, but these errors were encountered: