Skip to content

Instantly share code, notes, and snippets.

View rayrayzayzay's full-sized avatar

Rachel Zack rayrayzayzay

View GitHub Profile
geo_db_meta = MetaData()
geo_db_meta.reflect(bind=engine)
geom_table = geo_db_meta.tables[table_name]
initial_cols = _column_metadata_from_columns(geom_table.columns)
stmt = (
update(snapshots).where(snapshots.c.id==snapshot_id)
.values(columns=initial_cols)
)
session = orm.Session(bind=bind)
type Dog = {
numberOfLegs: number
collar: {
name: string
material: "leather" | "plastic"
}
}
type Cat = {
numberOfLegs: number
function getName(pet: Pet): string {
if ("collar" in pet) {
// TypeScript knows that pet is a Dog now
return pet.collar.name
}
// TypeScript knows that it can only be a Cat now
return pet.microchip.name
}
function isCat(pet: Pet): pet is Cat {
return "microchip" in pet
}
function isDog(pet: Pet): pet is Dog {
return "collar" in pet
}
function getName(pet: Pet): string {
if (isDog(pet)) return pet.collar.name
function isCat(pet: Pet): pet is Cat {
return "mcirochip" in pet // 🔥 spelling mistake - no cats ever!
}
type Hamster = {
microchip: {
id: string
name: string
}
}
type Pet = Dog | Cat | Hamster
type Dog = {
kind: "dog" // 👀
numberOfLegs: number
collar: {
name: string
material: "leather" | "plastic"
}
}
type Cat = {
❌ Unclear relationships between properties
type UserProfileRequest = {
isLoading: boolean
data?: UserProfileData
errorMessage?: string
statusCode?: number
}
// NOTE: In reality, we would probably use a generic like Loadable<UserProfileData>
❌ A "simple" component which isn't really simple
function UserProfile({request}: {request: MyProfileRequest}) {
// can we have stale data while we're loading? I guess not?
if (request.isLoading) return <Spinner />
// this goes before checking the errors, so I presume we can't have
// data and an error together? Or maybe we just don't care if there's
// an error but we get data?
✅ Explicit modeling of distinct states
type UserProfileFetching = {
status: "fetching"
}
type UserProfileSuccess = {
status: "success"
data: UserProfileData
statusCode: number