Skip to content

Commit

Permalink
Fix issue with @overload in projections (#2582)
Browse files Browse the repository at this point in the history
Fix #2533.
  • Loading branch information
tjprescott authored Oct 18, 2023
1 parent 9d2cf85 commit 7d10458
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Fix issue where using `@overload` could result in incorrect `unassignable` type errors.",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/openapi3",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/openapi3"
}
8 changes: 4 additions & 4 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1162,15 +1162,15 @@ const overloadsOperationKey = createStateSymbol("overloadsOperation");
export function $overload(context: DecoratorContext, target: Operation, overloadBase: Operation) {
// Ensure that the overloaded method arguments are a subtype of the original operation.
const [paramValid, paramDiagnostics] = context.program.checker.isTypeAssignableTo(
target.parameters,
overloadBase.parameters,
target.parameters.projectionBase ?? target.parameters,
overloadBase.parameters.projectionBase ?? overloadBase.parameters,
target
);
if (!paramValid) context.program.reportDiagnostics(paramDiagnostics);

const [returnTypeValid, returnTypeDiagnostics] = context.program.checker.isTypeAssignableTo(
target.returnType,
overloadBase.returnType,
target.returnType.projectionBase ?? target.returnType,
overloadBase.returnType.projectionBase ?? overloadBase.returnType,
target
);
if (!returnTypeValid) context.program.reportDiagnostics(returnTypeDiagnostics);
Expand Down
15 changes: 15 additions & 0 deletions packages/openapi3/test/overloads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,19 @@ describe("openapi3: overloads", () => {
}
`);
});

it("can overload a boolean property with true or false", async () => {
const _ = await openApiFor(`
@test
op someThing(param: boolean): string | int32;
@test
@overload(someThing)
op someStringThing(param: true): string;
@test
@overload(someThing)
op someNumberThing(param: false): int32;
`);
});
});

0 comments on commit 7d10458

Please sign in to comment.