Skip to content

Add .getObjectType() to the TypeChecker #61517

Closed
@mrazauskas

Description

@mrazauskas

πŸ” Search Terms

"getNonPrimitiveType", "getObjectType", "TypeChecker"

βœ… Viability Checklist

⭐ Suggestion

I would like to propose adding .getObjectType() method to the TypeChecker. Similar to methods like .getAnyType() or .getUndefinedType(), the returned type of .getObjectType() would be the Type of the object type.

Alternative name: .getNonPrimitiveType(). Same as the variable is named:

var nonPrimitiveType = createIntrinsicType(TypeFlags.NonPrimitive, "object");

πŸ“ƒ Motivating Example

If I see it right object is the only intrinsic / built-in type that does not have a .get method. Might be this is intentional and also might be object isn’t considered intrinsic.

The motivation is to fill in a gap. If there is one.

Somewhat similar to: #59256

πŸ’» Use Cases

  1. What do you want to use this for?

The project I am working on is using TypeScript programatically. The object type is used to validate user input. A user provides a type and the logic checks if that type is assignable to the object type. If not, a friendly message is printed that an object type was expected.

  1. What shortcomings exist with current approaches?

As a workaround, I create a fake type. Obviously this cannot work with TypeScript 7 anymore.

  1. What workarounds are you using in the meantime?
const nonPrimitiveType = { flags: this.#compiler.TypeFlags.NonPrimitive } as ts.Type; // the intrinsic 'object' type

if (!typeChecker.isTypeAssignableTo(sourceType, nonPrimitiveType)) {
  // skipped
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions