Skip to content

Commit

Permalink
feat: add Type#isReadonlyArray() and Type#isArray() also includes…
Browse files Browse the repository at this point in the history
… readonly arrays

Closes #1306
Closes #1305
  • Loading branch information
dsherret committed Sep 3, 2022
1 parent 8a87a1b commit f1d5c43
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
2 changes: 2 additions & 0 deletions deno/ts_morph.d.ts
Expand Up @@ -10125,6 +10125,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
isAny(): boolean;
/** Gets if this is an array type. */
isArray(): boolean;
/** Gets if this is a readonly array type. */
isReadonlyArray(): boolean;
/** Gets if this is a template literal type. */
isTemplateLiteral(): boolean;
/** Gets if this is a boolean type. */
Expand Down
8 changes: 7 additions & 1 deletion deno/ts_morph.js
Expand Up @@ -17984,7 +17984,13 @@ class Type {
const symbol = this.getSymbol();
if (symbol == null)
return false;
return symbol.getName() === "Array" && this.getTypeArguments().length === 1;
return (symbol.getName() === "Array" || symbol.getName() === "ReadonlyArray") && this.getTypeArguments().length === 1;
}
isReadonlyArray() {
const symbol = this.getSymbol();
if (symbol == null)
return false;
return symbol.getName() === "ReadonlyArray" && this.getTypeArguments().length === 1;
}
isTemplateLiteral() {
return this._hasTypeFlag(TypeFlags.TemplateLiteral);
Expand Down
2 changes: 2 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Expand Up @@ -10125,6 +10125,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
isAny(): boolean;
/** Gets if this is an array type. */
isArray(): boolean;
/** Gets if this is a readonly array type. */
isReadonlyArray(): boolean;
/** Gets if this is a template literal type. */
isTemplateLiteral(): boolean;
/** Gets if this is a boolean type. */
Expand Down
14 changes: 13 additions & 1 deletion packages/ts-morph/src/compiler/types/Type.ts
Expand Up @@ -386,7 +386,19 @@ export class Type<TType extends ts.Type = ts.Type> {
const symbol = this.getSymbol();
if (symbol == null)
return false;
return symbol.getName() === "Array" && this.getTypeArguments().length === 1;
// this is not bulletproof and should be improved
return (symbol.getName() === "Array" || symbol.getName() === "ReadonlyArray") && this.getTypeArguments().length === 1;
}

/**
* Gets if this is a readonly array type.
*/
isReadonlyArray() {
const symbol = this.getSymbol();
if (symbol == null)
return false;
// this is not bulletproof and should be improved
return symbol.getName() === "ReadonlyArray" && this.getTypeArguments().length === 1;
}

/**
Expand Down
48 changes: 47 additions & 1 deletion packages/ts-morph/src/tests/compiler/type/typeTests.ts
Expand Up @@ -52,6 +52,8 @@ let tupleType: [string];
let tupleTypeMultiple: [string, number];
let genericArrayType: Array<string>;
let arrayType: string[];
let readonlyArrayType: readonly string[];
let explicitReadonlyArrayType: ReadonlyArray<string>;
let arrayTypeOfTuples: [string][];
let undefinedType: undefined;
let classType: MyClass;
Expand Down Expand Up @@ -415,7 +417,7 @@ let unknownType: unknown;
doTest("tupleTypeMultiple", true);
});

it("should not be when not an array", () => {
it("should not be when an array", () => {
doTest("arrayType", false);
});

Expand All @@ -428,6 +430,50 @@ let unknownType: unknown;
});
});

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

it("should be when an array", () => {
doTest("arrayType", true);
});

it("should be when a readonly array", () => {
doTest("readonlyArrayType", true);
});

it("should be when explicitly a readonly array", () => {
doTest("explicitReadonlyArrayType", true);
});

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

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

it("should not be when an array", () => {
doTest("arrayType", false);
});

it("should be when a readonly array", () => {
doTest("readonlyArrayType", true);
});

it("should be when explicitly a readonly array", () => {
doTest("explicitReadonlyArrayType", true);
});

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

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

0 comments on commit f1d5c43

Please sign in to comment.