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 / 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 / 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 / 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 / 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.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 / intersection.py
Created August 9, 2020 11:47
Intersection types [python] - Typescript to Python field guide
from dataclasses import dataclass
@dataclass
class HasAge:
age: int
@dataclass
class HasName:
name: str
@jaycosaur
jaycosaur / intersection.ts
Created August 9, 2020 11:46
Intersection types [typescript] - Typescript to Python field guide
type HasAge = { age: number };
type HasName = { name: string };
type HasNameAndAge = HasName & HasAge;
const correct: HasNameAndAge = { name: "Tim", age: 23 };
@jaycosaur
jaycosaur / union.py
Created August 9, 2020 11:45
Union types [python] - Typescript to Python field guide
from typing import Union
StringOrNumber = Union[str, int]
something_good: StringOrNumber = "123" # ok!
something_bad: StringOrNumber = {} # not ok!
@jaycosaur
jaycosaur / union.ts
Created August 9, 2020 11:44
Union types [typescript] - Typescript to Python field guide
type StringOrNumber = string | number;
const somethingGood: StringOrNumber = "123"; // ok!
const somethingBad: StringOrNumber = {}; // not ok!
@jaycosaur
jaycosaur / person.py
Created August 9, 2020 11:42
Implicit vs Explicit typing [python] - Typescript to Python field guide
from typing import Sequence
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
height: float
friends: Sequence[Person]
# in Python, unless using Protocols, variables must implement types explicitly