Skip to content

Commit

Permalink
feat: #278 - Add Type.isTupleType
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Mar 17, 2018
1 parent f6d85f3 commit d7c3c3d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/compiler/type/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ export class Type<TType extends ts.Type = ts.Type> {
return (this.compilerType.flags & TypeFlags.TypeParameter) !== 0;
}

/**
* Gets if this is a tuple type.
*/
isTupleType() {
const targetType = this.getTargetType();
if (targetType == null)
return false;
return (targetType.getObjectFlags() & ObjectFlags.Tuple) !== 0;
}

/**
* Gets if this is a union type.
*/
Expand Down
54 changes: 42 additions & 12 deletions src/tests/compiler/type/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,48 @@ describe(nameof(Type), () => {
});
});

describe(nameof<Type>(t => t.isTupleType), () => {
function doTest(text: string, expected: boolean) {
const {firstType} = getTypeFromText(text);
expect(firstType.isTupleType()).to.equal(expected);
}

it("should be when tuple and one element", () => {
doTest("let myType: [string];", true);
});

it("should be when tuple and multiple", () => {
doTest("let myType: [string, number];", true);
});

it("should not be when not an array", () => {
doTest("let myType: string[];", false);
});

it("should not be when not an array of tuples", () => {
doTest("let myType: [string][];", false);
});

it("should not be when not tuple", () => {
doTest("let myType: string;", false);
});
});

describe(nameof<Type>(t => t.isUndefinedType), () => {
function doTest(text: string, expected: boolean) {
const {firstType} = getTypeFromText(text);
expect(firstType.isUndefinedType()).to.equal(expected);
}

it("should be when undefined", () => {
doTest("let myType: undefined;", true);
});

it("should not be when not undefined", () => {
doTest("let myType: string;", false);
});
});

describe(nameof<Type>(t => t.getFlags), () => {
it("should get the type flags", () => {
const {firstType} = getTypeFromText("let myType: number;");
Expand Down Expand Up @@ -467,16 +509,4 @@ describe(nameof(Type), () => {
expect(typeArgs[0].getText()).to.equal("string");
});
});

describe(nameof<Type>(t => t.isUndefinedType), () => {
it("should not be undefined when not undefined", () => {
const {firstType} = getTypeFromText("let myType: string;");
expect(firstType.isUndefinedType()).to.be.false;
});

it("should be undefined when not undefined", () => {
const {firstType} = getTypeFromText("let myType: undefined;");
expect(firstType.isUndefinedType()).to.be.true;
});
});
});

0 comments on commit d7c3c3d

Please sign in to comment.