7

Given an arbitrary type, I want to check if the type is an object whose prototype is Object.prototype. In other words, I want to exclude Functions, Sets, Maps, custom classes, etc. I can't just list every possible object type, since there's an infinite number of custom classes. Is this possible?

I want to do this to recursively apply a mapping to an object. Here's what I have:

type MapDeep<T> = F<
  T extends Primitive ? T
  : T extends Array<infer U> ? Array<MapDeep<U>>
  : T extends ReadonlyArray<infer U> ? ReadonlyArray<MapDeep<U>>
  : T extends Set<infer U> ? Set<MapDeep<U>>
  : T extends Map<infer K, infer V> ? Map<K, MapDeep<V>>
  : T extends Partial<Record<string, any>> ? { [K in keyof T]: MapDeep<T[K]> }
  : T
>;

However, for objects that have prototypes other than Object.prototype, this doesn't work. E.g. MapDeep<() => void> returns F<{}> instead of F<() => void>, since it matches Partial<Record<string, any>>. How can I tell if a type's prototype is Object.prototype?

3
  • What is F type here? Commented Aug 22, 2021 at 9:26
  • 1
    F is any arbitrary transform of any arbitrary input Commented Aug 22, 2021 at 9:39
  • "* E.g. MapDeep<() => void> returns F<{}> instead of F<() => void>, since it matches Partial<Record<string, any>>.*" - well, a function is an object, so this is correct. Does your code (i.e. the implementation of a function using MapDeep) actually treat functions specially? Then you also need to test for callable objects in that type declaration. Commented Aug 22, 2021 at 17:39

1 Answer 1

4

This is not possible. A typescript type does not contain information about the prototype chain of an object, it only specifies the type interface of properties and their signatures.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.