From b3d01c8418dde927c8b37c1fe8819a33406a5c56 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 23 Jun 2024 11:37:04 -0400 Subject: [PATCH] feat: Type.prototype.isBigInt and isBigIntLiteral (#1546) --- packages/ts-morph/lib/ts-morph.d.ts | 4 +++ packages/ts-morph/src/compiler/types/Type.ts | 14 +++++++++ .../src/tests/compiler/type/typeTests.ts | 30 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/packages/ts-morph/lib/ts-morph.d.ts b/packages/ts-morph/lib/ts-morph.d.ts index 02e007705..682320afb 100644 --- a/packages/ts-morph/lib/ts-morph.d.ts +++ b/packages/ts-morph/lib/ts-morph.d.ts @@ -10029,10 +10029,14 @@ export declare class 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. */ diff --git a/packages/ts-morph/src/compiler/types/Type.ts b/packages/ts-morph/src/compiler/types/Type.ts index 3b29d2031..b9d042c9f 100644 --- a/packages/ts-morph/src/compiler/types/Type.ts +++ b/packages/ts-morph/src/compiler/types/Type.ts @@ -443,6 +443,13 @@ export class 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. */ @@ -459,6 +466,13 @@ export class 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. */ diff --git a/packages/ts-morph/src/tests/compiler/type/typeTests.ts b/packages/ts-morph/src/tests/compiler/type/typeTests.ts index 8c764b7ca..be2d74727 100644 --- a/packages/ts-morph/src/tests/compiler/type/typeTests.ts +++ b/packages/ts-morph/src/tests/compiler/type/typeTests.ts @@ -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'; @@ -156,6 +158,34 @@ let unknownType: unknown; }); }); + describe(nameof("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("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("isBoolean"), () => { function doTest(typeName: string, expected: boolean) { expect(typesByName[typeName].isBoolean()).to.equal(expected);