Skip to content

Commit

Permalink
feat: Add Type#isUnknown()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Feb 18, 2019
1 parent d10877f commit 30bb042
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/details/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type.isTuple();
type.isUnion();
type.isIntersection();
type.isUnionOrIntersection();
type.isUnknown();
type.isNull();
type.isUndefined();
```
Expand Down
6 changes: 5 additions & 1 deletion lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ export declare class WriterFunctions {
* Gets a writer function for writing the provided object as an object literal expression.
* @param obj - Object to write.
*/
object(obj: {
static object(obj: {
[key: string]: WriterFunctionOrValue | undefined;
}): WriterFunction;
/** Gets a writer function for writing an object type. */
Expand Down Expand Up @@ -9855,6 +9855,10 @@ export declare class Type<TType extends ts.Type = ts.Type> {
* Gets if this is a union or intersection type.
*/
isUnionOrIntersection(): boolean;
/**
* Gets if this is the unknown type.
*/
isUnknown(): boolean;
/**
* Gets if this is the null type.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ export class Type<TType extends ts.Type = ts.Type> {
return this.compilerType.isUnionOrIntersection();
}

/**
* Gets if this is the unknown type.
*/
isUnknown() {
return this._hasTypeFlag(TypeFlags.Unknown);
}

/**
* Gets if this is the null type.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/tests/compiler/type/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ let emptyObjectType: { };
let stringWithUndefinedType: string | undefined;
let stringWithNullType: string | null;
let stringWithUndefinedAndNullType: string | undefined | null;
let unknownType: unknown;
`;
const { sourceFile } = getInfoFromTextWithTypeChecking(text);
const typesByName: { [name: string]: Type; } = {};
Expand Down Expand Up @@ -415,6 +416,20 @@ let stringWithUndefinedAndNullType: string | undefined | null;
});
});

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

it("should get when it is", () => {
doTest("unknownType", true);
});

it("should get when it's not", () => {
doTest("anyType", false);
});
});

describe(nameof<Type>(t => t.getFlags), () => {
it("should get the type flags", () => {
expect(typesByName["numberType"].getFlags()).to.equal(TypeFlags.Number);
Expand Down

0 comments on commit 30bb042

Please sign in to comment.