Skip to content

Commit

Permalink
[core] Widen signature of isTemplateDeclaration/isTemplateDeclaration…
Browse files Browse the repository at this point in the history
…OrInstance (#5527)

As described in #5447

This changes the signature of `isTemplateDeclaration` and
`isTemplateDeclarationOrInstance` so that they accept any `Type` rather
than just `TemplatedType`. This makes them consistent with
`isTemplateInstance`.

`isTemplateDeclarationOrInstance` also gains a type guard to refine the
input type when it returns true.

Closes #5447

---------

Co-authored-by: Will Temple <will@wtemple.net>
  • Loading branch information
witemple-msft and willmtemple authored Jan 8, 2025
1 parent 49387ea commit 1536482
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

Widen type signature of `isTemplateDeclaration` and `isTemplateDeclarationOrInstance` to accept any type and assert the return type.
8 changes: 5 additions & 3 deletions packages/compiler/src/core/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function isDeclaredType(type: Type): boolean {
* Resolve if the type is a template type declaration(Non initialized template type).
*/
export function isTemplateDeclaration(
type: TemplatedType,
type: Type,
): type is TemplatedType & { node: TemplateDeclarationNode } {
if (type.node === undefined) {
return false;
Expand All @@ -129,14 +129,16 @@ export function isTemplateDeclaration(
return (
node.templateParameters &&
node.templateParameters.length > 0 &&
type.templateMapper === undefined
(type as TemplatedType).templateMapper === undefined
);
}

/**
* Resolve if the type was created from a template type or is a template type declaration.
*/
export function isTemplateDeclarationOrInstance(type: TemplatedType): boolean {
export function isTemplateDeclarationOrInstance(
type: Type,
): type is TemplatedType & { node: TemplateDeclarationNode } {
if (type.node === undefined) {
return false;
}
Expand Down

0 comments on commit 1536482

Please sign in to comment.