Skip to content
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

feat: Type.prototype.isBigInt and isBigIntLiteral #1546

Merged
merged 1 commit into from
Jun 23, 2024
Merged
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/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10029,10 +10029,14 @@ export declare class Type<TType extends ts.Type = ts.Type> {
isString(): boolean;
/** Gets if this is a number type. */
isNumber(): boolean;
/** Gets if this is a BigInt. */
isBigInt(): boolean;
/** Gets if this is a literal type. */
isLiteral(): boolean;
/** Gets if this is a boolean literal type. */
isBooleanLiteral(): boolean;
/** Gets if this is a BigInt literal type. */
isBigIntLiteral(): boolean;
/** Gets if this is an enum literal type. */
isEnumLiteral(): boolean;
/** Gets if this is a number literal type. */
Expand Down
14 changes: 14 additions & 0 deletions packages/ts-morph/src/compiler/types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ export class Type<TType extends ts.Type = ts.Type> {
return this.#hasTypeFlag(TypeFlags.Number);
}

/**
* Gets if this is a BigInt.
*/
isBigInt() {
return this.#hasTypeFlag(TypeFlags.BigInt);
}

/**
* Gets if this is a literal type.
*/
Expand All @@ -459,6 +466,13 @@ export class Type<TType extends ts.Type = ts.Type> {
return this.#hasTypeFlag(TypeFlags.BooleanLiteral);
}

/**
* Gets if this is a BigInt literal type.
*/
isBigIntLiteral() {
return this.#hasTypeFlag(TypeFlags.BigIntLiteral);
}

/**
* Gets if this is an enum literal type.
*/
Expand Down
30 changes: 30 additions & 0 deletions packages/ts-morph/src/tests/compiler/type/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ let anyType: any;
let stringType: string;
let booleanType: boolean;
let numberType: number;
let bigIntType = 5n + 10n;
const bigIntLiteralType = 5n;
let booleanLiteralType: true;
let numberLiteralType: 5;
let stringLiteralType: 'test';
Expand Down Expand Up @@ -156,6 +158,34 @@ let unknownType: unknown;
});
});

describe(nameof<Type>("isBigInt"), () => {
function doTest(typeName: string, expected: boolean) {
expect(typesByName[typeName].isBigInt()).to.equal(expected);
}

it("should get when it is", () => {
doTest("bigIntType", true);
});

it("should get when it's not", () => {
doTest("stringType", false);
});
});

describe(nameof<Type>("isBigIntLiteral"), () => {
function doTest(typeName: string, expected: boolean) {
expect(typesByName[typeName].isBigIntLiteral()).to.equal(expected);
}

it("should get when it is", () => {
doTest("bigIntLiteralType", true);
});

it("should get when it's not", () => {
doTest("stringType", false);
});
});

describe(nameof<Type>("isBoolean"), () => {
function doTest(typeName: string, expected: boolean) {
expect(typesByName[typeName].isBoolean()).to.equal(expected);
Expand Down