Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9743,6 +9743,11 @@ export declare class TypeChecker {
* @param type - Type to get the apparent type of.
*/
getApparentType(type: Type): Type<ts.Type>;
/**
* Gets the awaited type of a type.
* @param type - Type to get the awaited type of.
*/
getAwaitedType(type: Type): Type<ts.Type> | undefined;
/**
* Gets the constant value of a declaration.
* @param node - Node to get the constant value from.
Expand Down Expand Up @@ -9889,6 +9894,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
getAliasTypeArguments(): Type[];
/** Gets the apparent type. */
getApparentType(): Type;
/** Gets the awaited type. */
getAwaitedType(): Type | undefined;
/** Gets the array element type or throws if it doesn't exist (ex. for `T[]` it would be `T`). */
getArrayElementTypeOrThrow(message?: string | (() => string)): Type<ts.Type>;
/** Gets the array element type or returns undefined if it doesn't exist (ex. for `T[]` it would be `T`). */
Expand Down
9 changes: 9 additions & 0 deletions packages/ts-morph/src/compiler/tools/TypeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export class TypeChecker {
return this.#context.compilerFactory.getType(this.compilerObject.getApparentType(type.compilerType));
}

/**
* Gets the awaited type of a type.
* @param type - Type to get the awaited type of.
*/
getAwaitedType(type: Type) {
const awaitedType = this.compilerObject.getAwaitedType(type.compilerType);
return awaitedType ? this.#context.compilerFactory.getType(awaitedType) : undefined;
}

/**
* Gets the constant value of a declaration.
* @param node - Node to get the constant value from.
Expand Down
7 changes: 7 additions & 0 deletions packages/ts-morph/src/compiler/types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export class Type<TType extends ts.Type = ts.Type> {
return this._context.typeChecker.getApparentType(this);
}

/**
* Gets the awaited type.
*/
getAwaitedType(): Type | undefined {
return this._context.typeChecker.getAwaitedType(this);
}

/**
* Gets the array element type or throws if it doesn't exist (ex. for `T[]` it would be `T`).
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/ts-morph/src/tests/compiler/type/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ let stringWithUndefinedType: string | undefined;
let stringWithNullType: string | null;
let stringWithUndefinedAndNullType: string | undefined | null;
let unknownType: unknown;
let stringWithPromiseType: Promise<string>;
`;
const { sourceFile } = getInfoFromTextWithTypeChecking(text);
const typesByName: { [name: string]: Type } = {};
Expand Down Expand Up @@ -605,6 +606,12 @@ let unknownType: unknown;
});
});

describe(nameof<Type>("getAwaitedType"), () => {
it("should get the awaited type", () => {
expect(typesByName["stringWithPromiseType"].getAwaitedType()!.getText()).to.equal("string");
});
});

describe(nameof<Type>("getCallSignatures"), () => {
it("should return no call signatures when none exist", () => {
expect(typesByName["stringType"].getCallSignatures().length).to.equal(0);
Expand Down