简体   繁体   English

Typescript 类型上不存在属性

[英]Typescript property does not exist on type

I am receiving this error for both list.item references: client_1 |我收到了两个 list.item 引用的错误:client_1 | TS2339: Property 'items' does not exist on type 'List'. TS2339:属性“items”在类型“List”上不存在。

Here is some relevant code.这是一些相关的代码。 How do I work around this without setting it to any?如何在不将其设置为任何的情况下解决此问题?

export interface List {
    createdAt: number;
    id: string;
    items: Array<ToDo>;
    updatedAt: number;
}
const emptyList = { createdAt: 0, id: '', items: [], updatedAt: 0 }
...
const [list, setList] = useState<List>(emptyList)
...
const filterList = <List extends any>(list: List) => {
    if (activeFilter === 'All') {
        return setFilteredTodos(list.items)
    }

    setFilteredTodos(list.items.filter((todo: ToDo) => todo.completed === filterMap[activeFilter as keyof object] as {}))
}

Writing <List extends any> before the function defines a generic type parameter with the (new) name List on the arrow function filterList .在 function 之前编写<List extends any>定义了一个泛型类型参数,在箭头filterList上具有(新)名称List

To make it more clear, your code is equivalent to:为了更清楚,您的代码等效于:

const filterList = <T extends any>(list: T) => { ... }

As you probably want to use the type List from above, you should remove the <List extends any> from the function assigned to filterList :由于您可能想使用上面的类型List ,因此您应该从分配给 filterList 的filterList中删除<List extends any>

const filterList = (list: List) => { ... }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM