Reputation: 15091
I am learning TypeScript.
The contents of todoItem.ts
:
export class TodoItem {
constructor(
public id: number,
public task: string,
public complete: boolean = false
) {}
printDetails(): void {
console.log(
`${this.id}\t${this.task} ${this.complete ? "\t(complete)" : ""}`
);
}
}
The contents of todoCollection.ts
:
import { TodoItem } from "./todoItem";
export class TodoCollection {
private nextId: number = 1;
constructor(public userName: string, public todoItems: TodoItem[] = []) {}
addTodo(task: string): number {
while (this.getTodoById(this.nextId)) {
this.nextId++;
}
this.todoItems.push(new TodoItem(this.nextId, task));
return this.nextId;
}
getTodoById(id: number): TodoItem {
return this.todoItems.find((item) => item.id === id);
}
}
When compiling with tsc
, I get the following error:
src/todoCollection.ts:17:5 - error TS2322: Type 'TodoItem | undefined' is not assignable to type 'TodoItem'. Type 'undefined' is not assignable to type 'TodoItem'.
17 return this.todoItems.find((item) => item.id === id); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error.
How to fix this error? I really have no idea because I cannot spot any peculiarity. Everything looks fine.
Upvotes: 1
Views: 175
Reputation: 7573
Array#find
may return undefined
if the predicate function does not return a truthy value for any element of the array. Typescript is complaining because you've told it you will be returning a TodoItem
in the method header, but you actually attempted to return something that might be a TodoItem
or might be undefined
(as far as static analysis can tell).
You have a few options on how to resolve this:
| undefined
to your return type, eggetTodoById(id: number): TodoItem | undefined {
getTodoById(id: number): TodoItem {
const result = this.todoItems.find(item => item.id === id)
if (!result) {
throw new Error("Not Found!");
}
return result;
}
!
postfix operator.return this.todoItems.find(item => item.id === id)!
Upvotes: 1