This file contains hidden or 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 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) |
This file contains hidden or 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
function variadic<T>(...args: T[]): T[] { | |
return args; | |
} | |
function multiGeneric<A, B>(argA: A, argB: B): [A, B] { | |
return [argA, argB]; | |
} |
This file contains hidden or 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 Animal(Protocol): | |
length: float | |
def eat(self, food: str) -> None: | |
... | |
# Fish extends the protocol Animal and is itself a protocol |
This file contains hidden or 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 Animal(ABC): | |
@property | |
@abstractmethod | |
def length(self) -> float: | |
... | |
@abstractmethod |
This file contains hidden or 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 Animal { | |
length: number; | |
eat: (food: string) => void; | |
} | |
// Fish extends the Animal interface | |
interface Fish extends Animal { | |
swim: (howLong: number) => void; | |
} |
This file contains hidden or 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 dataclasses import dataclass | |
@dataclass | |
class HasAge: | |
age: int | |
@dataclass | |
class HasName: | |
name: str |
This file contains hidden or 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 HasAge = { age: number }; | |
type HasName = { name: string }; | |
type HasNameAndAge = HasName & HasAge; | |
const correct: HasNameAndAge = { name: "Tim", age: 23 }; |
This file contains hidden or 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 Union | |
StringOrNumber = Union[str, int] | |
something_good: StringOrNumber = "123" # ok! | |
something_bad: StringOrNumber = {} # not ok! |
This file contains hidden or 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 StringOrNumber = string | number; | |
const somethingGood: StringOrNumber = "123"; // ok! | |
const somethingBad: StringOrNumber = {}; // not ok! |
This file contains hidden or 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 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 |