Javascript Type 'false' is not assignable to type 'string'

Viewed 497

Trying to figure out what I missed when declaring event.allDay = false.

The following code works fine, but the vscode error would not go away.

loadFromDevice(year, month, status) {
    this.db.dbState().subscribe(res => {
        if (res) {
            this.db.fetchActivities(year, month, status).subscribe(item => {

                // console.log("fetchActivities:")
                // console.log(item)

                // var entry: {
                //     allDay: boolean
                // }

                item.forEach(function (entry) {
                    var date = new Date();
                    entry.startTime = new Date(entry.start_datetime);
                    entry.endTime = new Date(entry.end_datetime);
                    entry.allDay = false
                })

                return this.eventSource = item;

            })
        }
    })
}

enter image description here

Appreciate any leads or correction if I made a mistake. Thank you.


Solution

Pasting my solution here based on the suggestion I received.

loadFromDevice(year, month, status) {
        this.db.dbState().subscribe(res => {
            if (res) {
                this.db.fetchActivities(year, month, status).subscribe(data => {

                    // console.log("Activities->loadFromDevice.fetchActivities:")
                    // console.log(data)

                    let cal = [];
                    data.forEach(function(item) {
                        cal.push({
                            title: item.title,
                            startTime: new Date(item.start_datetime),
                            endTime: new Date(item.end_datetime),
                            allDay: (item.all_day == 'true') ? true : false
                        })

                    })

                    console.log("Activities->loadFromDevice.cal:")
                    console.log(cal)

                    return this.eventSource = cal;
                });
            }
        });
    }

This works perfectly!

1 Answers

You're not writing JavaScript, you're writing TypeScript.

Somewhere (not enough context provided) there's a type definition for these entry objects that states that the allDay property is represented by a string. It might look something like this (but could also be a type, class, ... instead of an interface):

interface Entry {
  startTime: Date;
  endTime: Date;
  allDay: string;
}

However, you're trying to assign a boolean false value (which is not a string), so the TypeScript compiler notifies you of a type violation.

Related