Skip to content

TagOfの実装が間違っている #89

@sidepelican

Description

@sidepelican

以下の入力をして、AAAA~CCCCの型がどうなっているか見てみるといろいろ壊れてることに気づくと思います。

struct User: Codable {
  var name: String
}

struct GenericID<T>: RawRepresentable {
  var rawValue: String
}

public typealias AAAA = GenericID<[User]>
public typealias BBBB = GenericID<User?>
public typealias CCCC = GenericID<[String: User]>

いろいろ試したところ、以下の形が正しそうです

  1. 最初のConditional Typeでは[]で要素数1のタプル化を行う。T | null が分割されるのを防ぐ。
  2. 各々の派生タグを[]で囲む。ジェネリックパラメータのタグはTagRecord側でタプル型にまとまることになっていて、ルールがあっていませんでした
type TagOf<Type> = [Type] extends [TagRecord<infer TAG>]
    ? TAG
    : null extends Type
        ? "Optional" & [TagOf<Exclude<Type, null>>]
        : Type extends (infer E)[]
            ? "Array" & [TagOf<E>]
            : Type extends Map<infer K, infer V>
                ? "Dictionary" & [K, TagOf<V>]
                : never;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions