Skip to content

Instantly share code, notes, and snippets.

View pfftdammitchris's full-sized avatar
💭
Dreaming

Christopher Tran pfftdammitchris

💭
Dreaming
View GitHub Profile
@pfftdammitchris
pfftdammitchris / snippet-1.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-1.ts
// Using a type annotation
const theme: Record<string, string> = {
primary: '#3b82f6',
secondary: '#10b981',
danger: '#ef4444',
}
// Later in your code...
theme.primary.toUpperCase() // Works fine
@pfftdammitchris
pfftdammitchris / snippet-2.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-2.ts
// No type annotation
const theme = {
primary: '#3b82f6',
secondary: '#10b981',
danger: '#ef4444',
}
// Great! TypeScript knows the exact keys and values
theme.primary // Type is '#3b82f6'
theme.primry // Error! Property 'primry' does not exist
@pfftdammitchris
pfftdammitchris / snippet-3.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-3.ts
type ThemeColors = Record<'primary' | 'secondary' | 'danger', string>
const theme = {
primary: '#3b82f6',
secondary: '#10b981',
danger: '#ef4444',
} satisfies ThemeColors
// Validation: TypeScript ensures all required keys exist
// Inference: TypeScript still knows the exact literal values
@pfftdammitchris
pfftdammitchris / snippet-4.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-4.ts
type Route = {
path: string
method: 'GET' | 'POST' | 'PUT' | 'DELETE'
}
const routes = {
users: { path: '/api/users', method: 'GET' },
createUser: { path: '/api/users', method: 'POST' },
updateUser: { path: '/api/users/:id', method: 'PUT' },
} satisfies Record<string, Route>
@pfftdammitchris
pfftdammitchris / snippet-5.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-5.ts
// Contract is explicit - great for function parameters and return types
function createUser(data: UserInput): User {
// ...
}
// But for objects, it widens the type
const config: Config = { timeout: 5000 }
// config.timeout is now just `number`, not `5000`
@pfftdammitchris
pfftdammitchris / snippet-6.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-6.ts
// Sometimes necessary with DOM or external data
const element = document.getElementById('app') as HTMLDivElement
// Dangerous! You're telling TypeScript to trust you
const data = JSON.parse(response) as User // Could be anything at runtime
@pfftdammitchris
pfftdammitchris / snippet-7.ts
Created January 16, 2026 04:25
The Power of TypeScript's Satisfies Operator - snippet-7.ts
// Best of both worlds
const config = {
timeout: 5000,
retries: 3,
} satisfies Partial<Config>
// config.timeout is `5000`, and we know it matches Config
@pfftdammitchris
pfftdammitchris / snippet-8.ts
Created January 16, 2026 04:24
The Power of TypeScript's Satisfies Operator - snippet-8.ts
type RGB = [number, number, number]
type Color = string | RGB
const palette = {
red: [255, 0, 0],
green: '#00ff00',
blue: [0, 0, 255],
} satisfies Record<string, Color>
// TypeScript knows the exact type of each property!
@pfftdammitchris
pfftdammitchris / snippet-9.ts
Created January 16, 2026 04:24
The Power of TypeScript's Satisfies Operator - snippet-9.ts
type Endpoint = {
url: string
method: 'GET' | 'POST'
}
const endpoints = {
getUsers: { url: '/users', method: 'GET' },
createUser: { url: '/users', method: 'POST' },
// Forgot an endpoint? TypeScript can help if you use a stricter type
} satisfies Record<string, Endpoint>
@pfftdammitchris
pfftdammitchris / snippet-10.ts
Created January 16, 2026 04:24
The Power of TypeScript's Satisfies Operator - snippet-10.ts
type Features = 'darkMode' | 'notifications' | 'analytics'
const featureFlags = {
darkMode: true,
notifications: false,
analytics: true,
} satisfies Record<Features, boolean>
// If you add a new feature to the union and forget to add it here,
// TypeScript will error!