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

Improvements to type relation errors #4357

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

Improvements to type relation errors: Show stack when it happens in a nested property otherwise show up in the correct location.
12 changes: 4 additions & 8 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5649,7 +5649,7 @@ export function createChecker(program: Program): Checker {
function checkArgumentAssignable(
argumentType: Type | Value | IndeterminateEntity,
parameterType: Entity,
diagnosticTarget: DiagnosticTarget
diagnosticTarget: Entity | Node
): boolean {
const [valid] = relation.isTypeAssignableTo(argumentType, parameterType, diagnosticTarget);
if (!valid) {
Expand Down Expand Up @@ -7420,7 +7420,7 @@ export function createChecker(program: Program): Checker {
function checkTypeOfValueMatchConstraint(
source: Entity,
constraint: CheckValueConstraint,
diagnosticTarget: DiagnosticTarget
diagnosticTarget: Entity | Node
): boolean {
const [related, diagnostics] = relation.isTypeAssignableTo(
source,
Expand Down Expand Up @@ -7455,7 +7455,7 @@ export function createChecker(program: Program): Checker {
function checkTypeAssignable(
source: Entity | IndeterminateEntity,
target: Entity,
diagnosticTarget: DiagnosticTarget
diagnosticTarget: Entity | Node
): boolean {
const [related, diagnostics] = relation.isTypeAssignableTo(source, target, diagnosticTarget);
if (!related) {
Expand All @@ -7464,11 +7464,7 @@ export function createChecker(program: Program): Checker {
return related;
}

function checkValueOfType(
source: Value,
target: Type,
diagnosticTarget: DiagnosticTarget
): boolean {
function checkValueOfType(source: Value, target: Type, diagnosticTarget: Entity | Node): boolean {
const [related, diagnostics] = relation.isValueOfType(source, target, diagnosticTarget);
if (!related) {
reportCheckerDiagnostics(diagnostics);
Expand Down
9 changes: 7 additions & 2 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,13 @@ const diagnostics = {
unassignable: {
severity: "error",
messages: {
default: paramMessage`Type '${"value"}' is not assignable to type '${"targetType"}'`,
withDetails: paramMessage`Type '${"sourceType"}' is not assignable to type '${"targetType"}'\n ${"details"}`,
default: paramMessage`Type '${"sourceType"}' is not assignable to type '${"targetType"}'`,
},
},
"property-unassignable": {
severity: "error",
messages: {
default: paramMessage`Types of property '${"propName"}' are incompatible`,
},
},
"property-required": {
Expand Down
Loading
Loading