Skip to content

Instantly share code, notes, and snippets.

@dmmulroy
Created March 14, 2024 01:05
Show Gist options
  • Save dmmulroy/d12210f4f347c39551b69b75dcddeada to your computer and use it in GitHub Desktop.
Save dmmulroy/d12210f4f347c39551b69b75dcddeada to your computer and use it in GitHub Desktop.
Generating TypeScript declaration files with Gleam
import type * as $io from "../gleam_stdlib/gleam/io.d.mts";
import type * as _ from "./gleam.d.mts";
export class User extends _.CustomType {
constructor(id: string, username: string, role: Role$);
id: string;
username: string;
role: Role$;
}
export type User$ = User;
export class Default extends _.CustomType {}
export class Admin extends _.CustomType {}
export class Guest extends _.CustomType {}
export class Other extends _.CustomType {
constructor(argument$0: string);
0: string;
}
export type Role$ = Default | Admin | Guest | Other;
export function role_to_string(role: Role$): string;
export function make_admin(user: User$): _.Result<User$, any>;
export function print_role(user: User$): null;
import gleam/io
pub type User {
User(id: String, username: String, role: Role)
}
pub type Role {
Default
Admin
Guest
Other(String)
}
pub fn role_to_string(role) {
case role {
Admin -> "admin"
Default -> "default"
Guest -> "Guest"
Other(name)-> "Other(" <> name <> ")"
}
}
pub fn make_admin(user) {
// some business logic
Ok(User(..user, role: Admin))
}
pub fn print_role(user: User) {
user.role |> role_to_string |> io.println
}
import * as $io from "../gleam_stdlib/gleam/io.mjs";
import { Ok, CustomType as $CustomType } from "./gleam.mjs";
export class User extends $CustomType {
constructor(id, username, role) {
super();
this.id = id;
this.username = username;
this.role = role;
}
}
export class Default extends $CustomType {}
export class Admin extends $CustomType {}
export class Guest extends $CustomType {}
export class Other extends $CustomType {
constructor(x0) {
super();
this[0] = x0;
}
}
export function role_to_string(role) {
if (role instanceof Admin) {
return "admin";
} else if (role instanceof Default) {
return "default";
} else if (role instanceof Guest) {
return "Guest";
} else {
let name = role[0];
return ("Other(" + name) + ")";
}
}
export function make_admin(user) {
return new Ok(user.withFields({ role: new Admin() }));
}
export function print_role(user) {
let _pipe = user.role;
let _pipe$1 = role_to_string(_pipe);
return $io.println(_pipe$1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment