Created
April 29, 2024 15:26
-
-
Save CodeByAidan/a5b3ade9991cbf220a68cc89dec2512a to your computer and use it in GitHub Desktop.
most typescript thing i've wrote, will update when things get worse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Category = { | |
title: string; | |
items: Array<{ name: string }>; | |
}; | |
type Config = { | |
categories: Record<string, Category>; | |
}; | |
function isCategory(obj: unknown): obj is Category { | |
return ( | |
typeof obj === 'object' && | |
obj !== null && | |
'title' in obj && | |
'items' in obj && | |
Array.isArray((obj as Category).items) && | |
(obj as Category).items.every( | |
(item) => typeof item === 'object' && 'name' in item | |
) | |
); | |
} | |
function createWebsite<T extends Config>(config: T): void { | |
Object.entries(config.categories).forEach(([key, category]) => { | |
if (isCategory(category)) { | |
console.log(`Category title: ${category.title}`); | |
category.items.forEach((item, index) => { | |
console.log(`Item ${index + 1}: ${item.name}`); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
javascript equivalent is something like: