Skip to content

Commit 39acd09

Browse files
Check the number of type parameters before handling the Range type (#45)
Don't assume that `Range` refers to the built-in Range type unless it has exactly one type parameter. This allows for the correct export of custom-defined Range structs.
1 parent 9cb9563 commit 39acd09

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
/**
4+
* @param {Range} _range
5+
*/
6+
export function consume(_range: Range): void;
7+
/**
8+
* @returns {Range}
9+
*/
10+
export function into_js(): Range;
11+
/**
12+
* @param {(Range)[]} _ranges
13+
*/
14+
export function consume_vector(_ranges: (Range)[]): void;
15+
/**
16+
* @returns {(Range)[]}
17+
*/
18+
export function vector_into_js(): (Range)[];
19+
export interface Range {
20+
foo: number;
21+
bar: string;
22+
}
23+
24+
export interface A {
25+
range: Range;
26+
}
27+

tests-e2e/test_range1/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "test_range1"
3+
publish = false
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]
8+
wasm-bindgen = "0.2"
9+
tsify-next = { path = "../..", version = "*" }
10+
serde = { version = "1.0", features = ["derive"] }
11+
serde_json = "1.0"
12+
13+
[dev-dependencies]
14+
wasm-bindgen-test = "0.3"
15+
16+
[lib]
17+
path = "entry_point.rs"
18+
crate-type = ["cdylib"]
19+
20+
[build-dependencies]
21+
wasm-bindgen-cli = "0.2"

tests-e2e/test_range1/entry_point.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use serde::{Deserialize, Serialize};
2+
use tsify_next::Tsify;
3+
use wasm_bindgen::prelude::*;
4+
5+
#[derive(Tsify, Serialize, Deserialize)]
6+
#[tsify(into_wasm_abi, from_wasm_abi)]
7+
pub struct Range {
8+
foo: u32,
9+
bar: String,
10+
}
11+
12+
#[derive(Tsify, Serialize, Deserialize)]
13+
#[tsify(into_wasm_abi, from_wasm_abi)]
14+
pub struct A {
15+
range: Range,
16+
}
17+
18+
#[wasm_bindgen]
19+
pub fn consume(_range: Range) {}
20+
21+
#[wasm_bindgen]
22+
pub fn into_js() -> Range {
23+
Range {
24+
foo: 42,
25+
bar: "BAR".to_string(),
26+
}
27+
}
28+
29+
#[wasm_bindgen]
30+
pub fn consume_vector(_ranges: Vec<Range>) {}
31+
32+
#[wasm_bindgen]
33+
pub fn vector_into_js() -> Vec<Range> {
34+
vec![
35+
Range {
36+
foo: 42,
37+
bar: "BAR".to_string(),
38+
},
39+
Range {
40+
foo: 42,
41+
bar: "BAR".to_string(),
42+
},
43+
Range {
44+
foo: 42,
45+
bar: "BAR".to_string(),
46+
},
47+
]
48+
}

tsify-next-macros/src/typescript/ts_type_from_name.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ impl TsType {
117117
nanos_since_epoch: Self::NUMBER;
118118
},
119119

120-
"Range" | "RangeInclusive" => {
120+
// Treat as std::ops::Range or std::ops::RangeInclusive only when there is exactly one type parameter.
121+
// Otherwise, consider it a user-defined type and do not perform any conversion.
122+
"Range" | "RangeInclusive" if args.len() == 1 => {
121123
let start = Self::from_syn_type(config, args[0]);
122124
let end = start.clone();
123125

0 commit comments

Comments
 (0)