That’s what they tell you... That seems to be the generally received wisdom for new writers, except that, in my experience, new writers are young and don’t know diddly squat. I didn’t begin writing in earnest until I was in my forties and I didn’t know diddly squat.
Besides, you won’t learn anything by writing what you already know, and where’s the fun in that?
I don’t know who said it, but I imagine the reason to think it’s a good idea to write what you know is that it’s tough to write, so it’s not a bad idea to make it easier, and I get that. To write a novel, for example, takes months or even years, and considerable mental stamina, so is it a good idea, in the first instance, to begin with unfamiliar subject matter? Maybe not.
Honestly, I don’t think it matters in the slightest whether you write what you know. I think what really matters is that you write what you feel... that you write what you must.
My first novel was, thematically, about something I thought I knew a good deal about, but I gave it an SF setting, introduced a lot of stuff about maths that I had to learn, had a key character on the Autistic spectrum that gave me plenty to research, and lo and behold all the thematic stuff changed when the very process of writing the novel adjusted my thinking. It was a great experience, a real pleasure and a lovely way to learn something new about myself. I don’t know if you’ll ever read Savant, but it’s a book I’m still proud of, and a great beginning.
Writing is a transformative experience, for me at least. Whatever I know when I begin a project might alter entirely by the time I’ve finished, my opinions might change, and I’m almost bound to learn a great deal along the way. I might only learn a lot of facts, but, more than once, I’ve learned things about life, about how I feel about things, about the way my mind works, about how to write better.
In the end, perhaps we always write what we know, but, here’s the thing: The more we write, the more we learn, and, with any luck, the better we get at this. I certainly hope that’s true, because I’m about to put Naming Names out into the world, and then I’m embarking on The Winter Lamb, and everything I’ve learned about being a writer is going to be very useful when it comes to writing that book... very useful indeed.