Description
π Search Terms
"getNonPrimitiveType", "getObjectType", "TypeChecker"
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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
- 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.
- What shortcomings exist with current approaches?
As a workaround, I create a fake type. Obviously this cannot work with TypeScript 7 anymore.
- 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
};