mypy, type hint: Union[float, int] -> is there a Number type?

Use float only, as int is implied in that type: def my_func(number: float): PEP 484 Type Hints specifically states that: Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument … Read more

What’s the difference between a constrained TypeVar and a Union?

T‘s type must be consistent across multiple uses within a given scope, while U‘s does not. With a Union type used as function parameters, the arguments as well as the return type can all be different: U = Union[int, str] def union_f(arg1: U, arg2: U) -> U: return arg1 x = union_f(1, “b”) # No … Read more

Typescript: How can I make entries in an ES6 Map based on an object key/value type

Here’s the closest I can imagine getting, although I still don’t understand why we don’t just use plain objects to begin with: type ObjectToEntries<O extends object> = { [K in keyof O]: [K, O[K]] }[keyof O] interface ObjectMap<O extends object> { forEach(callbackfn: <K extends keyof O>( value: O[K], key: K, map: ObjectMap<O> ) => void, … Read more

tech