This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Celsius = { | |
type: "celsius"; | |
value: number; | |
}; | |
const celsius = (value: number): Celsius => ({ | |
value, | |
type: "celsius", | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Protocol | |
class Dog(Protocol): | |
type: str | |
def __call__(self, food: str) -> None: | |
pass | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Protocol, Sequence | |
from dataclasses import dataclass | |
class PersonProtocol(Protocol): | |
name: str | |
age: int | |
height: float | |
friends: Sequence["PersonProtocol"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Protocol | |
from dataclasses import dataclass | |
class HasAge(Protocol): | |
age: int | |
class HasName(Protocol): | |
name: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Protocol, runtime_checkable | |
@runtime_checkable | |
class FileHandlerProtocol(Protocol): | |
def open(self) -> bytes: | |
... | |
def close(self) -> None: | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
class FileHandlerABC(ABC): | |
@abstractmethod | |
def open(self) -> bytes: | |
... | |
@abstractmethod | |
def close(self) -> None: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface FileHandler { | |
open: () => string; | |
close: () => void; | |
} | |
// with usage like | |
function getContents(fileHandler: FileHandler): string { | |
try { | |
return fileHandler.open(); |
NewerOlder