【问题标题】:How can I type this function in Typescript如何在 Typescript 中键入此函数
【发布时间】:2021-09-09 01:51:11
【问题描述】:

当我构建项目时,它显示为 2 个功能,请帮助我!!

第一个错误是:

Argument of type 'IFilmCard[] | undefined' is not assignable to parameter of type 'IFilmCard[]'.
  Type 'undefined' is not assignable to type 'IFilmCard[]'
 
export function __filterFilms(crts: [string, string?], arr: IFilmCard[]) {
    return __filterByGenre(crts[0], __filterByYear(crts[1], arr))
} 

第二个错误是:

Argument of type 'IPersonCard[] | undefined' is not assignable to parameter of type 'IPersonCard[]'.Type 'undefined' is not assignable to type 'IPersonCard[]'


export function __filterPersons(crts: [string, string?], arr: IPersonCard[]) {
    return __filterByCountry(crts[0], __filterByYearsPersons(crts[1], arr))
} 

【问题讨论】:

  • 在两个函数arr: IFilmCard[] = []上设置一个空数组作为arr参数的默认值

标签: javascript typescript next.js


【解决方案1】:

如果您的变量是 IPersonCard[] 并且不能未定义,您可以在属性后添加“as IPersonCard[]”。但是你会失去 Typescript 检查的好处。 此错误意味着您的数据可能是 IPersonCard[],但也可能是未定义的并且您之前没有检查过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2021-11-04
    • 2021-11-17
    • 1970-01-01
    • 2021-02-10
    • 2018-10-23
    • 2018-08-24
    相关资源
    最近更新 更多