TypeScript types Object.keys to return string[] instead of (keyof T)[] due to its structural type system. Because an object typed as T may contain additional properties at runtime that TypeScript isn't aware of, returning keyof T would be unsafe — it could lead to accessing undefined validators or properties. The post explains structural typing with concrete examples, shows how extraneous properties are allowed by design, and demonstrates how to leverage structural typing positively by defining minimal interfaces that only declare the properties a function actually needs.

7m read timeFrom alexharri.com
Post cover image
Table of contents
Unsafe usage of Object.keysMaking use of structural typing

Sort: