简体   繁体   English

angular 中的 TypeScript 错误:声明类型既不是“void”也不是“any”的 function 必须返回一个值

[英]TypeScript error in angular: A function whose declared type is neither 'void' nor 'any' must return a value

I'm trying to highlight dates on a calendar.我正在尝试突出显示日历上的日期。 I'm using angular-material-datepicker with dateClass.我将angular-material- datepicker 与 dateClass 一起使用。 The logic is fine but I can't figure out why I'm getting the below error.逻辑很好,但我不知道为什么会出现以下错误。 Can anyone please tell me why I'm getting the below error?谁能告诉我为什么会出现以下错误?

A function whose declared type is neither 'void' nor 'any' must return a value.

HTML: HTML:

<mat-calendar *ngIf="loadCalendar" [dateClass]="dateClass()" [selected]="selectedDate" (selectedChange)="onSelect($event)"></mat-calendar>

Ts: TS:

import { DatePipe } from "@angular/common";
import { Component, OnInit } from "@angular/core";
import { MatCalendarCellCssClasses } from "@angular/material";
import { DataService } from "../app/services/data.service";
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
  providers: [DatePipe],
})
export class AppComponent implements OnInit {
  cardData;
  dates: string[] = [];
  loadCalendar: boolean = false;
  constructor(private data: DataService, public datepipe: DatePipe) {}
  ngOnInit() {
    this.data.getData().subscribe((data) => {
      this.cardData = data;
      this.cardData.forEach((element) => {
        //  console.log(this.datepipe.transform(element.start, 'yyyy/MM/dd'));
        this.dates.push(element.start);
        this.loadCalendar = true;
      });
    });
  }

  dateClass() { //Error is caused by this function
      return (date: Date): MatCalendarCellCssClasses => {

        this.dates.forEach((sD)=>{

          let startDate = this.datepipe.transform(sD, "yyyy/MM/dd");
          let calendarDate = this.datepipe.transform( date.getDate(),"yyyy/MM/dd");
          
          if (calendarDate === startDate) {
            return "special-date";
          } else {
            return "";
          }
        })

      };
  }
}

 return (date: Date): MatCalendarCellCssClasses => {  <-- !!!You say it returns a MatCalendarCellCssClasses

        this.dates.forEach((sD)=>{

          let startDate = this.datepipe.transform(sD, "yyyy/MM/dd");
          let calendarDate = this.datepipe.transform( date.getDate(),"yyyy/MM/dd");
          
          if (calendarDate === startDate) {
            return "special-date";
          } else {
            return""   
          }
        
        })
       return "" <--- you must also return something here incase forEach does not loop 
}

Edit: Updated answer as question was updated编辑:随着问题的更新而更新答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声明类型既不是“ void”也不是“ any”的函数必须返回一个值。 TypeScript / React - A function whose declared type is neither 'void' nor 'any' must return a value. TypeScript/React 未捕获的错误:输入是一个空元素标签,既不能有`children`也不能使用`dangerouslySetInnerHTML` - Uncaught Error: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` ReactJS 错误输入是一个空元素标签,不能有`children`也不能使用`dangerouslySetInnerHTML` - ReactJS Error input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` 为什么在 angular typescript 中使用 onclick 事件必须给它一个 void 类型? - Why using onclick event in angular typescript must give it a void type? 当值既不是数字也不是字符串类型时,Number() 是否显式调用 toString()? (例如功能) - Does Number() explicitly call toString() when value is neither number nor string type? (e.g. function) TypeScript 错误 '(response) =&gt; void' 类型的参数不可分配给'(value: void) =&gt; void | 类型的参数承诺喜欢<void> '</void> - TypeScript error Argument of type '(response) => void' is not assignable to parameter of type '(value: void) => void | PromiseLike<void>' 在打字稿中,返回空函数的函数的返回类型是什么? - In typescript, what is the return type of a function that returns a void function? ReactJs:输入是一个空元素标签,不能有`children`也不能使用`dangerouslySetInnerHTML` - ReactJs: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` 错误:使用了无效 function 返回值 - Error: Void function return value is used 为什么JS异步函数既不打印也不显示错误? - Why JS async function shows neither print nor error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM