2 ms·
This is a misunderstanding of the Python type system. In the Typescript example you are depending on "foo" being structurally compatible to the "Foo" interface.
by bayesian_horse 5y ago
This is a misunderstanding of the Python type system. In the Typescript example you are depending on "foo" being structurally compatible to the "Foo" interface.
In the Python case, "foo" and "Foo" might look structurally compatible, but they aren't. "Foo" isn't just a type, it's an object of type "class". You can for example print(Foo), you can't console.log(Foo). "foo" is not an instance of the Foo class. What you are looking for is "dict[str, object]".
- goodoldneon 5y agoTypedDicts fall under structural typing, not nominal typing. So Mypy is cool with this: import typing class Foo(typing.TypedDict): a: int def do_thing(value: Foo) -> None: return foo: Foo = {"a": 1} do_thing(foo) My complaint here is that Mypy doesn't go far enough with structural inference. I assume this is because it doesn't support anonymous TypedDicts, whereas TypeScript supports anonymous interfaces/types