Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 6e54fd9

Browse files
committed
add types for language
1 parent 6de4b18 commit 6e54fd9

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/components/guides/GuideItem.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react'
33
import { cn } from '@/lib/utils'
44
import Link from 'next/link'
55
import { LanguageIcon } from '../icons/LanguageIcon'
6+
import { Language } from '@/lib/constants'
67

78
interface Props {
89
guide: Doc
@@ -53,7 +54,11 @@ export const GuideItem: React.FC<Props> = ({ guide, featured }) => {
5354
{guide.languages?.length ? (
5455
<div className="ml-auto flex items-center gap-x-2">
5556
{guide.languages.map((lang) => (
56-
<LanguageIcon key={lang} name={lang} className="size-6" />
57+
<LanguageIcon
58+
key={lang}
59+
name={lang as Language}
60+
className="size-6"
61+
/>
5762
))}
5863
</div>
5964
) : null}

src/components/icons/LanguageIcon.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import PythonColorLogo from './PythonLogoColour'
55
import GoColorLogo from './GoLogoColour'
66
import DartLogoNoTextColour from './DartLogoNoTextColour'
77
import { cn } from '@/lib/utils'
8+
import type { Language } from '@/lib/constants'
89

910
interface LanguageIconProps {
10-
name: string
11+
name: Language
1112
className?: string
1213
}
1314

14-
const icons: Record<string, React.FC<{ className: string }>> = {
15+
const icons: Record<Language, React.FC<{ className: string }>> = {
1516
javascript: JavaScriptLogoColour,
1617
typescript: TypeScriptLogoColour,
1718
python: PythonColorLogo,
@@ -25,9 +26,5 @@ export const LanguageIcon: React.FC<LanguageIconProps> = ({
2526
}) => {
2627
const IconComponent = icons[name]
2728

28-
if (!IconComponent) {
29-
return <span>Unknown language</span>
30-
}
31-
3229
return <IconComponent className={cn(`size-8`, className)} />
3330
}

src/lib/constants.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ export const LANGUAGE_LABEL_MAP: Record<string, string> = {
99
csharp: 'C#',
1010
}
1111

12-
export const languages = ['javascript', 'typescript', 'python', 'go', 'dart']
12+
export const languages = [
13+
'javascript',
14+
'typescript',
15+
'python',
16+
'go',
17+
'dart',
18+
] as const
19+
20+
export type Language = (typeof languages)[number]
1321

1422
export const isMobile =
1523
typeof navigator !== 'undefined' &&

0 commit comments

Comments
 (0)