
An interesting aspect of TypeScript's Partial is that it is not actually a language feature, but a library feature: type Partial<T> = { [P in keyof T]?: T[P]; }; TypeScript's type system essentially provides a way to build a type that maps over the attributes of another type. Introducing a mechanism like that in Python would be very ambitious, but it could unlock other use cases. El mié, 26 oct 2022 a las 13:33, Patrick Arminio (<patrick.arminio@gmail.com>) escribió:
Hi folks! I just wanted to get feelings on having something like Partial in TypeScript. The way it works is that it makes every key inside a TS type/interface optional.
Here's an example of how it works: https://www.typescriptlang.org/play#code/JYOwLgpgTgZghgYwgAgCoHsAm7kG8BQyyYw...
For a library I work on we might have an use case that could use a way to make a partial class from another class, for example:
``` @dataclass class Todo: title: str description: str
def update_todo(todo: Todo, fields_to_update: Partial[Todo]) -> Todo: ...
todo1 = Todo(title="organize desk", description="clear clutter");
update_todo(todo1, Partial[Todo](description="Something")) ```
I purposely didn't do the example using typed dicts, mostly because I was curious to see if we can find a nice API that can work for classes too. And my example is a test 😊
What do you think? Do you see us having a Partial type that *also* works with classes? _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com