# Dependencies

> Link tasks with finish-to-start, start-to-start, finish-to-finish and start-to-finish dependencies, add lag or lead in working or elapsed time, and understand which links are allowed.

A dependency says that one task cannot start or finish until another one has started or finished. The engine uses dependencies to place tasks and to find the critical path.

## The dependency object

```ts
import type { GanttDependency } from '@birchui/gantt';

const dependencies: GanttDependency[] = [
  { id: 'l1', from: 'discovery', to: 'ia' },
  { id: 'l2', from: 'ia', to: 'design', type: 'FS' },
  { id: 'l3', from: 'ia', to: 'content', type: 'SS', lag: 2, lagUnit: 'day' },
  { id: 'l4', from: 'build', to: 'qa', type: 'FS', lag: -1 },
  { id: 'l5', from: 'printing', to: 'mailing', type: 'FS', lag: 2, lagUnit: 'elapsedDay' },
];
```

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | Unique identifier. |
| `from` | `string` | The predecessor task. |
| `to` | `string` | The successor task. |
| `type` | `'FS' \| 'SS' \| 'FF' \| 'SF'` | Defaults to `'FS'`. |
| `lag` | `number` | Delay after the link point. Negative values are a lead (overlap). Defaults to `0`. |
| `lagUnit` | `LagUnit` | Unit of `lag`. Defaults to `'day'`. |
| any other key | `unknown` | Your own fields, kept on the link. |

## Link types

| Type | Meaning | Example |
| --- | --- | --- |
| `'FS'` finish-to-start | The successor starts after the predecessor finishes. | Pour foundations, then build walls. |
| `'SS'` start-to-start | The successor starts after the predecessor starts. | Content migration can begin two days after the information architecture starts. |
| `'FF'` finish-to-finish | The successor finishes after the predecessor finishes. | Scanner configuration cannot finish before racking is complete. |
| `'SF'` start-to-finish | The successor finishes after the predecessor starts. | The old system's support shift ends once the new system goes live. |

## Lag and lead

`lag` is measured in the **successor's calendar**, so `lag: 2` with `lagUnit: 'day'` means two working days of the task that waits. A negative lag is a lead: `lag: -1` on an FS link lets the successor start one working day before the predecessor finishes.

| `lagUnit` | Meaning |
| --- | --- |
| `'minute'`, `'hour'`, `'day'`, `'week'` | Working time. A day is `hoursPerDay` hours and a week is `daysPerWeek` days. |
| `'elapsedHour'`, `'elapsedDay'` | Calendar time that ignores working hours, weekends and holidays, such as concrete curing or paint drying over a weekend. |

In predecessor text, the same units are written `min`, `h`, `d`, `w`, `eh` and `ed`: `4FS+2ed` is a finish-to-start link with a two-elapsed-day lag. See [Predecessor text](https://docs.birchui.com/gantt/task-grid.md#predecessor-text).

## Links and summary tasks

- A link **to** a summary task applies to all of its subtasks: none of them can start before the link allows. Summary successors accept only `'FS'` and `'SS'` links; an `'FF'` or `'SF'` link to a summary in loaded data is kept but not scheduled, with a warning.
- A link **from** a summary task uses the summary's rolled-up start or finish.
- A summary task cannot be linked to its own subtasks, in either direction. When indenting or outdenting would create such a link, the link is removed.

## Rules and validation

`addDependency` and every link created in the UI are checked before they are added. These links are refused:

- a task linked to itself;
- a link between a summary and one of its own subtasks;
- an `'FF'` or `'SF'` link into a summary task;
- a second link between the same two tasks;
- a link that would create a loop.

```ts
const link = gantt.addDependency({ from: 'qa', to: 'golive', type: 'FS' });
if (!link) showToast('That link is not allowed.');
```

A refused `addDependency` returns `undefined` and logs a warning that names the reason, such as "this link would create a dependency loop". Without an `id`, the link gets one like `'qa->golive'`.

Your own rules go in `validateDependency`, which runs for every new link: dragged, created from the keyboard, typed as predecessor text or added with `addDependency`. It receives the scheduled `from` and `to` tasks, the `type`, `lag`, `lagUnit` and the `source`; return `false` to refuse:

```ts
createGantt(container, {
  tasks,
  dependencies,
  validateDependency: ({ from, to, type }) => {
    if (from.phase !== to.phase && type !== 'FS') return false;
    return !to.isLocked;
  },
});
```

## Loops in loaded data

Data loaded with `tasks` and `dependencies`, `fromJSON` or `fromMSProjectXML` can contain loops. The engine never hangs on them: it ignores the links that close each loop, schedules the rest, and emits `cycleDetected` once per new set of loops:

```ts
gantt.on('cycleDetected', ({ cycles }) => {
  for (const { taskIds, dependencyIds } of cycles) {
    console.warn(`Loop through ${taskIds.join(', ')}; ignored links ${dependencyIds.join(', ')}`);
  }
});
```

`schedule()` also returns the loops in `cycles`.

## Editing links

| Method | Description |
| --- | --- |
| `addDependency({ from, to, type?, lag?, lagUnit?, id? })` | Add a link. Returns the link, or `undefined` when it is refused. |
| `removeDependency(id)` | Remove a link. |
| `setDependencies(dependencies)` | Replace all links and reschedule. Clears the undo history. |
| `getDependencies()` | Copies of all links. |
| `selectDependency(id \| null)` | Select a link, as clicking it does. |

Users create links by dragging between bar edges or from the keyboard (see [Interactions](https://docs.birchui.com/gantt/interactions.md#drawing-links)), and edit them as text in the predecessors column. Adding or removing a link emits `dependencyCreate` or `dependencyRemove` with the link and the `source`, then `tasksScheduled`. Changing the type or lag of an existing link in predecessor text updates it in place and emits only `tasksScheduled`, so read the new values with `getDependencies()` if you persist links.

Links are drawn as orthogonal connectors from the predecessor's start or finish to the successor's, with an arrow at the successor. Click a link to select it and press <kbd>Delete</kbd> to remove it. With `showCriticalPath`, links between critical tasks are highlighted. `showDependencies: false` hides links without changing the schedule.

## Next steps

- [Calendars](https://docs.birchui.com/gantt/calendars.md) for working time.
- [Scheduling](https://docs.birchui.com/gantt/scheduling.md) for how links and constraints combine.
