Skip to content

Instantly share code, notes, and snippets.

View jaycosaur's full-sized avatar
🐦

Jacob Richter jaycosaur

🐦
  • Sydney, Australia
View GitHub Profile
@jaycosaur
jaycosaur / composition.ts
Created August 9, 2020 11:48
Composable types [typescript] - Typescript to Python field guide
interface Animal {
length: number;
eat: (food: string) => void;
}
// Fish extends the Animal interface
interface Fish extends Animal {
swim: (howLong: number) => void;
}
@jaycosaur
jaycosaur / composition.py
Last active August 10, 2020 06:35
Composable types [python-ABC] - Typescript to Python field guide
from abc import ABC, abstractmethod
class Animal(ABC):
@property
@abstractmethod
def length(self) -> float:
...
@abstractmethod
@jaycosaur
jaycosaur / composition.py
Created August 9, 2020 11:50
Composable types [python-protocol] - Typescript to Python field guide
from typing import Protocol
class Animal(Protocol):
length: float
def eat(self, food: str) -> None:
...
# Fish extends the protocol Animal and is itself a protocol
@jaycosaur
jaycosaur / generics.ts
Last active August 9, 2020 12:11
Generics [typescript] - Typescript to Python field guide
function variadic<T>(...args: T[]): T[] {
return args;
}
function multiGeneric<A, B>(argA: A, argB: B): [A, B] {
return [argA, argB];
}
@jaycosaur
jaycosaur / generics.py
Created August 9, 2020 11:53
Generics [python] - Typescript to Python field guide
from typing import TypeVar, Sequence, Tuple
T = TypeVar("T")
def variadic(*args: T) -> Sequence[T]:
return args
A = TypeVar("A")
B = TypeVar("B")
def multi_generic(arg_a: A, arg_b: B) -> Tuple[A, B]:
return (arg_a, arg_b)
@jaycosaur
jaycosaur / interfaces.ts
Created August 9, 2020 11:54
Interfaces [typescript] - Typescript to Python field guide
interface FileHandler {
open: () => string;
close: () => void;
}
// with usage like
function getContents(fileHandler: FileHandler): string {
try {
return fileHandler.open();
@jaycosaur
jaycosaur / interfaces.py
Created August 9, 2020 11:56
Interfaces [python-ABC] - Typescript to Python field guide
from abc import ABC, abstractmethod
class FileHandlerABC(ABC):
@abstractmethod
def open(self) -> bytes:
...
@abstractmethod
def close(self) -> None:
@jaycosaur
jaycosaur / interfaces.py
Created August 9, 2020 11:57
Interfaces [python-protocol] - Typescript to Python field guide
from typing import Protocol, runtime_checkable
@runtime_checkable
class FileHandlerProtocol(Protocol):
def open(self) -> bytes:
...
def close(self) -> None:
...
@jaycosaur
jaycosaur / intersection.py
Last active August 10, 2020 06:32
Intersection types [python-protocols] - Typescript to Python field guide
from typing import Protocol
from dataclasses import dataclass
class HasAge(Protocol):
age: int
class HasName(Protocol):
name: str
@jaycosaur
jaycosaur / person.py
Last active August 10, 2020 06:31
Implicit vs Explicit typing [python-protocol] - Typescript to Python field guide
from typing import Protocol, Sequence
from dataclasses import dataclass
class PersonProtocol(Protocol):
name: str
age: int
height: float
friends: Sequence["PersonProtocol"]