Hah! It turns out those ads for Heat Wave during Castle aren’t fake: ABC actually had someone write the Nikki Heat book as a tie-in.
The first time I remember seeing something like this was with (appropriately enough) Murder She Wrote, with a paperback mystery novel credited to Jessica Fletcher. In that case, though, the show had been on for years and was a television staple. DC Comics got into it in 1997 with The Life Story of the Flash, credited to Iris (West) Allen, which had previously been referenced in the comic books.
More recently, Lost had an in-universe book published in the real world: Bad Twin was billed as the final novel by one of the passengers on Oceanic 815 who didn’t make it through the first episode. I actually read that one. It was interesting enough, though it had little to do with the show beyond the presence of the Widmores.