more typescript stuff and new post

This commit is contained in:
2024-02-21 20:26:19 -05:00
parent b4fd4e3403
commit d1bd9388fc
9 changed files with 117 additions and 52 deletions
+7
View File
@@ -374,6 +374,13 @@ declare module 'astro:content' {
collection: "blog"; collection: "blog";
data: InferEntrySchema<"blog"> data: InferEntrySchema<"blog">
} & { render(): Render[".md"] }; } & { render(): Render[".md"] };
"2024/doctor-type-love.md": {
id: "2024/doctor-type-love.md";
slug: "2024/doctor-type-love";
body: string;
collection: "blog";
data: InferEntrySchema<"blog">
} & { render(): Render[".md"] };
"2024/my-backup-solution.md": { "2024/my-backup-solution.md": {
id: "2024/my-backup-solution.md"; id: "2024/my-backup-solution.md";
slug: "2024/my-backup-solution"; slug: "2024/my-backup-solution";
+1 -1
View File
@@ -1,5 +1,5 @@
import { defineConfig } from 'astro/config'; import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions'; import netlify from '@astrojs/netlify';
// https://astro.build/config // https://astro.build/config
import mdx from '@astrojs/mdx'; import mdx from '@astrojs/mdx';
BIN
View File
Binary file not shown.
+24
View File
@@ -0,0 +1,24 @@
import { Tag } from 'src/types';
const renderFile = (
title: string,
date: string,
tags: string[],
md: string
) => `
---
title: ${title}
pubDate: ${date}
tags: ['${tags.join("', '")}']
---
${md}
`;
const newPost = () => {
const tagArr = Object.values(Tag);
console.log(tagArr);
};
newPost();
+28 -26
View File
@@ -1,28 +1,30 @@
{ {
"name": "ghall.blog", "name": "ghall.blog",
"type": "module", "type": "module",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "astro dev", "dev": "astro dev",
"start": "astro dev", "start": "astro dev",
"build": "astro build", "build": "astro build",
"preview": "astro preview", "preview": "astro preview",
"astro": "astro" "astro": "astro",
}, "new-post": "bun ./newPost.ts"
"dependencies": { },
"@astrojs/mdx": "2.1.0", "dependencies": {
"@astrojs/netlify": "5.0.0", "@astrojs/mdx": "2.1.0",
"@astrojs/rss": "4.0.4", "@astrojs/netlify": "5.0.0",
"astro": "4.2.6", "@astrojs/rss": "4.0.4",
"date-fns": "^3.3.1", "astro": "4.2.6",
"date-fns-tz": "^1.3.7", "date-fns": "^3.3.1",
"markdown-it": "^13.0.1", "date-fns-tz": "^1.3.7",
"sanitize-html": "^2.9.0", "markdown-it": "^13.0.1",
"sharp": "^0.33.2" "sanitize-html": "^2.9.0",
}, "sharp": "^0.33.2"
"devDependencies": { },
"prettier": "^2.8.1", "devDependencies": {
"prettier-plugin-astro": "^0.7.0" "inquirer": "^9.2.15",
} "prettier": "^2.8.1",
"prettier-plugin-astro": "^0.7.0"
}
} }
+2 -5
View File
@@ -1,10 +1,7 @@
--- ---
const { pathname } = Astro.url; import type { NavLink } from 'src/types';
type NavLink = { const { pathname } = Astro.url;
label: string;
path: string;
};
const navLinks: NavLink[] = [ const navLinks: NavLink[] = [
{ label: '📝 Blog', path: '/' }, { label: '📝 Blog', path: '/' },
+28
View File
@@ -0,0 +1,28 @@
---
title: 'How I Learned to Stop Worrying and Love TypeScript'
pubDate: '2/21/24'
tags: ['Learning', 'Programming']
---
One of the first steps I took on my post-boot camp webdev journey was to learn TypeScript. It wasn't something that was coveredthough I wish it had been, I'll get into why in a secondbut it was something we were encouraged to study on our own. I immediately got frustrated, and determined that there was no point. Why would I need to go through the trouble of assigning types to my variables when it was all just getting compiled into vanilla JavaScript anyway?
Two major things happened since then that changed my tune; I learned Swift, and I started using Bun for personal projects.
First, I'll tackle Swift.
Learning Swift was both pretty straightforward coming from JavaScript. The syntax, while pretty different, has some concepts that were familiar to me. By the same token though, Swift is a statically-typed[^1] language and I found that difficult to wrap my head around at firstI had, after all, given up on TypeScript in frustration.
As I got more comfortable with Swift, I starte to miss the benefits of static typing when working in JavaScript. The ability to just tell my code "hey, this variable here is an integer" and have my code crash, or even just fail to even compile, with a super specific error when I inevitably do something stupid and try to treat that variable as a string is infinitely less frustrating than the alternative.
Secondly, there's Bun.
Bun is amazing, and I could write an entire post about how much I love working with bun. Heck, maybe I will, but that's something for another day...
The main thing about Bun that's relevant to my TypeScript journey is the fact that it can natively interpret TypeScript without having to compile to JavaScript. That means that right out of the box, with no dependencies, Bun can just run your TypeScript code just as easily as it can run JavaScript.[^2]
While any code getting shipped to the browser will still need to be compiled to vanilla JavaScript, you could, as I understand it, theoretically write your entire back-end in TypeScript and just skip the build step. 🤯
I started off being annoyed with using types in my code because, to be quite frank, they are annoying. They'll make your code throw errors whenever you try to multiply a string, or something silly like that. But that's part of the beauty I've come to appreciate. They're there to help you write better code, and to point you in the right direction when something inevitably goes wrong.
[^1]: [Here](https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages) is a StackOverflow thread on the difference between dynamic and static typing put far better than I ever could explain it
[^2]: As an example, I rewrote [my CLI clone of Wordle](https://github.com/ghall89/wordle-cli) in TypeScript
+2 -20
View File
@@ -1,28 +1,10 @@
import { z, defineCollection } from 'astro:content'; import { z, defineCollection } from 'astro:content';
import { Tag } from 'src/types';
export enum Tag {
'Apps' = 'Apps',
'Digital Life' = 'Digital Life',
'Gaming' = 'Gaming',
'Learning' = 'Learning',
'Life' = 'Life',
'MacOS' = 'MacOS',
'Music' = 'Music',
'Making Stuff' = 'Making Stuff',
'Mental Health' = 'Mental Health',
'Movies & TV' = 'Movies & TV',
'Programming' = 'Programming',
'Tech' = 'Tech',
'Tutorial' = 'Tutorial',
'Web Dev' = 'Web Dev',
// only for filtering
all = 'all',
}
const blogCollection = defineCollection({ const blogCollection = defineCollection({
schema: z.object({ schema: z.object({
title: z.string(), title: z.string(),
pubDate: z.string().transform((str) => new Date(str)), pubDate: z.string().transform((str: string) => new Date(str)),
tags: z.array(z.nativeEnum(Tag)), tags: z.array(z.nativeEnum(Tag)),
}), }),
}); });
+25
View File
@@ -0,0 +1,25 @@
// navigation links
export type NavLink = {
label: string;
path: string;
};
// blog tags
export enum Tag {
'Apps' = 'Apps',
'Digital Life' = 'Digital Life',
'Gaming' = 'Gaming',
'Learning' = 'Learning',
'Life' = 'Life',
'MacOS' = 'MacOS',
'Music' = 'Music',
'Making Stuff' = 'Making Stuff',
'Mental Health' = 'Mental Health',
'Movies & TV' = 'Movies & TV',
'Programming' = 'Programming',
'Tech' = 'Tech',
'Tutorial' = 'Tutorial',
'Web Dev' = 'Web Dev',
// only for filtering
all = 'all',
}