-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typescript_type attribute #1986
Conversation
This pr has some problems, |
Thanks for the PR! I'm not personally too familiar with the motivations here, but @Pauan would you be able to review this? |
What about adding an I just got a new idea to deal this with #[wasm_bindgen(typescript_custom_section)]
const ITEXT_STYLE: &'static str = r#"
interface ITextStyle {
bold: boolean;
italic: boolean;
size: i32;
}
"#;
#[wasm_bindgen]
struct TextStyle {
pub bold: bool,
pub italic: bool,
pub size: i32,
}
#[wasm_bindgen]
impl TextStyle {
#[wasm_bindgen(constructor, object_args="ITextStyle")]
pub fn new(interface: JsValue) {
// parse JsValue
}
} and in typescript, we can generate import { TextStyle } from "../pkg";
const style = new TextStyle({
bold: true,
italic: true,
size: 8,
});
|
Although I originally suggested this, I'm not sure So I agree that something like #[wasm_bindgen(typescript_custom_section)]
const ITEXT_STYLE: &'static str = r#"
interface ITextStyle {
bold: boolean;
italic: boolean;
size: i32;
}
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "ITextStyle")]
pub type ITextStyle;
}
#[wasm_bindgen]
struct TextStyle {
pub bold: bool,
pub italic: bool,
pub size: i32,
}
#[wasm_bindgen]
impl TextStyle {
#[wasm_bindgen(constructor)]
pub fn new(interface: ITextStyle) {
// parse JsValue
}
} The The benefit of The above code is rather verbose, however it's possible to create a macro which will generate it: typescript! {
interface ITextStyle {
bold: boolean;
italic: boolean;
size: i32;
}
} So I think it would be best to add in support for |
Cool! I agree with |
@clearloop As a consequence of #2012 I needed to add a But this PR is still useful, since it adds in some unit tests. So could you please rebase this on master? |
@Pauan Yep, I'll do it right now! (Waiting for your reply for a long long time...) |
It occurs lots of warnings in my fork, even before rebase
I didn't see these before...should I try to fix these? |
Waiting for your review @Pauan, I also have a big update for my project if Well, after l fix the tests... |
Sorry, I'm usually busy with a lot of different things, so things sometimes slip through the cracks. If I'm taking too long to respond please do ping me.
To be clear, #2012 already added in |
Thanks for offering me this opportunity, I'll remove my verbose code(except the test) right now~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
proc-macro attribute
#[wasn_bindgen(plain_object)]
Motivation
Ref to @Pauan 's suggestion in #1591, I just wrote an attribute named
plain_object
.I am writing a wasm UI library these days, and I have to duplicate some types for 3 times(in my project, rust for 2 and ts for 1) without the ability exporting typed struct from rust...
I feel very excited about
wasm
, and never thinking about that I can contribute towasm-bindgen
one day, hope this pr can help.Solution
plain_object
constructor
withpalin_object
free() void;
while generating.d.ts
Result
Now we can export rust types like:
And in typescript file:
Confusions
I'm quite a newbie in programming, not sure about if just hide
free() void;
ind.ts
is okay.