backend-plugin-api: tighten internal any usage (#33867)

* backend-plugin-api: tighten internal any usage

Replace internal `any` type annotations with proper types:

- Use `'root' | 'plugin'` union in DepsToInstances conditional type
  and createServiceRef implementation signature
- Use Error type guard with object cast fallback in
  isDatabaseConflictError instead of `as any`

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Made-with: Cursor

* Use isError in isDatabaseConflictError

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Made-with: Cursor

* Update changeset wording per review feedback

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Made-with: Cursor

---------

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2026-04-13 14:58:03 +02:00
committed by GitHub
parent d815917053
commit 68c557b381
4 changed files with 15 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-plugin-api': patch
---
Added stricter type checks in `isDatabaseConflictError`.
@@ -136,8 +136,8 @@ export function createServiceRef<
TService,
TInstances extends 'singleton' | 'multiton',
>(
options: ServiceRefOptions<TService, any, TInstances>,
): ServiceRef<TService, any, TInstances> {
options: ServiceRefOptions<TService, 'root' | 'plugin', TInstances>,
): ServiceRef<TService, 'root' | 'plugin', TInstances> {
const { id, scope = 'plugin', multiton = false, defaultFactory } = options;
return {
id,
@@ -14,6 +14,8 @@
* limitations under the License.
*/
import { isError } from '@backstage/errors';
/**
* Tries to deduce whether a thrown error is a database conflict.
*
@@ -23,7 +25,7 @@
* known database engine
*/
export function isDatabaseConflictError(e: unknown) {
const message = (e as any)?.message;
const message = isError(e) ? e.message : undefined;
return (
typeof message === 'string' &&
@@ -56,7 +56,11 @@ type DepsToInstances<
[key in string]: ServiceRef<unknown> | ExtensionPoint<unknown>;
},
> = {
[key in keyof TDeps]: TDeps[key] extends ServiceRef<unknown, any, 'multiton'>
[key in keyof TDeps]: TDeps[key] extends ServiceRef<
unknown,
'root' | 'plugin',
'multiton'
>
? Array<TDeps[key]['T']>
: TDeps[key]['T'];
};