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

Type Relation API #4360

Open
timotheeguerin opened this issue Sep 6, 2024 · 0 comments
Open

Type Relation API #4360

timotheeguerin opened this issue Sep 6, 2024 · 0 comments
Labels
compiler:core Issues for @typespec/compiler feature New feature or request triaged:core
Milestone

Comments

@timotheeguerin
Copy link
Member

timotheeguerin commented Sep 6, 2024

PR #4357 improve the type relation but leaving this as a separate issue. Need to design the type relation api.

Issues

Currently you have this on the checker

function isTypeAssignableTo(
    source: Entity,
    target: Entity,
    diagnosticTarget: DiagnosticTarget
  ): [boolean, readonly Diagnostic[]];

so one can call

const [related, diagnostics] = program.checker.isTypeAssignable(source, target, source);

There is a few problems with this:

  1. The name doesn't really make sense anymore as you can pass any entity(value, etc.)
  2. You always get this tuple forcing extra wrapping for something that ends up being quite common. Would be nice to have an api that just return true/false without the diagnositcs.
  3. Passing the diagnostic target feels like an advanced scenario specially if you don't care about the diagnostic.

Other question:

  • exposing this on the checker is the right place?
  • handle projection if type is in a projection

Proposal

  1. Rename this to isEntityRelatedTo
  2. Expose isValueOfType
  3. Make the default one without diagnostic and provide a isEntityRelatedToWithDiags
  4. Default the diagnostic target to the target.
function isEntityRelatedTo(source: Entity,  target: Entity): boolean;
function isEntityRelatedToWithDiagnostics(source: Entity,  target: Entity, diagnosticTarget?: DiagnosticTarget):  [boolean, readonly Diagnostic[]];
function isValueOfType(source: Value,  target: Type): boolean;
function isValueOfTypeWithDiagnostics(source: Value,  target: Type, diagnosticTarget?: DiagnosticTarget):  [boolean, readonly Diagnostic[]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:core Issues for @typespec/compiler feature New feature or request triaged:core
Projects
None yet
Development

No branches or pull requests

2 participants