Skip to content

Commit

Permalink
add isStringType, isNumberType
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Mar 16, 2018
1 parent e2b4a99 commit 66eecde
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/compiler/type/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ export class Type<TType extends ts.Type = ts.Type> {
return (this.compilerType.flags & TypeFlags.Boolean) !== 0;
}

/**
* Gets if this is a string type.
*/
isStringType() {
return (this.compilerType.flags & TypeFlags.String) !== 0;
}

/**
* Gets if this is a number type.
*/
isNumberType() {
return (this.compilerType.flags & TypeFlags.Number) !== 0;
}

/**
* Gets if this is an enum type.
*/
Expand Down
24 changes: 24 additions & 0 deletions src/tests/compiler/type/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ describe(nameof(Type), () => {
});
});

describe(nameof<Type>(t => t.isStringType), () => {
it("should get when it is a string type", () => {
const {firstType} = getTypeFromText("let myType: string;");
expect(firstType.isStringType()).to.equal(true);
});

it("should get when it's not a string type", () => {
const {firstType} = getTypeFromText("let myType: boolean;");
expect(firstType.isStringType()).to.equal(false);
});
});

describe(nameof<Type>(t => t.isNumberType), () => {
it("should get when it is a number type", () => {
const {firstType} = getTypeFromText("let myType: number;");
expect(firstType.isNumberType()).to.equal(true);
});

it("should get when it's not a number type", () => {
const {firstType} = getTypeFromText("let myType: boolean;");
expect(firstType.isNumberType()).to.equal(false);
});
});

describe(nameof<Type>(t => t.isEnumType), () => {
it("should get when it is an enum type", () => {
const {firstType} = getTypeFromText("let myType: MyEnum; enum MyEnum {}");
Expand Down

0 comments on commit 66eecde

Please sign in to comment.