Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Fix: Interface with templated operation causing crash if defined after use",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
6 changes: 5 additions & 1 deletion packages/compiler/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,11 @@ export function createChecker(program: Program): Checker {
return sym.type as TemplatedType;
}

return checkDeclaredType(sym, decl, mapper) as TemplatedType;
if (sym.flags & SymbolFlags.Member) {
return checkMemberSym(sym, mapper) as TemplatedType;
} else {
return checkDeclaredType(sym, decl, mapper) as TemplatedType;
}
}

/**
Expand Down
29 changes: 29 additions & 0 deletions packages/compiler/test/checker/interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,35 @@ describe("compiler: interfaces", () => {
strictEqual(returnType.name, "int32");
});

it("can instantiate template operation inside templated interface (inverted order)", async () => {
const { Foo, bar } = (await runner.compile(`
alias Bar = MyFoo.bar<int32>;

alias MyFoo = Foo<string>;

@test interface Foo<A> {
@test bar<B>(input: A): B;
}
`)) as {
Foo: Interface;
bar: Operation;
};

strictEqual(Foo.operations.size, 1);
ok(
isTemplateDeclaration(Foo.operations.get("bar")!),
"Operation inside MyFoo interface is still a template"
);

const input = bar.parameters.properties.get("input")!.type;
strictEqual(input.kind, "Scalar" as const);
strictEqual(input.name, "string");

const returnType = bar.returnType;
strictEqual(returnType.kind, "Scalar" as const);
strictEqual(returnType.name, "int32");
});

it("cache templated operations", async () => {
const { Index } = (await runner.compile(`
@test interface Foo<A> {
Expand Down