arabidopsis / typescript-definitions

Apache License 2.0
69 stars 19 forks source link

ts(1036) error on simple enum #2

Open uonr opened 5 years ago

uonr commented 5 years ago

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: [] };
DrSensor commented 5 years ago

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";
 */