Last active
August 9, 2020 23:20
-
-
Save jaycosaur/fc77323051901cb7820c6a5dba023cd1 to your computer and use it in GitHub Desktop.
Strong or Weak [python] - Typescript to Python field guide
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 NewType | |
Celsius = NewType("Celsius", float) | |
Fahrenheit = NewType("Fahrenheit", float) | |
def convert_to_fahrenheit(value: Celsius) -> Farenheit: | |
return Farenheit((value * 9 / 5) + 32) | |
def convert_to_celsius(value: Fahrenheit) -> Celsius: | |
return Celsius((value - 32) * 5 / 9) | |
converted = convert_to_celsius( | |
0 | |
) # Argument 1 to "convert_to_celsius" has incompatible type "int"; expected "Fahrenheit" | |
converted = convert_to_celsius(Fahrenheit(0)) # it works! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment