Skip to content

Commit

Permalink
feat: Type.prototype.isAssignableTo (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Mar 9, 2024
1 parent e7799eb commit e8b5727
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions deno/ts_morph.d.ts
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
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
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
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
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
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
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;
});
});
});

0 comments on commit e8b5727

Please sign in to comment.