From 56e7e59b8763df2727606770aab3c6429fd5b473 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 Nov 2026 14:32:39 -0800 Subject: [PATCH] Add Type.isClass to the public API Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- packages/typescript/src/api/async/api.ts | 4 +++ packages/typescript/src/api/async/types.ts | 2 ++ packages/typescript/src/api/sync/api.ts | 4 +++ packages/typescript/src/api/sync/types.ts | 2 ++ packages/typescript/test/async/api.test.ts | 37 ++++++++++++++++++++++ packages/typescript/test/sync/api.test.ts | 37 ++++++++++++++++++++++ 6 files changed, 86 insertions(+) diff --git a/packages/typescript/src/api/async/api.ts b/packages/typescript/src/api/async/api.ts index 7256e47c0da36..4390b74142a49 100644 --- a/packages/typescript/src/api/async/api.ts +++ b/packages/typescript/src/api/async/api.ts @@ -3926,6 +3926,10 @@ class TypeObject implements Type { return isClassOrInterfaceType(this); } + isClass(): this is InterfaceType { + return isObjectType(this) && (this.objectFlags & ObjectFlags.Class) !== 0; + } + isUnionType(): this is UnionType { return isUnionType(this); } diff --git a/packages/typescript/src/api/async/types.ts b/packages/typescript/src/api/async/types.ts index 290fb7d4c683c..0cfdeac039b97 100644 --- a/packages/typescript/src/api/async/types.ts +++ b/packages/typescript/src/api/async/types.ts @@ -90,6 +90,8 @@ export interface Type { /** Whether this type is a class or interface type */ isClassOrInterface(): this is InterfaceType; + /** Whether this is an originating class type, rather than a constructor or instantiated type reference */ + isClass(): this is InterfaceType; /** Whether this type is a union type */ isUnionType(): this is UnionType; /** Whether this type is an intersection type */ diff --git a/packages/typescript/src/api/sync/api.ts b/packages/typescript/src/api/sync/api.ts index 5534ffd53e1df..e9828750571c5 100644 --- a/packages/typescript/src/api/sync/api.ts +++ b/packages/typescript/src/api/sync/api.ts @@ -8257,6 +8257,10 @@ class TypeObject implements Type { return isClassOrInterfaceType(this); } + isClass(): this is InterfaceType { + return isObjectType(this) && (this.objectFlags & ObjectFlags.Class) !== 0; + } + isUnionType(): this is UnionType { return isUnionType(this); } diff --git a/packages/typescript/src/api/sync/types.ts b/packages/typescript/src/api/sync/types.ts index 8cf9530cbcc1f..071ae96cad212 100644 --- a/packages/typescript/src/api/sync/types.ts +++ b/packages/typescript/src/api/sync/types.ts @@ -148,6 +148,8 @@ export interface Type { /** Whether this type is a class or interface type */ isClassOrInterface(): this is InterfaceType; + /** Whether this is an originating class type, rather than a constructor or instantiated type reference */ + isClass(): this is InterfaceType; /** Whether this type is a union type */ isUnionType(): this is UnionType; /** Whether this type is an intersection type */ diff --git a/packages/typescript/test/async/api.test.ts b/packages/typescript/test/async/api.test.ts index 3f303868a73cc..0ada3f413687f 100644 --- a/packages/typescript/test/async/api.test.ts +++ b/packages/typescript/test/async/api.test.ts @@ -7084,6 +7084,43 @@ export const n: number = 0; }); }); +describe("Type - isClass", { concurrency }, () => { + test("distinguishes originating class types from interfaces, instances, and constructors", async () => { + const source = ` +export class Plain {} +export class Generic { value!: T; } +export interface Shape {} +export type Specialized = Generic; +export const constructor = Plain; +export const object = {}; +export const number = 0; +`; + await using api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": source, + }); + const snapshot = await api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + const project = snapshot.getConfiguredProject("/tsconfig.json")!; + + for (const [name, expected] of [["Plain", true], ["Generic", true], ["Shape", false], ["Specialized", false]] as const) { + const symbol = await project.checker.getSymbolAtPosition("/src/main.ts", source.indexOf(name)); + assert.ok(symbol); + const type = await project.checker.getDeclaredTypeOfSymbol(symbol); + assert.equal(type.isClass(), expected, name); + if (type.isClass()) { + const classType: InterfaceType = type; + assert.ok(classType.isClassOrInterface()); + } + } + for (const name of ["constructor", "object", "number"]) { + const symbol = await project.checker.getSymbolAtPosition("/src/main.ts", source.indexOf(name)); + assert.ok(symbol); + const type = await project.checker.getTypeOfSymbol(symbol); + assert.equal(type.isClass(), false, name); + } + }); +}); + describe("Type - isErrorType", { concurrency }, () => { test("identifies the error type from an unresolvable annotation", async () => { await using api = spawnAPI({ diff --git a/packages/typescript/test/sync/api.test.ts b/packages/typescript/test/sync/api.test.ts index c57ca53e905bc..4bb75fb51a9df 100644 --- a/packages/typescript/test/sync/api.test.ts +++ b/packages/typescript/test/sync/api.test.ts @@ -6842,6 +6842,43 @@ export const n: number = 0; }); }); +describe("Type - isClass", { concurrency }, () => { + test("distinguishes originating class types from interfaces, instances, and constructors", () => { + const source = ` +export class Plain {} +export class Generic { value!: T; } +export interface Shape {} +export type Specialized = Generic; +export const constructor = Plain; +export const object = {}; +export const number = 0; +`; + using api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": source, + }); + const snapshot = api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + const project = snapshot.getConfiguredProject("/tsconfig.json")!; + + for (const [name, expected] of [["Plain", true], ["Generic", true], ["Shape", false], ["Specialized", false]] as const) { + const symbol = project.checker.getSymbolAtPosition("/src/main.ts", source.indexOf(name)); + assert.ok(symbol); + const type = project.checker.getDeclaredTypeOfSymbol(symbol); + assert.equal(type.isClass(), expected, name); + if (type.isClass()) { + const classType: InterfaceType = type; + assert.ok(classType.isClassOrInterface()); + } + } + for (const name of ["constructor", "object", "number"]) { + const symbol = project.checker.getSymbolAtPosition("/src/main.ts", source.indexOf(name)); + assert.ok(symbol); + const type = project.checker.getTypeOfSymbol(symbol); + assert.equal(type.isClass(), false, name); + } + }); +}); + describe("Type - isErrorType", { concurrency }, () => { test("identifies the error type from an unresolvable annotation", () => { using api = spawnAPI({