Skip to content

Instantly share code, notes, and snippets.

View akaptur's full-sized avatar

Allison Kaptur akaptur

View GitHub Profile
StateTypes = Literal["loading", "loaded", "error"]
class State(enum.Enum):
loading = "loading"
loaded = "loaded"
error = "error"
type Account = {
name: string,
balance: string,
}
function makeAccount({
name = "foo",
balance = "10.05"
}: Partial<Account> = {}): Account {
return {name, balance}
}
@akaptur
akaptur / summary.md
Last active December 14, 2021 01:09
Structural typing Nominative typing
TypeScript default behavior Use tagged unions
Python with mypy Use protocols default behavior
const val = {value: "hi", kind: "a" as const, image: "a gzipped photo of a cat"}
my_func(val); // no error
type A = {
value: string;
kind: "a"; // here's the tag
};
type B = {
value: string;
kind: "b";
};
function my_func(a: A): string {
return a.value;
from typing import Protocol
class MyProto(Protocol):
value: str
@dataclass
class A:
value: str
@dataclass
type A = {
value: string;
}
function my_func(a: A): string {
return a.value;
}
my_func({value: "hi", image: "a gzipped picture of a cat"}); // no error
class A {
value: string;
constructor(val: string) {
this.value = val;
}
};
class B {
value: string;
constructor(val: string) {
this.value = val;
type A = {
value: string;
};
type B = {
value: string;
};
function my_func(a: A): string {
return a.value;
}
my_func({value: "hi"}); // no error here!