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 / workAround.ts
Last active August 9, 2020 23:17
Working around implicit conversions [typescript] - Typescript to Python field guide
type Celsius = {
type: "celsius";
value: number;
};
const celsius = (value: number): Celsius => ({
value,
type: "celsius",
});
@jaycosaur
jaycosaur / weak.ts
Last active August 9, 2020 23:18
Strong or Weak [typescript] - Typescript to Python field guide
type Celsius = number;
type Fahrenheit = number;
function convertToCelsius(value: Fahrenheit): Celsius {
return (value * 9) / 5 + 32;
}
function convertToFahrenheit(value: Celsius): Fahrenheit {
return ((value - 32) * 5) / 9;
}
@jaycosaur
jaycosaur / strong.py
Last active August 9, 2020 23:20
Strong or Weak [python] - Typescript to Python field guide
from typing import NewType
Celsius = NewType("Celsius", float)
Fahrenheit = NewType("Fahrenheit", float)
def convert_to_fahrenheit(value: Celsius) -> Farenheit:
return Farenheit((value * 9 / 5) + 32)
@jaycosaur
jaycosaur / hybrid.py
Created August 9, 2020 13:40
Hybrid [python-protocol] - Typescript to Python field guide
from typing import Protocol
class Dog(Protocol):
type: str
def __call__(self, food: str) -> None:
pass
@jaycosaur
jaycosaur / hybrid.ts
Last active August 9, 2020 13:40
Hybrid [typescript] - Typescript to Python field guide
interface Dog {
(food: string): void;
type: string;
}
const dogFactory = (breed: string): Dog => {
const eat = (food: string) => console.log(`Eating ${food}.`);
eat.type = breed;
return eat;
};
@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"]
@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 / 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 / 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.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();