import { type} from "arktype";
const Account = type({name: "string"});
const Reminder = type({content: "string", due_at: "number"});
const Subtask = type({title: "string", completed_at: "number|null");
const Task = type([Subtask, "&", {subtasks: Subtask[]}]);
exists<T extends Account | Reminder | Task>(id: string, records: T[]) {
for (let item of records) {
if (item.id === id) {
return true;
}
if (item is a Task) {
for (let subtask of item.subtasks) {
if (subtask.id === id) {
return true
}
}
}
}
return false;
}```in this scenario, is there a way to do the `if (item is a Task)` check? I tried `if (item["subtasks"] !== undefined)` since `subtasks` is only a property of Task, but that gives an error saying it doesn't exist on the other types and so I can't index by it.
#How to determine object variable type from a union?
3 messages · Page 1 of 1 (latest)
if ("subtasks" in item)
Yep, thanks :)