Skip to content

Commit

Permalink
feat: Type.prototype.isBigInt and isBigIntLiteral (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 23, 2024
1 parent 868755d commit b3d01c8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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

0 comments on commit b3d01c8

Please sign in to comment.