Skip to content

Conversation

@omochi
Copy link
Owner

@omochi omochi commented Jan 1, 2025

従来、空のenumはneverにトランスパイルしていた。
しかしこれには型タグが付いていないという問題があり、
以下のようなSwiftコードで問題があった。

struct GenericID<Tag> { ... }

enum FooTag {}
typealias FooID = GenericID<FooTag>

一見、neverに型タグを付ければ良さそうだが、
ここでTypeScriptコンパイラが特殊な挙動をしてしまい、
仮に以下のようにコード生成しても

type FooTag = never & TagRecord<"FooTag">;
type FooID = GenericID<FooTag>;

FooID のタグは期待される

"GenericID", ["FooTag"]

とはならず

"GenericID", [string]

となってしまう。
これではタグ型がタグとして機能しないので困る。

そこでこのパッチでは、代わりに

type FooTag = void & TagRecord<"FooTag">;

とトランスパイルするモードを追加する。
これだと期待通りタグが機能する。

従来の never への変換は、
それはそれで型の値が存在しないことを意味する点で望ましいため、
仕様変更をせずにデフォルトの挙動として維持する。

新しいvoidへの変換は
EnumConverter に指定するAPIオプションとして提供する。

ユーザーはこれを custom type converter を使用して、
特定の enum 型の変換の挙動として指定することができる。

またこの際、
全ての enum 型についての挙動を変更することもできるように、
デフォルトの type converter 選択を直接再利用できるようにAPI化した。
テストケースではそのようなセットアップをした。

}

return nil
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デフォルトの変換処理が EnumConverter を採用した場合に、
モードを変更した新しい EnumConverter に差し替える

Base automatically changed from avoid-conflict-name to main January 6, 2025 06:27
@omochi omochi merged commit 755efb9 into main Jan 6, 2025
1 check passed
@omochi omochi deleted the empty-enum-void branch January 6, 2025 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant