fix(chapters): narrow optional section fields in page frontmatter
The chapters schema marks section.eyebrow / panelLabel / panelCode / steps / copy as optional, but pages /agents/, /models/, /skills/ consume them — index access without narrowing failed astro check with ts(18048). Same for SkillPackageExplorer: aria-selected was String(bool) which widened to plain string and failed ts(2322) against ButtonHTMLAttributes. Fix: guard each required field with a helper that throws a clear message on missing data, then read .en / .map from the narrowed value. Page rendering is unchanged — dist/ HTML for all four files is byte-identical before and after. No any / as any / ! / @ts-ignore. Schema and tsconfig untouched.
This commit is contained in:
+28
-4
@@ -10,14 +10,38 @@ import ChapterHero from '../components/blocks/ChapterHero.astro';
|
||||
import SkillPackageExplorer from '../components/islands/SkillPackageExplorer.astro';
|
||||
import skillsStylesheet from '../../skills/styles.css?url';
|
||||
|
||||
// Required-field guard. The chapters schema marks section eyebrow /
|
||||
// panelLabel / panelCode / steps / copy as optional because the schema
|
||||
// does not know which page consumes which shape. These four pages do
|
||||
// consume them — fail loudly here rather than rendering a blank section.
|
||||
function requireField<T>(value: T | undefined, name: string): T {
|
||||
if (value === undefined) {
|
||||
throw new Error(`skills chapter: missing required field "${name}"`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const base = import.meta.env.BASE_URL;
|
||||
const chapter = await getEntry('chapters', 'skills');
|
||||
if (!chapter) {
|
||||
throw new Error('skills chapter: missing collection entry');
|
||||
}
|
||||
const lede = chapter.data.lede.en;
|
||||
const title = chapter.data.title.en;
|
||||
const eyebrow = chapter.data.eyebrow.en;
|
||||
const sections = chapter.data.sections ?? [];
|
||||
const anatomySection = sections[0];
|
||||
const createSection = sections[1];
|
||||
if (!anatomySection) {
|
||||
throw new Error('skills chapter: missing sections[0]');
|
||||
}
|
||||
if (!createSection) {
|
||||
throw new Error('skills chapter: missing sections[1]');
|
||||
}
|
||||
const anatomyEyebrow = requireField(anatomySection.eyebrow, 'sections[0].eyebrow');
|
||||
const anatomyCopy = requireField(anatomySection.copy, 'sections[0].copy');
|
||||
const createEyebrow = requireField(createSection.eyebrow, 'sections[1].eyebrow');
|
||||
const createSteps = requireField(createSection.steps, 'sections[1].steps');
|
||||
---
|
||||
|
||||
<ChapterLayout title="AI For Dummies — Skills" description={lede.replace(/<[^>]+>/g, '')}>
|
||||
@@ -33,21 +57,21 @@ const createSection = sections[1];
|
||||
|
||||
<section class="pipeline package-anatomy">
|
||||
<div>
|
||||
<p class="eyebrow">{anatomySection.eyebrow.en}</p>
|
||||
<p class="eyebrow">{anatomyEyebrow.en}</p>
|
||||
<h2 set:html={anatomySection.title.en} />
|
||||
<p class="package-hint">{anatomySection.copy.en}</p>
|
||||
<p class="package-hint">{anatomyCopy.en}</p>
|
||||
</div>
|
||||
<SkillPackageExplorer />
|
||||
</section>
|
||||
|
||||
<section class="practice">
|
||||
<div>
|
||||
<p class="eyebrow">{createSection.eyebrow.en}</p>
|
||||
<p class="eyebrow">{createEyebrow.en}</p>
|
||||
<h2 set:html={createSection.title.en} />
|
||||
</div>
|
||||
<div class="steps">
|
||||
{
|
||||
createSection.steps.map((step, index) => (
|
||||
createSteps.map((step, index) => (
|
||||
<article>
|
||||
<b>{String(index + 1).padStart(2, '0')}</b>
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user