Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/typescript/src/api/async/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript/src/api/async/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 4 additions & 0 deletions packages/typescript/src/api/sync/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript/src/api/sync/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
37 changes: 37 additions & 0 deletions packages/typescript/test/async/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> { value!: T; }
export interface Shape {}
export type Specialized = Generic<string>;
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({
Expand Down
37 changes: 37 additions & 0 deletions packages/typescript/test/sync/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> { value!: T; }
export interface Shape {}
export type Specialized = Generic<string>;
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({
Expand Down
Loading