#proc-macro #type #utility #typescript #string #content #realize

macro utility-types

This crate use proc-macro to realize several utility types of TypeScript

2 releases

0.0.2 Nov 26, 2021
0.0.1 Nov 26, 2021

#2265 in Procedural macros

Download history 10/week @ 2023-11-27 5/week @ 2024-02-05 18/week @ 2024-02-12 1/week @ 2024-02-19 68/week @ 2024-02-26 19/week @ 2024-03-04

108 downloads per month

MIT license

22KB
393 lines

utility types

WIP

This crate use proc-macro to realize several utility types of TypeScript

Example

source code:

#[partial(PartialArticle)]
#[pick(ContentComments, [content, comments], [Debug])]
#[omit(AuthorLikedComments, [content], [])]
pub struct Article<T> {
    author: String,
    content: String,
    liked: usize,
    comments: T,
}

generated code:

pub struct Article<T> {
    author: String,
    content: String,
    liked: usize,
    comments: T,
}
pub struct AuthorLikedComments<T> {
    author: String,
    liked: usize,
    comments: T,
}
#[derive(Debug)]
pub struct ContentComments<T> {
    content: String,
    comments: T,
}

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.


lib.rs:

utility_types

This crate use proc-macro to realize several utility types of TypeScript

macro TypeScript Utility Type
[macro@partial] Partial<Type>
[macro@required] Required<Type>
[macro@pick] Pick<Type, Keys>
[macro@omit] Omit<Type, Keys>
[macro@exclude] Exclude<Type, ExcludedUnion>
[macro@extract] Extract<Type, Union>

Dependencies

~1.5MB
~35K SLoC