Skip to content

Commit 2d2e914

Browse files
author
Hweinstock
committed
fix(tui): add safe assertions for typechecker
1 parent d460026 commit 2d2e914

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/components/ui/markdown/Markdown.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,17 @@ export const Markdown: React.FC<MarkdownProps> = ({ content, theme = darkTheme }
144144
let i = 0;
145145

146146
while (i < lines.length) {
147-
const line = lines[i];
147+
// Note: assertion is safe based on bound above.
148+
const line = lines[i]!;
148149

149150
// Fenced code block
150151
if (line.startsWith("```")) {
151152
const lang = line.slice(3).trim();
152153
const codeLines: string[] = [];
153154
i++;
154-
while (i < lines.length && !lines[i].startsWith("```")) {
155-
codeLines.push(lines[i]);
155+
// Note: assertions on lines[i] are safe based on first term in the while condition.
156+
while (i < lines.length && !lines[i]!.startsWith("```")) {
157+
codeLines.push(lines[i]!);
156158
i++;
157159
}
158160
elements.push(
@@ -253,10 +255,11 @@ export const Markdown: React.FC<MarkdownProps> = ({ content, theme = darkTheme }
253255
// Ordered list
254256
const olMatch = line.match(/^(\d+)\.\s(.*)/);
255257
if (olMatch) {
258+
// note: if the regex matched, there must be number at olMatch[1], so assertion is safe.
256259
elements.push(
257260
<Box key={i} flexDirection="row">
258-
<Text color={theme.colors.primary}>{` ${olMatch[1]}. `}</Text>
259-
<Text>{renderInline(parseInline(olMatch[2]), theme)}</Text>
261+
<Text color={theme.colors.primary}>{` ${olMatch[1]!}. `}</Text>
262+
<Text>{renderInline(parseInline(olMatch[2] ?? ""), theme)}</Text>
260263
</Box>,
261264
);
262265
i++;

0 commit comments

Comments
 (0)