Skip to content

A platform for community discussion. Free, open, simple.

DEV: Tighten block types for strict mode (#44354)

Sérgio Saquetim607b43194c2b
diff --git a/frontend/discourse/app/blocks/types.ts b/frontend/discourse/app/blocks/types.ts
index cea0ebd8b4c..e1f9f68d86b 100644
--- a/frontend/discourse/app/blocks/types.ts
+++ b/frontend/discourse/app/blocks/types.ts
@@ -80,9 +80,10 @@ export interface ArgSchema {
/**
* Required class (or a `"model:*"` string, resolved via the registry) for an
- * `object` argument's value. Mutually exclusive with `properties`.
+ * `object` argument's value. Mutually exclusive with `properties`. Any class is
+ * accepted; `never[]` is what lets a constructor of any shape fit.
*/
- instanceOf?: (abstract new (...args: unknown[]) => object) | string;
+ instanceOf?: (abstract new (...args: never[]) => object) | string;
/**
* Human-readable class name used in error messages when `instanceOf` is a
diff --git a/frontend/discourse/app/lib/blocks/-internals/entry-processing.ts b/frontend/discourse/app/lib/blocks/-internals/entry-processing.ts
index 342a77bc4af..7500bc5989c 100644
--- a/frontend/discourse/app/lib/blocks/-internals/entry-processing.ts
+++ b/frontend/discourse/app/lib/blocks/-internals/entry-processing.ts
@@ -97,7 +97,7 @@ function getOrCreateLeafBlockComponent(
): ChildBlockResult {
const { key } = debugContext;
const cachedEntry = cache.get(key);
- const hasChildren = entry.children?.length > 0;
+ const hasChildren = (entry.children?.length ?? 0) > 0;
// Only cache leaf blocks (no children). Container blocks are always recreated
// to ensure their children reflect current visibility state.
@@ -226,7 +226,7 @@ export function processBlockEntries({
// creates the child components at the root level, so containers receive
// pre-processed children via the `@children` arg instead of raw entries.
let processedChildren: ChildBlockResult[] | undefined;
- if (isContainer && entry.children?.length) {
+ if (containerPath !== undefined && entry.children?.length) {
processedChildren = processBlockEntries({
entries: entry.children,
cache, // Same root cache for all levels.
diff --git a/frontend/discourse/app/lib/blocks/-internals/registry/helpers.ts b/frontend/discourse/app/lib/blocks/-internals/registry/helpers.ts
index eb0d98c1ae8..45607e076c2 100644
--- a/frontend/discourse/app/lib/blocks/-internals/registry/helpers.ts
+++ b/frontend/discourse/app/lib/blocks/-internals/registry/helpers.ts
@@ -40,7 +40,7 @@ interface AssertRegistryNotFrozenOptions {
entityType: string;
/** Name of the entity being registered. */
- entityName: string;
+ entityName: string | undefined;
}
/**
diff --git a/frontend/discourse/app/lib/blocks/-internals/types.ts b/frontend/discourse/app/lib/blocks/-internals/types.ts
index 202f59b85e1..e83cc11c5f2 100644
--- a/frontend/discourse/app/lib/blocks/-internals/types.ts
+++ b/frontend/discourse/app/lib/blocks/-internals/types.ts
@@ -10,9 +10,11 @@ import type { ComponentLike } from "@glint/template";
* is only ever stored, compared, and curried as an opaque token here — its
* component-ness is enforced at runtime by the `@block` decorator's
* `instanceof Component` check — a permissive construct signature is the
- * idiomatic way to accept "any block component class".
+ * idiomatic way to accept "any block component class". Its parameters are
+ * `never[]` because they are checked contravariantly: only `never` fits every
+ * constructor. Nothing constructs a block class through this type.
*/
-export type BlockClass = abstract new (...args: unknown[]) => object;
+export type BlockClass = abstract new (...args: never[]) => object;
/**
* A factory that lazily resolves to a block class (or a module whose default
diff --git a/frontend/discourse/app/lib/blocks/-internals/validation/args.ts b/frontend/discourse/app/lib/blocks/-internals/validation/args.ts
index 919c799553d..423b0757829 100644
--- a/frontend/discourse/app/lib/blocks/-internals/validation/args.ts
+++ b/frontend/discourse/app/lib/blocks/-internals/validation/args.ts
@@ -44,7 +44,7 @@ export interface ArgErrorContext {
* only known at runtime.
*/
interface ModelRegistryOwner {
- factoryFor?: (fullName: string) => { class?: unknown } | undefined;
+ factoryFor?: (fullName: `model:${string}`) => { class?: unknown } | undefined;
}
/** Options accepted by `validateArgValue` and `validateArgsAgainstSchema`. */
diff --git a/frontend/discourse/app/static/dev-tools/styleguide/button.gts b/frontend/discourse/app/static/dev-tools/styleguide/button.gts
index d6a854e99d3..b9056f1e9a7 100644
--- a/frontend/discourse/app/static/dev-tools/styleguide/button.gts
+++ b/frontend/discourse/app/static/dev-tools/styleguide/button.gts
@@ -1,11 +1,15 @@
import Component from "@glimmer/component";
import { service } from "@ember/service";
import getURL from "discourse/lib/get-url";
+import type Site from "discourse/models/site";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
export default class StyleguideButton extends Component {
- @service site;
+ // TODO(devxp-typescript-pending): `can_see_styleguide` is added to the site
+ // payload by a plugin serializer, which the `Site` model cannot declare. Drop
+ // this once plugins have a typed way to extend site fields.
+ @service declare site: Site & { can_see_styleguide?: boolean };
<template>
{{#if this.site.can_see_styleguide}}
diff --git a/frontend/discourse/tests/helpers/block-testing.gjs b/frontend/discourse/tests/helpers/block-testing.gjs
index 0a4726fc481..9960d72e9fa 100644
--- a/frontend/discourse/tests/helpers/block-testing.gjs
+++ b/frontend/discourse/tests/helpers/block-testing.gjs
@@ -298,6 +298,7 @@ export { debugHooks };
* assert.strictEqual(capturedGhosts[0].name, "my-block");
*/
export function setupGhostCapture({ enabled = true } = {}) {
+ /** @type {Array<{name: string, failureType: string, failureReason: string|undefined}>} */
const capturedGhosts = [];
debugHooks.setCallback(DEBUG_CALLBACK.GHOST_BLOCKS, () => enabled);