const o1 = { f: () => 1 }
const keys1 = Object.keys(o1) // ['f']
class C { f() {} }
const c1 = new C()
const keys2 = Object.keys(c1) // []
以下ObjectKeys
返回['f']
的{{1}}。这是我的问题。
class C
我知道TypeScript永远不会知道属性是否被隐藏。但是我想摆脱那些我们知道对于type ObjectKeys<T> = T extends object ? { [Key in keyof T]: Key }[keyof T] : never
function objectKeys<T>(o: T) {
if (o === undefined || o === null) {
return []
}
return (Object.keys(o) as any) as ReadonlyArray<ObjectKeys<T>>
}
const keys3 = objectKeys(o1) // Return type is ['f']
const keys4 = objectKeys(c1) // Return type is ['f'], but the value is []
不可见的类方法名称。