0
export function collectAttributes(element: Element): AttributesInterface {
    return Array.from(element.attributes, ({ name, value }) => [
        name,
        value,
    ]).reduce((a, [key, val]) => {
        if (key.includes("@")) {
            let handlerName = key.replace("@", "")
            a.handlers.push([handlerName, val])
        } else if (key.startsWith("s:")) {
            let styleProp = key.replace("s:", "")
            a.bindedStyle.push([styleProp, val])
        } else if (key.startsWith("c:")) {
            let classProp = key.replace("c:", "");
            a.bindedClasses.push([classProp, val])
        } else if (key.startsWith("a:")) {
            let attributeProp = key.replace("a:", "");
            a.bindedAttr.push([attributeProp, val])
        } else if (key === "if") {
            a.show = val
        } else if (key.startsWith("p:")) {
            let prop = key.replace("p:", "");
            a.props[prop] = val  // <------ HERE
        } else {
            a.attr.push([key, val])
        }
        return a
    }, {
        attr: [],
        handlers: [],
        bindedStyle: [],
        bindedAttr: [],
        bindedClasses: [],
        show: null,
        visible: true,
        props: {},
        parent: element.parentElement,
        index: element.parentElement ? Array.prototype.indexOf.call(element.parentElement.children, element) : 0
    })
}
export interface AttributesInterface {
    attr: string[][],
    handlers: string[][],
    bindedStyle: string[][],
    bindedAttr: string[][]
    bindedClasses: string[][],
    show: string,
    visible: Boolean,
    parent: HTMLElement,
    index: number,
    props: { [key: string]: string }
}

I have the problem that i cannot set a new property. I have tried it to set { [key: string]: string } to my props object but it doesnt seems to fix the problem. I still get the error:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'.

1
  • share your tsconfig.json Commented Nov 8, 2020 at 20:55

1 Answer 1

1

I have fixed it:

I have added the AttributesInterface to the accumulator of the reduce function

.reduce((a: AttributesInterface, [key, val]) => {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.