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
Why can I not specify any type alias as an index signature parameter? Specifically, if each type in an alias is an instance of an accepted signature parameter.
This specifically supports goal #5, and obliquely supports goal #1 as well.
Use Cases
I notice that use cases where we want hinting to be exact, the hinting cannot alwys be fully expressive. for example, starting from:
Here we provide an attributes dictionary that maps selectors to arrays of attributes. Such that we could iterate through the keys, querySelect on each key, then apply the attributes to each matching element.
The default use would probably be from document.querySelectorAll. A non-global use of this interface would instead be from el.querySelectorAll. If we want the use case to support applying attributes to el itself, we need a special case, ':root'. Now, the interface suffices but users of the interface won't know the special case from hinting alone. Better would be:
import{Directive,ElementRef,Input,Renderer2}from'@angular/core'exportinterfaceStyleSetting{styleName: stringvalue: string|number}exportinterfaceDataAttribute{dataName: stringvalue: string}exportinterfaceAttributeSetting{attributeName: stringvalue: string}exporttypeElementAttribute=StyleSetting|DataAttribute|AttributeSettingexportinterfaceAttributesDictionary{[selector: ':root'|string]: ElementAttribute[]}
@Directive({selector: '[common-dynamic-attributes]'})exportclassDynamicAttributesDirective{private_dynamicAttributes: AttributesDictionary
@Input('common-dynamic-attributes')setdynamicAttributes(attributes: AttributesDictionary){this._dynamicAttributes=attributesthis.applyDynamicAttributes()}constructor(privateelRef: ElementRef,privaterenderer: Renderer2){}applyDynamicAttributes(attributes: AttributesDictionary=this._dynamicAttributes){Object.entries(attributes).forEach(([selector,rule])=>{if(selector==':root'){this.applyRuleToElement(rule,this.elRef.nativeElement)}else{// some browsers may not support forEach on NodeList, so we wrap with Array.fromArray.from(this.elRef.nativeElement.querySelectorAll(selector)).forEach(el=>this.applyRuleToElement(rule,this.elRef.nativeElement))}})}applyRuleToElement(rule: Array<ElementAttribute>,el: HTMLElement){rule.forEach(setting=>{if(setting.hasOwnProperty('attributeName')){constattribute=settingasAttributeSettingel.setAttribute(attribute.attributeName,attribute.value)}elseif(setting.hasOwnProperty('styleName')){conststyle=settingasStyleSettingel.style[style.styleName]=style.value}else{constdata=settingasDataAttributeel.dataset[data.dataName]=data.value}})}}
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Search Terms
type alias, alias, index signature parameter
Suggestion
Why can I not specify any type alias as an index signature parameter? Specifically, if each type in an alias is an instance of an accepted signature parameter.
This specifically supports goal #5, and obliquely supports goal #1 as well.
Use Cases
I notice that use cases where we want hinting to be exact, the hinting cannot alwys be fully expressive. for example, starting from:
Here we provide an attributes dictionary that maps selectors to arrays of attributes. Such that we could iterate through the keys, querySelect on each key, then apply the attributes to each matching element.
The default use would probably be from
document.querySelectorAll
. A non-global use of this interface would instead be fromel.querySelectorAll
. If we want the use case to support applying attributes to el itself, we need a special case, ':root'. Now, the interface suffices but users of the interface won't know the special case from hinting alone. Better would be:Examples
angular 9 example:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: