4

I am getting the following error and I cannot work out why its complaining..

Error TS2322 (TS) Type '() => string' is not assignable to type 'string'.

Closest I came to finding info about this error was here but while the error was the same the situation was not.

I am equating "cssClasses", which is defined as a string, to an anonymous function that returns a string.. That's the idea and the function returns a string yet its saying "string is not assignable to string"

How do I fix this?

This is my code:

        for (let day = 0; day < numberOfDays; day++) {

        cssClasses = (): string => {

            if (day == calendarLayout.dayDifference) {
                return "cal-heading2-day-" + day.toString() + " " + cssToday;
            } else
                return ("cal-heading2-date-" + day.toString())
        }...
1
  • 1
    A function is not a string. So you can't assign a function to a variable of type string. That's what the error is telling you. You can call the function and assign the string it returns to the variable, if that's what you want to do. But I don't really see the point of the function. Commented Apr 16, 2019 at 6:43

1 Answer 1

2

The correct typing for your function is the following:

const cssClasses: () => string = () => {
    if (day == calendarLayout.dayDifference) {
        return "cal-heading2-day-" + day.toString() + " " + cssToday;
    } else {
        return ("cal-heading2-date-" + day.toString());
    }
}

As correctly pointed out in the comments, you are trying to assign the type string to a function that is returning a string, which is quite different.

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.