Skip to content

Commit 415a850

Browse files
authored
feat: TypeChecker - add getAwaitedType (#1643)
1 parent e43677f commit 415a850

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

packages/ts-morph/lib/ts-morph.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9743,6 +9743,11 @@ export declare class TypeChecker {
97439743
* @param type - Type to get the apparent type of.
97449744
*/
97459745
getApparentType(type: Type): Type<ts.Type>;
9746+
/**
9747+
* Gets the awaited type of a type (ex. `Promise<string>` -> `string`).
9748+
* @param type - Type to get the awaited type of.
9749+
*/
9750+
getAwaitedType(type: Type): Type<ts.Type> | undefined;
97469751
/**
97479752
* Gets the constant value of a declaration.
97489753
* @param node - Node to get the constant value from.
@@ -9889,6 +9894,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
98899894
getAliasTypeArguments(): Type[];
98909895
/** Gets the apparent type. */
98919896
getApparentType(): Type;
9897+
/** Gets the awaited type. */
9898+
getAwaitedType(): Type | undefined;
98929899
/** Gets the array element type or throws if it doesn't exist (ex. for `T[]` it would be `T`). */
98939900
getArrayElementTypeOrThrow(message?: string | (() => string)): Type<ts.Type>;
98949901
/** Gets the array element type or returns undefined if it doesn't exist (ex. for `T[]` it would be `T`). */

packages/ts-morph/src/compiler/tools/TypeChecker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ export class TypeChecker {
5252
return this.#context.compilerFactory.getType(this.compilerObject.getApparentType(type.compilerType));
5353
}
5454

55+
/**
56+
* Gets the awaited type of a type (ex. `Promise<string>` -> `string`).
57+
* @param type - Type to get the awaited type of.
58+
*/
59+
getAwaitedType(type: Type) {
60+
const awaitedType = this.compilerObject.getAwaitedType(type.compilerType);
61+
return awaitedType ? this.#context.compilerFactory.getType(awaitedType) : undefined;
62+
}
63+
5564
/**
5665
* Gets the constant value of a declaration.
5766
* @param node - Node to get the constant value from.

packages/ts-morph/src/compiler/types/Type.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ export class Type<TType extends ts.Type = ts.Type> {
6767
return this._context.typeChecker.getApparentType(this);
6868
}
6969

70+
/**
71+
* Gets the awaited type.
72+
*/
73+
getAwaitedType(): Type | undefined {
74+
return this._context.typeChecker.getAwaitedType(this);
75+
}
76+
7077
/**
7178
* Gets the array element type or throws if it doesn't exist (ex. for `T[]` it would be `T`).
7279
*/

packages/ts-morph/src/tests/compiler/type/typeTests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ let stringWithUndefinedType: string | undefined;
6969
let stringWithNullType: string | null;
7070
let stringWithUndefinedAndNullType: string | undefined | null;
7171
let unknownType: unknown;
72+
let stringWithPromiseType: Promise<string>;
7273
`;
7374
const { sourceFile } = getInfoFromTextWithTypeChecking(text);
7475
const typesByName: { [name: string]: Type } = {};
@@ -605,6 +606,12 @@ let unknownType: unknown;
605606
});
606607
});
607608

609+
describe(nameof<Type>("getAwaitedType"), () => {
610+
it("should get the awaited type", () => {
611+
expect(typesByName["stringWithPromiseType"].getAwaitedType()!.getText()).to.equal("string");
612+
});
613+
});
614+
608615
describe(nameof<Type>("getCallSignatures"), () => {
609616
it("should return no call signatures when none exist", () => {
610617
expect(typesByName["stringType"].getCallSignatures().length).to.equal(0);

0 commit comments

Comments
 (0)