简体   繁体   中英

Typescript property does not exist on type

I am receiving this error for both list.item references: client_1 | TS2339: Property 'items' does not exist on type '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 .

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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