Having trouble with typescript with issue #45916

hey I am tryin to tackle Wrong hint for step 24 of Cafe Menu project · Issue #45919 · freeCodeCamp/freeCodeCamp (github.com)

my code,

Edit: I have added Array<string>, because I changed find function to filter function in getStyle method

interface ExtendedStyleDeclaration extends CSSStyleDeclaration {

  getPropVal: (prop: string | Array<string>, strip?: boolean) => string;

}
getStyle(selector: string): ExtendedStyleDeclaration | null {

    const style = this._getStyleRules().filter(  <-----

      ele => ele?.selectorText === selector

    )?.style as ExtendedStyleDeclaration |undefined;

    if (!style) return null;

    style.getPropVal = (prop: string | Array<string>, strip = false) => {

      return strip

        ? style.getPropertyValue(prop).replace(/\s+/g, '')

        : style.getPropertyValue(prop);

    };

    return style;

  }

the errors:

  • Property 'style' does not exist on type 'CSSStyleRule[]'.

  • "Argument of type 'string | string[]' is not assignable to parameter of type 'string'.\n Type 'string[]' is not assignable to type 'string'."

Hey Sboony.

What are you mainly struggling with?

The error does make sense, because there is no style property on an Array type.

Hey Shaun,

The error does make sense, because there is no style property on an Array type.

lack of knowledge in my part.

I hoped adding the code below will make it feasible, because filter function result in array, and from my understanding the style error happened because I was passing the wrong type

interface ExtendedStyleDeclaration extends CSSStyleDeclaration {

  getPropVal: (prop: string | Array<string>, strip?: boolean) => string;

}

I do not see a filter function in your code.

You are currently just taking in either a string or and array of strings for the prop parameter.

oh it’s in getStyle method

 const style = this._getStyleRules().filter(  <---

      ele => ele?.selectorText === selector

    )?.style as ExtendedStyleDeclaration |undefined;

looking back, I should have made it clearer, what I have changed. Sorry about that.