typescript-definitions icon indicating copy to clipboard operation
typescript-definitions copied to clipboard

ts(1036) error on simple enum

Open uonr opened this issue 6 years ago • 1 comments

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: [] };

uonr avatar Apr 25 '19 05:04 uonr

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

DrSensor avatar Jul 14 '19 02:07 DrSensor