From 4ce86b15321245bb0fd7e560ffd99b06d8951740 Mon Sep 17 00:00:00 2001 From: Liam Pietralla Date: Fri, 15 Aug 2025 13:23:35 +1000 Subject: [PATCH] added ts string literal types --- .vitepress/config.mts | 8 ++++++++ docs/index.md | 4 ++++ docs/typescript/index.md | 3 +++ docs/typescript/string-literal-types.md | 22 ++++++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 docs/typescript/index.md create mode 100644 docs/typescript/string-literal-types.md diff --git a/.vitepress/config.mts b/.vitepress/config.mts index 06c2b42..d5598dd 100644 --- a/.vitepress/config.mts +++ b/.vitepress/config.mts @@ -154,6 +154,14 @@ export default defineConfig({ { text: 'Ansible Inventory Generation', link: '/terraform/ansible-inventory-generation' }, ] }, + { + text: 'Typescript', + link: '/typescript/', + collapsed: true, + items: [ + { text: 'String Literal Types', link: '/typescript/string-literal-types' }, + ] + } ], socialLinks: [ diff --git a/docs/index.md b/docs/index.md index b6723cf..3cafd78 100644 --- a/docs/index.md +++ b/docs/index.md @@ -62,6 +62,10 @@ hero: text: Terraform link: /terraform/ + - theme: alt + text: Typescript + link: /typescript/ + # features: # - title: Feature A # details: Lorem ipsum dolor sit amet, consectetur adipiscing elit diff --git a/docs/typescript/index.md b/docs/typescript/index.md new file mode 100644 index 0000000..9e3548c --- /dev/null +++ b/docs/typescript/index.md @@ -0,0 +1,3 @@ +# Typescript Snippets and Musings + +#### [String Literal Types](./string-literal-types.md) \ No newline at end of file diff --git a/docs/typescript/string-literal-types.md b/docs/typescript/string-literal-types.md new file mode 100644 index 0000000..db7091c --- /dev/null +++ b/docs/typescript/string-literal-types.md @@ -0,0 +1,22 @@ +# String Literal Types + +String literal types are a powerful feature in TypeScript that allows you to specify exact string values a variable can hold. This is useful for creating more precise types and can help catch errors at compile time. + +## Example + +```typescript +const DIRECTIONS = ["left", "right", "up", "down"] as const; +type Direction = typeof DIRECTIONS[number]; + +// Usage +function move(direction: Direction) { + // ... +} + +// Array Usage +for (const dir of DIRECTIONS) { + move(dir); +} +``` + +In the above examples you can see how we can both use the `Direction` type directly and also leverage the `DIRECTIONS` array for type-safe values.