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

Make isTypeAssignableTo public on TypeChecker #56448

Merged
merged 3 commits into from
Dec 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5158,7 +5158,20 @@ export interface TypeChecker {
/** @internal */ getPromiseLikeType(): Type;
/** @internal */ getAsyncIterableType(): Type | undefined;

/** @internal */ isTypeAssignableTo(source: Type, target: Type): boolean;
/**
* Returns true if the "source" type is assignable to the "target" type.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns true if the "source" type is assignable to the "target" type.
* Returns true if the `source` type is assignable to the `target` type.

*
* ```ts
* declare const abcLiteral: ts.Type; // Type of "abc"
* declare const stringType: ts.Type; // Type of string
*
* isTypeAssignableTo(abcLiteral, abcLiteral); // true; "abc" is assignable to "abc"
* isTypeAssignableTo(abcLiteral, stringType); // true; "abc" is assignable to string
* isTypeAssignableTo(stringType, abcLiteral); // false; string is not assignable to "abc"
* isTypeAssignableTo(stringType, stringType); // true; string is assignable to string
* ```
*/
isTypeAssignableTo(source: Type, target: Type): boolean;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a doc; please leave suggestions!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking on what I'd want as a user, it's mostly just knowing what kind of caching I'd have to worry about (since that was brought up by TS folks previously) and examples of what direction the assignability runs in. Vaguely:

/**
 * Performs an assignability check, including using persistent caches.
 * @returns Whether `source` is assignable to `type`.
 * @example
 * ```ts
 * declare const literal: ts.Type; // Type of "abc"
 * declare const primitive: ts.Type; // Type of string
 * isTypeAssignableTo(literal, literal); // true
 * isTypeAssignableTo(literal, primitive); // true
 * isTypeAssignableTo(primitive, literal); // false
 * isTypeAssignableTo(primitive, primitive); // true
 * ```

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added something similar to the above.

/** @internal */ createAnonymousType(symbol: Symbol | undefined, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], indexInfos: IndexInfo[]): Type;
/** @internal */ createSignature(
declaration: SignatureDeclaration | undefined,
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6832,6 +6832,20 @@ declare namespace ts {
* is `never`. Instead, use `type.flags & TypeFlags.Never`.
*/
getNeverType(): Type;
/**
* Returns true if the "source" type is assignable to the "target" type.
*
* ```ts
* declare const abcLiteral: ts.Type; // Type of "abc"
* declare const stringType: ts.Type; // Type of string
*
* isTypeAssignableTo(abcLiteral, abcLiteral); // true; "abc" is assignable to "abc"
* isTypeAssignableTo(abcLiteral, stringType); // true; "abc" is assignable to string
* isTypeAssignableTo(stringType, abcLiteral); // false; string is not assignable to "abc"
* isTypeAssignableTo(stringType, stringType); // true; string is assignable to string
* ```
*/
isTypeAssignableTo(source: Type, target: Type): boolean;
/**
* True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts.
* This function will _not_ return true if passed a type which
Expand Down
Loading