utility types
This crate use proc-macro to realize several utility types of Typescript in Rust.
| macro | Typescript Utility Type |
|---|---|
| [Partial] | Partial<Type> |
| [Pick] | Pick<Type, Keys> |
| [Omit] | Omit<Type, Keys> |
| [Extract] | Extract<Type, Union> |
| [Exclude] | Exclude<UnionType, ExcludedMembers> |
Example
Here is an example of how to use this crate.
use ;
The above code will generate the following code.
Some useful traits are also generated:
From<Foo>forPartialFoo,PickAB,OmitCDFrom<PartialFoo>forFoo
Forwarding Attributes
To use this crate with other crates that need attributes, you can use the forward_attrs attribute to control which attributes are forwarded to the generated struct or enum.
use ;
use Omit;
let omit_cd: OmitCD = from_str.unwrap;
assert_eq!;
The behavior of the forward_attrs attribute is as follows:
- If not provided, all attributes are forwarded by default.
- If provided with a list of attributes, only the specified attributes are forwarded.
- For example,
forward_attrs(doc, serde)will forward onlydocandserde. - If provided with only
*(forward_attrs(*)), all attributes are forwarded. - If provided with an empty list (
forward_attrs()), no attributes are forwarded.
- For example,
- If provided with a list inside
not(), all attributes except the specified attributes are forwarded.- For example,
forward_attrs(not(serde))will forward all attributes exceptserde.
- For example,
Known Issue
Currently I don't analyze which generic is used in the generated struct or enum. So rustc will complain if the field with generic is not included in the generated struct or enum.