Revert "test: update TS declarations test"

This reverts commit 3f64e8ccd4.
This commit is contained in:
Louis Pilfold 2025-12-11 19:19:53 +00:00 committed by GitHub
parent 2b9200d370
commit 911d1d76dc
4 changed files with 12 additions and 174 deletions

View file

@ -5,4 +5,4 @@ clean:
.PHONY: test
test:
cargo run --quiet -- build
tsc ./main.ts --noEmit --lib es2015,dom
tsc ./build/dev/javascript/typescript_declarations/typescript_declarations.d.mts --noEmit --lib es2015

View file

@ -1,3 +1,3 @@
# typescript_declarations
Check that generated TypeScript declarartions are correct. This requires `tsc` installed and be in PATH.
Check that generated TypeScript declarartions are correct. This requires TypeScript installed and be in PATH.

View file

@ -1,79 +0,0 @@
import type { Option$ } from "./build/dev/javascript/gleam_stdlib/gleam/option.d.mts";
import { List, Ok, Result } from "./build/dev/javascript/typescript_declarations/gleam.mjs";
import * as Gleam from "./build/dev/javascript/typescript_declarations/typescript_declarations.mjs"
// Check add and add_alias
const sum: number = Gleam.add_alias(Gleam.add(1, 2), 908);
// Check ID lists constants
const count_of_moderators: number = Gleam.moders.countLength();
const count_of_administrators: number = Gleam.admins.countLength();
// Check add_one closure
const add_one: (_: number) => number = Gleam.add_one()
const two: number = add_one(1)
// Check UserId type alias
const first_moderator: Gleam.UserId = Gleam.moders[ 0];
const first_moderator_num: number = first_moderator;
const me: UserID = 84738;
type UserID = Gleam.UserId;
// Check is_divisible_by
const is_five_divisible_by_two: boolean = Gleam.is_divisible_by(5, 2);
// Check extern alert function if we are in browser target
if (typeof window != undefined) {
Gleam.js_alert("Hello!");
}
// Check generic twice function
const twice_number: number = Gleam.twice(45, add_one);
// Check recursive sum_list
const sum_of_list: number = Gleam.sum_list(List.fromArray([10, 25, 65383, 8910, 1893]), 0);
// Check results
const result_ok: Result<number, number> = new Ok(10);
const is_ok: boolean = Gleam.is_ok_result(result_ok);
// Check name_description tuple
const name: string = Gleam.name_description[0];
const description: string = Gleam.name_description[1];
// Check User and related functions
const user: Gleam.User$ = new Gleam.User("King", 83874, new Gleam.PlainUser());
const guest: Gleam.User$ = new Gleam.Guest();
const user_username: Option$<string> = Gleam.user_name(user);
const guest_username: Option$<string> = Gleam.user_name(guest);
const plain_user_string: string = Gleam.role_string(user.withFields["role"]);
// Check Either type
const left_either: Gleam.Either$<string, number> = new Gleam.Left("Hello!");
const right_either: Gleam.Either$<string, number> = new Gleam.Right(3747);
// This added since TypeScript will give warnings about unused things otherwise
void [
sum,
count_of_administrators,
count_of_moderators,
add_one,
two,
first_moderator,
first_moderator_num,
me,
is_five_divisible_by_two,
twice_number,
sum_of_list,
result_ok,
is_ok,
name,
description,
user,
guest,
user_username,
guest_username,
plain_user_string,
left_either,
right_either,
]

View file

@ -1,102 +1,19 @@
import gleam/option.{type Option}
import gleam/result
//// Test TS declarartions
/// Greeting for user
pub const greeting: String = "Hello, dear user!"
/// Answer for all questions in the world
pub const answer = 256
/// Add two numbers
pub fn add(first a: Int, second b: Int) -> Int {
a + b
}
/// Returns true if number is divisible by another
pub fn is_divisible_by(number num: Int, divider div: Int) -> Bool {
case num % div {
0 -> True
_ -> False
}
}
/// ID of specific user
pub type UserId =
Int
/// Alias to add function
pub const add_alias = add
/// Return function that will add one to given number
pub fn add_one() -> fn(Int) -> Int {
let func = add(1, _)
func
}
/// IDs of administrators
pub const admins: List(UserId) = [00_010, 00_000, 00_001, 00_002, 29_373]
/// IDs of moderators
pub const moders = [01_013, 36_371, 74_839, 18_930, 36_373]
/// Create alert on browser target
@external(javascript, "globalThis", "alert")
pub fn js_alert(text: String) -> Nil
/// Twice value via given function
pub fn twice(val: a, function: fn(a) -> a) -> a {
function(function(val))
}
/// Recursively sum list of numbers
pub fn sum_list(list: List(Int), total: Int) -> Int {
case list {
[first, ..rest] -> sum_list(rest, total + first)
[] -> total
}
}
/// Returns true if result is ok
pub fn is_ok_result(res: Result(a, b)) -> Bool {
result.is_ok(res)
}
/// First value is name and second is description
pub const name_description = #("MyApp", "My Awesome Application")
/// Role of user
pub type UserRole {
/// Administrator
Administrator
/// Moderator
Moderator
/// Just a user
PlainUser
}
/// Represents user
pub type User {
/// Represents registered user
User(username: String, id: UserId, role: UserRole)
/// Represents guest without any data
// Registered user
User(name: String, email: String)
/// Unregistered guest
Guest
}
/// Get name of user, None if user is guest
pub fn user_name(user: User) -> Option(String) {
/// Get name of user. Returns "Guest" if user is guest
pub fn get_name(user) {
case user {
User(name, ..) -> option.Some(name)
Guest -> option.None
User(name, ..) -> name
Guest -> "Guest"
}
}
/// Format user role as string
pub fn role_string(role: UserRole) -> String {
case role {
PlainUser -> "user"
Moderator -> "moderator"
Administrator -> "admin"
}
}
pub type Either(a, b) {
Left(a)
Right(b)
}