arabidopsis/typescript-definitions

ts(1036) error on simple enum

uonr opened this issue · 1 comments

uonr commented

Problem

#[derive(Serialize, TypescriptDefinition)]
pub enum LinkType {
    Inline,
    Autolink,
    Email,
    Unsupported,
    // WorkAround(())
}

output:

export enum LinkType { Inline = "Inline" , Autolink = "Autolink" , Email = "Email" , Unsupported = "Unsupported" };

error

error TS1036: Statements are not allowed in ambient contexts

Workaround

#[derive(Serialize, TypescriptDefinition)]
pub enum LinkType {
    Inline,
    Autolink,
    Email,
    Unsupported,
    WorkAround(())
}
export type LinkType = 
 | "Inline" 
 | "Autolink" 
 | "Email" 
 | "Unsupported" 
 | { WorkAround: [] };

Additional workarounds:

On Rust side

#[derive(Serialize, TypeScriptify)]
pub enum LinkType {
    Inline,
    Autolink,
    Email,
    Unsupported(#[serde(skip)] ()),
}
export type LinkType = 
 | "Inline" 
 | "Autolink" 
 | "Email" 
 | "Unsupported";

On Typescript side (see here)

#[derive(Serialize, TypeScriptify)]
pub enum LinkType {
    Inline,
    Autolink,
    Email,
    Unsupported,
}
export enum LinkType { Inline = "Inline" , Autolink = "Autolink" , Email = "Email" , Unsupported = "Unsupported" };

// then alias the enum like this
export type LinkTypeString = keyof typeof LinkType;

/* That's equivalent to:
export type LinkTypeString = 
 | "Inline" 
 | "Autolink" 
 | "Email" 
 | "Unsupported";
 */