Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

697 - Tag #3768

Open
ianbunag opened this issue Oct 8, 2021 · 0 comments
Open

697 - Tag #3768

ianbunag opened this issue Oct 8, 2021 · 0 comments
Labels
697 answer Share answers/solutions to a question en in English

Comments

@ianbunag
Copy link

ianbunag commented Oct 8, 2021

Try me!

Expectation changes

"Has tags" predicate assertion changed to be non positional

Current assertions will only pass if tags argument order matches tagging order

type Cases = [
  // Should only pass if tags arguments is ["bar", "foo"]
  Expect<Equal<HasTags<Tag<Tag<{}, "bar">, "foo">, ["foo", "bar"]>, false>>,
  // Should only pass if tags arguments is ["foo", "baz"] or ["foo", "baz", 'bar]
  Expect<Equal<HasTags<Tag<Tag<Tag<{}, "foo">, "baz">, "bar">, ["foo", "bar"]>, false>>,
]

Solution will only fail if a non existent tag is provided

type Cases = [
  // Will pass for ["foo", "bar", "baz"], ["baz", "bar", "foo"], ...etc
  Expect<Equal<HasTags<Tag<Tag<Tag<{}, "foo">, "baz">, "bar">, ["foo", "bar"]>, true>>,
]

Solution

type Flatten<Type> = { [Key in keyof Type]: Type[Key] }

type UnionToIntersection<Type> = (
  Type extends any ? (wrapper: Type) => unknown :never
) extends (wrapper: infer WrappedType) => unknown ? WrappedType : never

type UnionToTuple<Type> = UnionToIntersection<
  Type extends any ? (wrapper: Type) => unknown :never
> extends (wrapper: infer WrappedType) => unknown
  ? [...UnionToTuple<Exclude<Type, WrappedType>> ,WrappedType]
  : []

/**
 * Copy of:
 *  import { Equal } from "@type-challenges/utils";
 */
type Equivalent<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? true : false

declare const TagKey: unique symbol;

type Injectable<Target> = typeof TagKey extends keyof Target
  ? Omit<Target, never> // Preserve special types like any, unknown and never
  : Target

type InjectTag<Target, Type = {}> = Injectable<Target> & { [TagKey]?: Type }

type Tag<
  Target,
  Type extends string
> = Equivalent<Target, null> extends true
  ? null
  : Equivalent<Target, undefined> extends true
    ? undefined
    : Injectable<Target> extends Injectable<InjectTag<infer RootTarget, infer RootType>>
      ? InjectTag<
          RootTarget,
          Flatten<
            { [Key in keyof RootType]?: RootType[Key]} &
            { [Key in Type]?: Type } &
            Record<string, string>
          >
        >
      : InjectTag<
          Target,
          Flatten<
            { [Key in Type]?: Type } &
            Record<string, string>
          >
        >

type GetTagUnion<
  Target
> = Target extends InjectTag<unknown, infer Tags>
  ? keyof { [Key in keyof Tags as string extends Key ? never : Key]: Tags[Key] }
  : never

type GetCommonTagUnion<
  Target,
  TargetTuple = UnionToTuple<Target>
> = TargetTuple extends [infer CurrentTarget, ...infer RestTarget]
  ? RestTarget extends []
    ? GetTagUnion<CurrentTarget>
    : GetTagUnion<CurrentTarget> & GetCommonTagUnion<RestTarget[number]>
  : never

type GetTags<Target> = UnionToTuple<GetCommonTagUnion<Target>>

type UnTag<Target> = keyof Target extends never
  ? Target
  : Omit<Target, typeof TagKey>

type HasTag<
  Target,
  Type extends string
> = Type extends GetCommonTagUnion<Target>
  ? true
  : false

type HasTags<
  Target,
  Type extends ReadonlyArray<string>
> = Type[number] extends GetCommonTagUnion<Target>
  ? true
  : false

type HasExactTags<
  Target,
  Type extends ReadonlyArray<string>
> = GetCommonTagUnion<Target> extends Type[number]
  ? true
  : false
@ianbunag ianbunag added answer Share answers/solutions to a question en in English labels Oct 8, 2021
@github-actions github-actions bot added the 697 label Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
697 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant