Skip to content

Add the .getNonPrimitiveType() method to the TypeChecker #61562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mrazauskas
Copy link

@mrazauskas mrazauskas commented Apr 10, 2025

Fixes #61517

This PR is adding the .getNonPrimitiveType() method to the TypeChecker. Similar to methods like .getAnyType() or .getUndefinedType(), the returned value of .getNonPrimitiveType() is the Type of the intrinsic object type.

A comment is included to explain what the method does. I think, it might not be clear that NonPrimitive is actually the intrinsic object type.

I updated the tests/baselines/reference/api/typescript.d.ts file. Is that enough for testing, perhaps? I looked around, but can’t find any explicit tests for other methods of the TypeChecker.

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Apr 10, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Apr 10, 2025
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #61517. If you can get it accepted, this PR will have a better chance of being reviewed.

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@mrazauskas mrazauskas changed the title Add the . getNonPrimitiveType() method to the TypeChecker Add the .getNonPrimitiveType() method to the TypeChecker Apr 10, 2025
@jakebailey
Copy link
Member

What's the use for this one? I can understand the rest, but this seems like a weird one to need.

@ritschwumm
Copy link

is there any advantage in naming the method getNonPrimitiveType and comment that it actually returns the object type, instead of naming it getObjectType in the first place?

@mrazauskas
Copy link
Author

What's the use for this one? I can understand the rest, but this seems like a weird one to need.

I was explaining my use case in the issue:

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.

And here is the workaround I have at the moment (obviously, this cannot work with TypeScript 7 anymore):

const nonPrimitiveType = { flags: this.#compiler.TypeFlags.NonPrimitive } as ts.Type; // the intrinsic 'object' type

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

@mrazauskas
Copy link
Author

mrazauskas commented Apr 10, 2025

is there any advantage in naming the method getNonPrimitiveType and comment that it actually returns the object type, instead of naming it getObjectType in the first place?

@ritschwumm See the issue, please. The naming was discussed there.

@mrazauskas
Copy link
Author

mrazauskas commented Apr 10, 2025

this seems like a weird one to need.

@jakebailey I agree, it might seem so. In a way I could use overloads and the one which requires the input to be an object could have extends object constraint. But.. Adding // @ts-expect-error just a line above disables any constraints.

A user might add // @ts-expect-error for any reason or simply by make mistake. The project I talk about does not execute the code. It use TypeScript programmatically to analyse types. The programmatic usage helps to catch user mistakes.

For example, current I can easily validate that the provided type is a function (type.getCallSignatures().length !== 0), but there is no simple way to check, if that is an object or not. Although it seems like object is an intrinsic type just like string or null. Current APIs allow to get the null, but not the object type.

Please, take into account the type provided by a user can be a union or an intersection. Hence looking for the NonPrimitive type flag is some job. Doable, but why not to rely on already existing TypeScript’s logic:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Add .getObjectType() to the TypeChecker
4 participants