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
So, I'm working as hobbyists to build a web component framework, the idea I want is that you create a type like this:
import{T,createComponent}from'unnamed-framework';typeElementAttributes={firstName: T.Attribute<string>;lastName: T.Attribute<string>;displayName: T.Computed<string>;};constelement=createElement({selector: 'app-element',attributes: {firstName: 'Joe',lastName: 'Doe',displayName: ({ firstName, lastName })=>`${firstName()}${lastName()}`,},content: ({ displayName })=>{console.log(`This is the full name ${displayName}`);};});
Now, all of this I manage to do it with the following type:
type_MapAttributes<AttrsextendsValidAttributes,PrimitiveMatcher,ComputedMatcher>=H.Pipe<Attrs,[H.Match<[// If no attributes returns never.H.Match.With<NoAttributes,never>,H.Match.With<Attributes,H.ComposeLeft<[// Maps all the attributes.H.Objects.MapValues<H.Match<[// If is a primitive attribute.H.Match.With<BaseAttributeType<unknown,'attribute'>,PrimitiveMatcher>,// If is a computed attribute.H.Match.With<BaseAttributeType<unknown,'computed'>,ComputedMatcher>,]>>,]>>,]>,]>;
Then have single type for each case, for example this is for the config in the function, you need to create the default values:
/** Utility type to get the value type passed by the end user. */type_GetAttributeType<TextendsBaseAttributeType<unknown,unknown>>=T['__value'];/** Matcher type using HotScript to pass in the `ConfigAttributes` type. */interface_ConfigMatcher<AttrsextendsValidAttributes=NoAttributes>extendsH.Fn{return: AttrsextendsNoAttributes// If no attributes it means is an primitive attribute.
? _GetAttributeType<this['arg0']>// If has attributes it means is a computed attribute.
: (self: SelfAttributes<Attrs>)=>_GetAttributeType<this['arg0']>;}interface_SignalMatcher<ReadOnlyextendsboolean>extendsH.Fn{return: Signal<_GetAttributeType<this['arg0']>,ReadOnly>;}/** * Attributes for the fragment config. */exporttypeConfigAttributes<AttrsextendsValidAttributes>=_MapAttributes<Attrs,_ConfigMatcher<NoAttributes>,_ConfigMatcher<Attrs>>;
Then you have another one for the content parameter to pass the values in the content function:
/** * Attributes for the fragment content. */exporttypeContentAttributes<AttrsextendsValidAttributes>=_MapAttributes<Attrs,_SignalMatcher<false>,_SignalMatcher<true>>;
And finally the one I'm having issues is the parameters for the computed attributes, the idea is to have all the other attributes available but itself, so this:
Should not have displayName inside of the param object. I haven figured how to so far. I was able to achieve this with raw types like this:
/** * Maps a single field of a fragment attributes to its real value. */exporttypeToRealValue<AttrsextendsValidAttributes,FieldNameextendsstring,FieldValue,AsSignalextendsboolean,ReadOnlyextendsboolean,>=// FieldName has a reserved name.FieldNameextends'children' ? ReservedErrorMessage<'children'>
: FieldNameextends'fallback' ? ReservedErrorMessage<'fallback'>
: FieldNameextends'errorBoundary' ? ReservedErrorMessage<'errorBoundary'>// FieldValue is an attribute.
: FieldValueextendsAttributeType<infer RealValue>
? AsSignalextendstrue ? Signal<_BooleanInfer<RealValue>,ReadOnly>
: _BooleanInfer<RealValue>// FieldValue is a computed attribute.
: FieldValueextendsComputedType<infer RealValue>
? AsSignalextendstrue ? Signal<ComputedReturn<_BooleanInfer<RealValue>>,true>
: ComputedAttribute<Attrs,FieldName,_BooleanInfer<RealValue>>// FieldValue is of a un-allowed type.
: never;/** * Maps the attributes of a fragment to their real value. */exporttypeAttributesForComputed<AttrsextendsValidAttributes>=AttrsextendsNoAttributes
? never
: {// Here it maps each value and you can get the Value and Name.[FieldNameinExtract<keyofAttrs,string>]: ToRealValue<Attrs,FieldName,Attrs[FieldName],true,true>;};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
So, I'm working as hobbyists to build a web component framework, the idea I want is that you create a type like this:
Now, all of this I manage to do it with the following type:
Then have single type for each case, for example this is for the config in the function, you need to create the default values:
Then you have another one for the content parameter to pass the values in the content function:
And finally the one I'm having issues is the parameters for the computed attributes, the idea is to have all the other attributes available but itself, so this:
Should not have
displayName
inside of the param object. I haven figured how to so far. I was able to achieve this with raw types like this:Beta Was this translation helpful? Give feedback.
All reactions