Structural typing | Nominative typing | |
---|---|---|
TypeScript | default behavior | Use tagged unions |
Python with mypy | Use protocols | default behavior |
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
StateTypes = Literal["loading", "loaded", "error"] |
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
class State(enum.Enum): | |
loading = "loading" | |
loaded = "loaded" | |
error = "error" |
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 Account = { | |
name: string, | |
balance: string, | |
} | |
function makeAccount({ | |
name = "foo", | |
balance = "10.05" | |
}: Partial<Account> = {}): Account { | |
return {name, balance} | |
} |
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
const val = {value: "hi", kind: "a" as const, image: "a gzipped photo of a cat"} | |
my_func(val); // no error |
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 A = { | |
value: string; | |
kind: "a"; // here's the tag | |
}; | |
type B = { | |
value: string; | |
kind: "b"; | |
}; | |
function my_func(a: A): string { | |
return a.value; |
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 MyProto(Protocol): | |
value: str | |
@dataclass | |
class A: | |
value: str | |
@dataclass |
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 A = { | |
value: string; | |
} | |
function my_func(a: A): string { | |
return a.value; | |
} | |
my_func({value: "hi", image: "a gzipped picture of a cat"}); // no error |
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
class A { | |
value: string; | |
constructor(val: string) { | |
this.value = val; | |
} | |
}; | |
class B { | |
value: string; | |
constructor(val: string) { | |
this.value = val; |
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 A = { | |
value: string; | |
}; | |
type B = { | |
value: string; | |
}; | |
function my_func(a: A): string { | |
return a.value; | |
} | |
my_func({value: "hi"}); // no error here! |
NewerOlder