WHERE to start with Maggie O’Farrell? The first time she almost died? The sixth? The 15th? Or with one of the three near-drownings?

No. Let us begin instead with when she came close to being murdered, close enough to feel the strangler’s strap. That incident forms the opening chapter of O’Farrell’s memoir I Am, I Am, I Am, a book which recalls, elegantly, all these encounters with death.

It was out on the hills. Almost 30 years ago now. O’Farrell had not long left school. A man on a mountain path, blocking her way. She couldn’t meet his eyes, but who can doubt what she would have seen in them? Wickedness and desire. She somehow escaped, talked her way out of it, even as he had her by the throat. She went to the police but wasn’t believed. Less than two weeks later, he raped and killed a 22-year-old backpacker from New Zealand. “Death brushed past me on that path,” O’Farrell writes, “so close that I could feel its touch, but it seized that other girl and thrust her under.”

She thinks about the young woman most days. “Is that survivor’s guilt?” I ask when we meet in Edinburgh.

“I think it is, yeah,” she replies. “I do feel guilty about her. Or more kind of ashamed that I couldn't have helped. That’s probably the reason why I never really talked about it. I’ve always wondered whether I could have changed what happened to her; if I had made more of a fuss at the police station, made them listen to me.”

O’Farrell is 45. She has a Pre-Raphaelite look: Millais skin, Rosetti hair, and, around her neck, a golden bee – that painterly symbol of hope, love and mortality – on a golden chain.

We are in a café just off The Meadows. O’Farrell lives in the city. She is married to the writer William Sutcliffe. They have a boy of 14, and two girls, eight and five. I Am, I Am, I Am is dedicated to the children. The book is, as she explains in an open letter to readers, a response to her elder daughter’s life-threatening medical condition: “Soon after she was born, she was diagnosed with a severe immunology disorder. Death, then, is a constant risk in our house. I must continually think about how best to defend her from it …”

O’Farrell intended the memoir as a way of making her daughter feel less alone, but it is for all three children because the trauma of seeing the little girl experience potentially fatal anaphylaxis – which happens once or twice a year – is something they all go through.

The ambulance, the flashing light, feeling her own heart race as her daughter’s slows down; she has grown as used to these as any parent ever can. The chapter she has written on this subject is the only one she is unable to read out loud.

Her daughter was born with chronic eczema. In a powerful essay for The Guardian, reworked and expanded for the memoir, O’Farrell has written about this with scalding candour. “Her eczema is an awful lot better now,” she tells me, “but when she was four or five she was very sick. It was agony, and she missed a lot of school. She couldn’t understand it. You could see she was baffled. She looked at other people and thought, ‘Why is their skin not like mine?’

“The only thing I ever found to help her was to tell her stories about it. I think we do, as humans, have a need for narrative. It explains the things that are unexplainable or difficult.”

What stories? “When she was small I would make up fairy stories about her with a metaphorical idea of why this has happened. I had this whole story about snow angels …” She stops herself. “I shouldn’t tell you this, because I’m going to write about it. I made up mythical stories about why stuff happens. To a four-year-old, you can’t say the reason your skin is blistered and painful is because of immunology. That’s white noise.”

I Am, I Am, I Am is, therefore, an exercise in the consolations of storytelling. O’Farrell, a successful novelist, uses the techniques and aesthetics of fiction in the service of autobiography. This provided an insulating layer which meant the stories (mostly) did not hurt to relive and articulate. Also, “taking difficult subject matter and pinning it down in words makes you feel in a way that you’ve got control. What my daughter has and what we go through with her is uncontrollable. So writing about it felt quite nice, in a way, because I could control the material on the page”.

But on the very day when O’Farrell submitted the final manuscript, “I had to take my daughter to a medical appointment where I found out that certain aspects of her condition had got worse … And I realised that feeling of control was a total illusion.”

O’Farrell herself was seriously ill as a child. At the age of eight, she contracted encephalitis: inflammation of the brain. “When you are a child, no-one tells you that you’re going to die,” she writes. “You have to work it out for yourself.” She had to learn how to walk again, could barely hold a pen, and has been left with motor control problems. Her long period of convalescence – bedridden, watchful, an eager witness – made her first a reader and then a writer.

In hospital one day she had a visitor. White hair, gold chain, cigar breath. He told the nurse to go; she refused. A lucky escape. A brush, not with death, but with the worst of life. It is interesting that she doesn’t name Jimmy Savile in the book. Why? “It was something to do with evil,” she says. "His name feels very potent and I didn’t want to write it. I felt it was a hex. He is pure black-hearted evil. I didn’t want to type those letters in that sequence. I didn’t want them in a book which my children and family and friends also inhabit.”

Evil, a hex, a black heart – I Am, I Am, I Am feels rooted in fairy story and myth. Death is almost personified; a figure reaching out, against whose grasp O’Farrell must defend her daughter and herself. This feeling goes back to school, she thinks, and reading Donne’s Holy Sonnets: “Death, be not proud …” That poem articulated something she had previously felt, but not realised.

“Similarly,” I start to say, “there are many personifications of goodness in your book …”

“Yeah,” she interrupts, laughing, “angels.”

These are people who helped her at moments of crisis. Among them: the nurse who keeps Savile at bay; a mysterious man in hospital scrubs who holds her hand as obstetric surgeons work to stop her bleeding; the bearded man from the Welsh Valleys, paralysed from the waist down, who encourages her to get out of her wheelchair and take that first step. Is that really how she sees them – guardian angels?

“Well, I’m not a religious person, but in a way, I do think that,” she says. “I’ve always been intrigued by the idea of these people who intersect with our lives, even just for a really short time, and change us or our path. All myths have their basis in truth, and that’s where our idea of guardian angels comes from.”

The most moving story in I Am, I Am, I Am – for me, anyway – concerns a number of miscarriages O’Farrell suffered between the births of her son and elder daughter. I was curious as to why someone who seems instinctually insular and private would choose to share such sadness.

“There’s a huge conspiracy of silence around miscarriage and fertility issues,” she says. “Well, conspiracy’s the wrong word. Naturally, people don’t want to talk about it. It’s so private and interior and biological and personal. I could probably count on one hand the number of conversations I’ve had with people about it. Miscarriage is so common yet so secret because it’s upsetting and visceral. And I think there is a kind of strength to be had from hearing the experiences of people who have been in a similar position. I certainly found that at the time.”

During a week when a miscarried child was due to have been born, she read Hilary Mantel’s memoir Giving Up The Ghost, in which she describes her endometriosis, a condition which led, ultimately, to her being unable to have children. “Hearing other people’s stories, you feel less alone,” O’Farrell says. “So it was important to me to write about it. I wasn’t in danger of dying, but it still feels like a brush with death. You are a vessel for death. You contain this dead child. It’s very important to who I am and the shape of my family.”

She writes, in the memoir: “If asked, I could reel off exactly, instantly and without hesitation, what age all my miscarried children would be, had they lived.” Reading this, I remembered the character Rosalind, in her novel This Must Be The Place, who imagines the three she lost as presences in the house. The Romany language has a word for this, O’Farrell had Rosalind muse: Detlene.

Does that word speak to her own experience? She nods. “It’s the only word I’ve ever found which refers directly to the soul of the child. I remember being hugely comforted by it. I had it stuck on my pinboard for a while. Just that word, no explanation. In our language there is nothing, and that sort of denies the existence of it. You can say "a miscarried child" or "a foetus", but that Romany word refers to the person they were, the wandering spirit, the idea that they’re out there – and I think a lot of people do feel that.”

It is tempting to regard O’Farrell’s encounters with death as a series of rebirths. A plane nearly crashes and she is left with the urge to write. Mugged at knifepoint, she becomes pregnant soon afterwards. Until that childhood morning when she wakes with a headache, she is one person; forever afterwards she is quite another.

“Rebirth sounds a bit New Age-y,” she says, “but I think you’re right. I very narrowly escaped either dying or being in a wheelchair, and there is a sense that you want to make every second count.” Her experiences have given her a sense of purpose, whetted the blade with which she’s slashed away at every obstacle. She is grateful for the life she might not have had, and determined to use it well.

“Ever since I was a child,” she says, “I’ve felt I was living on extra time.”

I Am, I Am, I Am: Seventeen Brushes With Death by Maggie O’Farrell is published by Tinder Press, priced £18.99