winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.02k stars 197 forks source link

Proper support for Enum interoperability with TypeScript #6742

Open asterkin opened 4 months ago

asterkin commented 4 months ago

Use Case

I developed a Wing extension code in TypeScript that uses Enums. Enums can not be imported from TypeScript to Wing and passed from Wing to TypeScript as strings. You may find examples of enforced workarounds here: https://github.com/asterkin/endor.w/tree/main/datetimex

Proposed Solution

Support the extern keyword for Enums as it's done for functions.

Implementation Notes

No response

Component

Compiler

Community Notes

Chriscbr commented 3 months ago

A minimal example showing the current discrepancy will be helpful here (I'm unable to view the code linked in the issue)

asterkin commented 3 months ago

@Chriscbr added you to the endor.w GitHub Repo. Here is the code: //Cannot be imported from TS pub enum DatetimeFormat { ISO, MMYYYY, YYYYMMDD_HHMM }

pub class Util { extern "./lib.js" static pub inflight toUtcString(date: std.Datetime): str; extern "./lib.js" static pub inflight plus(date: std.Datetime, offset: duration): std.Datetime; extern "./lib.js" static pub inflight minus(date: std.Datetime, offset: duration): std.Datetime; extern "./lib.js" static pub inflight format(date: std.Datetime, format: DatetimeFormat): str; extern "./lib.js" static pub inflight parse(date: str, format: DatetimeFormat): std.Datetime; extern "./lib.js" static pub inflight formatMMYYYY(date: std.Datetime): str; extern "./lib.js" static pub inflight parseMMYYYY(sd: str): std.Datetime; extern "./lib.js" static pub inflight formatYYYYMMDD_HHMM(date: std.Datetime): str; extern "./lib.js" static pub inflight parseYYYYMMDD_HHMM(sd: str): std.Datetime;

static pub inflight gt(left: std.Datetime, right: std.Datetime): bool {
    return left.timestampMs > right.timestampMs;
}
static pub inflight gte(left: std.Datetime, right: std.Datetime): bool {
    return left.timestampMs >= right.timestampMs;
}
static pub inflight lt(left: std.Datetime, right: std.Datetime): bool {
    return left.timestampMs < right.timestampMs;
}
static pub inflight lte(left: std.Datetime, right: std.Datetime): bool {
    return left.timestampMs <= right.timestampMs;
}

} In TypeScript, I need to do this: //Wing passes enums as strings, hence need extra validation export function getParser(format: DatetimeFormat | string): ParseDatetime { const parser = _parser.get( typeof format == 'string' ? DatetimeFormat[format as keyof typeof DatetimeFormat] : format );

if (!parser) {
    throw new Error(`Parser for format ${format} not found`);
}
return parser;

}

export function parse(dateTime: string, format: DatetimeFormat): std.Datetime { return getParser(format)(dateTime); }

export function getFormatter(format: DatetimeFormat | string): FormatDatetime { const formatter = _formatter.get( typeof format == 'string' ? DatetimeFormat[format as keyof typeof DatetimeFormat] : format ); if (!formatter) { throw new Error(Formatter for format ${format} not found); } return formatter; }

export function format(dateTime: std.Datetime, format: DatetimeFormat): string { return getFormatter(format)(dateTime); }

github-actions[bot] commented 3 weeks ago

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!