Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Type.prototype.isAssignableTo #1517

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions deno/ts_morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9856,6 +9856,8 @@ export declare class TypeChecker {
* @param typeReference - Type reference.
*/
getTypeArguments(typeReference: Type): Type<ts.Type>[];
/** Checks if a type is assignable to another type. */
isTypeAssignableTo(sourceType: Type, targetType: Type): boolean;
/** Gets the shorthand assignment value symbol of the provided node. */
getShorthandAssignmentValueSymbol(node: Node): Symbol | undefined;
}
Expand Down Expand Up @@ -10004,6 +10006,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
getSymbol(): Symbol | undefined;
/** Gets the symbol of the type or throws. */
getSymbolOrThrow(message?: string | (() => string)): Symbol;
/** Gets if the type is assignable to another type. */
isAssignableTo(target: Type): boolean;
/** Gets if this is an anonymous type. */
isAnonymous(): boolean;
/** Gets if this is an any type. */
Expand Down
6 changes: 6 additions & 0 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -17876,6 +17876,9 @@ class TypeChecker {
return this.compilerObject.getTypeArguments(typeReference.compilerType)
.map(arg => this.#context.compilerFactory.getType(arg));
}
isTypeAssignableTo(sourceType, targetType) {
return this.compilerObject.isTypeAssignableTo(sourceType.compilerType, targetType.compilerType);
}
#getDefaultTypeFormatFlags(enclosingNode) {
let formatFlags = (TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.NoTruncation | TypeFormatFlags.UseFullyQualifiedType
| TypeFormatFlags.WriteTypeArgumentsOfSignature);
Expand Down Expand Up @@ -18313,6 +18316,9 @@ class Type {
getSymbolOrThrow(message) {
return errors.throwIfNullOrUndefined(this.getSymbol(), message ?? "Expected to find a symbol.");
}
isAssignableTo(target) {
return this._context.typeChecker.isTypeAssignableTo(this, target);
}
isAnonymous() {
return this.#hasObjectFlag(ObjectFlags.Anonymous);
}
Expand Down
12 changes: 12 additions & 0 deletions docs/details/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ There are other ways for accessing a type. For example:
const returnType = functionDeclaration.getReturnType();
```

### Checking type is assignable to another type

Note: Requires ts-morph 22+

To check if a type is assignable to another type, use the `isAssignableTo` method:

```ts
if (stringLitType.isAssignableTo(stringType)) {
// ...
}
```

### Compiler Type

The underlying compiler type can be accessed via:
Expand Down
4 changes: 4 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9856,6 +9856,8 @@ export declare class TypeChecker {
* @param typeReference - Type reference.
*/
getTypeArguments(typeReference: Type): Type<ts.Type>[];
/** Checks if a type is assignable to another type. */
isTypeAssignableTo(sourceType: Type, targetType: Type): boolean;
/** Gets the shorthand assignment value symbol of the provided node. */
getShorthandAssignmentValueSymbol(node: Node): Symbol | undefined;
}
Expand Down Expand Up @@ -10004,6 +10006,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
getSymbol(): Symbol | undefined;
/** Gets the symbol of the type or throws. */
getSymbolOrThrow(message?: string | (() => string)): Symbol;
/** Gets if the type is assignable to another type. */
isAssignableTo(target: Type): boolean;
/** Gets if this is an anonymous type. */
isAnonymous(): boolean;
/** Gets if this is an any type. */
Expand Down
5 changes: 5 additions & 0 deletions packages/ts-morph/src/compiler/tools/TypeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ export class TypeChecker {
.map(arg => this.#context.compilerFactory.getType(arg));
}

/** Checks if a type is assignable to another type. */
isTypeAssignableTo(sourceType: Type, targetType: Type) {
return this.compilerObject.isTypeAssignableTo(sourceType.compilerType, targetType.compilerType);
}

/** @internal */
#getDefaultTypeFormatFlags(enclosingNode?: Node) {
let formatFlags = (TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.NoTruncation | TypeFormatFlags.UseFullyQualifiedType
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 @@ -365,6 +365,13 @@ export class Type<TType extends ts.Type = ts.Type> {
return errors.throwIfNullOrUndefined(this.getSymbol(), message ?? "Expected to find a symbol.");
}

/**
* Gets if the type is assignable to another type.
*/
isAssignableTo(target: Type) {
return this._context.typeChecker.isTypeAssignableTo(this, target);
}

/**
* Gets if this is an anonymous type.
*/
Expand Down
9 changes: 9 additions & 0 deletions packages/ts-morph/src/tests/compiler/type/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,4 +992,13 @@ let unknownType: unknown;
runTest("let myType: string;", undefined);
});
});

describe(nameof<Type>("isAssignableTo"), () => {
it("should be assignable to when so", () => {
const { firstType, sourceFile } = getTypeFromText("let firstType: string; let secondType: 'test';");
const secondType = sourceFile.getVariableDeclarationOrThrow("secondType").getType();
expect(firstType.isAssignableTo(secondType)).to.be.false;
expect(secondType.isAssignableTo(firstType)).to.be.true;
});
});
});