๐ typescript-types
Rusty TypeScript Type System Core ๐ฆ๐ท

๐ Introduction
typescript-types is the core type system package for Rusty TypeScript, defining the representation, type conversion, and error handling mechanisms for all TypeScript values.
โจ Core Features
| Feature |
Description |
Status |
| ๐ท TsValue |
Complete TypeScript value type system |
โ
Ready |
| โ ๏ธ TsError |
Standard error type definitions |
โ
Ready |
| ๐ ToTsValue |
Rust type to TS value conversion trait |
โ
Ready |
| ๐งฌ Complex Types |
Support for functions, objects, arrays, promises, and more |
โ
Ready |
| ๐ Type Checking |
Runtime type checking methods |
โ
Ready |
| ๐ฏ Type Conversion |
Safe type-to-type conversions |
โ
Ready |
๐ Quick Start
Add Dependency
[dependencies]
typescript-types = { path = "../compilers/typescript-types" }
Basic Usage
use typescript_types::{TsValue, TsError, ToTsValue};
fn main() {
let num = TsValue::Number(42.0);
println!("Number: {}", num.to_string());
let str = TsValue::String("Hello, TypeScript! ๐ฆ".to_string());
println!("String: {}", str.to_string());
let bool_val = TsValue::Boolean(true);
println!("Boolean: {}", bool_val.to_boolean());
let obj = TsValue::Object(vec![
("name".to_string(), TsValue::String("Rusty".to_string())),
("version".to_string(), TsValue::Number(1.0)),
]);
let arr = TsValue::Array(vec![
TsValue::Number(1.0),
TsValue::Number(2.0),
TsValue::Number(3.0),
]);
assert!(num.is_number());
assert!(str.is_string());
assert!(bool_val.is_boolean());
assert!(obj.is_object());
assert!(arr.is_array());
}
Rust Type Conversion
use typescript_types::ToTsValue;
fn main() {
let num: f64 = 3.14;
let ts_num = num.to_ts_value();
let text = "Hello";
let ts_str = text.to_ts_value();
let flag = true;
let ts_bool = flag.to_ts_value();
let vec = vec![1, 2, 3];
let ts_arr = vec.to_ts_value();
let maybe: Option<i32> = Some(42);
let ts_opt = maybe.to_ts_value();
let none: Option<i32> = None;
let ts_none = none.to_ts_value();
}
Error Handling
use typescript_types::TsError;
fn may_fail() -> Result<TsValue, TsError> {
Err(TsError::TypeError(
"Expected number, got string".to_string()
))
}
๐ก Usage Examples
Creating Complex Objects
use typescript_types::TsValue;
fn create_user() -> TsValue {
TsValue::Object(vec![
("id".to_string(), TsValue::Number(1.0)),
("name".to_string(), TsValue::String("Alice".to_string())),
("active".to_string(), TsValue::Boolean(true)),
("tags".to_string(), TsValue::Array(vec![
TsValue::String("admin".to_string()),
TsValue::String("developer".to_string()),
])),
("metadata".to_string(), TsValue::Object(vec![
("created".to_string(), TsValue::Date(1700000000)),
("version".to_string(), TsValue::Number(2.0)),
])),
])
}
Function Values
use typescript_types::{TsValue, ToTsValue};
use std::rc::Rc;
fn create_add_function() -> TsValue {
TsValue::Function(Rc::new(|args: &[TsValue]| {
if args.len() >= 2 {
let a = args[0].to_number();
let b = args[1].to_number();
TsValue::Number(a + b)
} else {
TsValue::Error("Expected 2 arguments".to_string())
}
}))
}
let add = create_add_function();
if let TsValue::Function(f) = add {
let result = f(&[TsValue::Number(10.0), TsValue::Number(20.0)]);
println!("10 + 20 = {}", result.to_number()); }
Promise Simulation
use typescript_types::TsValue;
fn create_promise() -> TsValue {
TsValue::Promise(Box::new(TsValue::String("Resolved!".to_string())))
}
let promise = create_promise();
assert!(promise.is_promise());
๐งช Testing
cargo test --package typescript-types
cargo test --package typescript-types -- --nocapture
๐ Dependencies
- No internal dependencies - this is a foundational package
๐ค Contributing
We welcome issues and PRs! Please ensure:
- โ
Code passes
cargo clippy checks
- โ
Code is formatted with
cargo fmt
- โ
New types include complete method implementations
- โ
Corresponding unit tests are added
๐ License
MIT License - see LICENSE
๐ฆ Types are the soul of programs, let's treat them well ๐ท