type File = { id: string; name: string }; type List = { id: string; name: string; files: File[]; }[]; function fileAlreadyInDestination(list: List, source: string, destination: string) { const targetFolder: { id: string; name: string; files: File[] } | undefined = list.find( (folder) => folder.id === destination, ); const targetFile: undefined | File = targetFolder && targetFolder.files.find((f) => f.id === source); if (targetFile !== undefined) { throw new Error('Given source is already in given folder'); } } function sourceIsNotFile(list: List, source: string) { let targetSource: File | undefined; list.forEach((folder) => { folder.files.forEach((file) => { if (file.id === source) { targetSource = file; } }); }); if (targetSource === undefined) { throw new Error('You cannot move a folder'); } } function destinationIsNotFolder(list: List, destination: string) { const targetFolder = list.find((folder) => folder.id === destination); if (targetFolder === undefined) { throw new Error('Destination is not a folder'); } }