Delving deeper into the realm of advanced types in Typescript, I came across an intriguing type called NonFunctionPropertyNames. This type is designed to extract only the properties of a given object that are not functions.
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
Understanding the initial part within the curly brackets
'{ [K in keyof T]: T[K] extends Function ? never : K }'
is relatively clear - we are constructing an object and filtering out properties that extend Function. However, the section following the curly brackets [keyof T]
appears to define an array {...}[keyof T]
, but it ultimately returns an object.
Can someone clarify why merely using the curly brackets is insufficient for defining the type, and elucidate the purpose of [keyof T]
.