Accept readonly arrays in function parameters
A function that only reads its array argument should say so. readonly T[] accepts both mutable and readonly arrays, and it stops the function from sorting or pushing into something the caller did not expect to change.
function newest(posts: readonly Post[]): Post | undefined {
return [...posts].sort(byDate)[0];
}sort mutates in place. The spread is the fix, and the readonly is what made me notice.
typescript