Skip to content

Commit

Permalink
feat: add Type#isNever()
Browse files Browse the repository at this point in the history
Closes #1303
  • Loading branch information
dsherret committed Sep 3, 2022
1 parent f1d5c43 commit ac0db0d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deno/ts_morph.d.ts
Expand Up @@ -10123,6 +10123,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
isAnonymous(): boolean;
/** Gets if this is an any type. */
isAny(): boolean;
/** Gets if this is a never type. */
isNever(): boolean;
/** Gets if this is an array type. */
isArray(): boolean;
/** Gets if this is a readonly array type. */
Expand Down
3 changes: 3 additions & 0 deletions deno/ts_morph.js
Expand Up @@ -17980,6 +17980,9 @@ class Type {
isAny() {
return this._hasTypeFlag(TypeFlags.Any);
}
isNever() {
return this._hasTypeFlag(TypeFlags.Never);
}
isArray() {
const symbol = this.getSymbol();
if (symbol == null)
Expand Down
2 changes: 2 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Expand Up @@ -10123,6 +10123,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
isAnonymous(): boolean;
/** Gets if this is an any type. */
isAny(): boolean;
/** Gets if this is a never type. */
isNever(): boolean;
/** Gets if this is an array type. */
isArray(): boolean;
/** Gets if this is a readonly array type. */
Expand Down
7 changes: 7 additions & 0 deletions packages/ts-morph/src/compiler/types/Type.ts
Expand Up @@ -379,6 +379,13 @@ export class Type<TType extends ts.Type = ts.Type> {
return this._hasTypeFlag(TypeFlags.Any);
}

/**
* Gets if this is a never type.
*/
isNever() {
return this._hasTypeFlag(TypeFlags.Never);
}

/**
* Gets if this is an array type.
*/
Expand Down
16 changes: 16 additions & 0 deletions packages/ts-morph/src/tests/compiler/type/typeTests.ts
Expand Up @@ -54,6 +54,7 @@ let genericArrayType: Array<string>;
let arrayType: string[];
let readonlyArrayType: readonly string[];
let explicitReadonlyArrayType: ReadonlyArray<string>;
let neverType: never;
let arrayTypeOfTuples: [string][];
let undefinedType: undefined;
let classType: MyClass;
Expand Down Expand Up @@ -139,6 +140,21 @@ let unknownType: unknown;
});
});

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

it("should be when never", () => {
doTest("neverType", true);
});

it("should not be when not never", () => {
doTest("anyType", false);
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 ac0db0d

Please sign in to comment.