From 0fdd9e75a6c639e46966d812cb80f4865a743a2f Mon Sep 17 00:00:00 2001 From: garronej Date: Tue, 21 Mar 2023 14:21:09 +0100 Subject: [PATCH] Fix the helper type that enable to extend the KcContext --- src/account/kcContext/getKcContext.ts | 2 +- .../kcContext/getKcContextFromWindow.ts | 2 +- src/account/kcContext/kcContextMocks.ts | 2 +- src/tools/AndByDiscriminatingKey.ts | 6 +- test/lib/tools/AndByDiscriminatingKey.type.ts | 126 ++++++++++-------- 5 files changed, 80 insertions(+), 58 deletions(-) diff --git a/src/account/kcContext/getKcContext.ts b/src/account/kcContext/getKcContext.ts index ed793333..79e224f1 100644 --- a/src/account/kcContext/getKcContext.ts +++ b/src/account/kcContext/getKcContext.ts @@ -1,4 +1,3 @@ -import { kcContextMocks, kcContextCommonMock } from "./kcContextMocks"; import type { DeepPartial } from "keycloakify/tools/DeepPartial"; import { deepAssign } from "keycloakify/tools/deepAssign"; import type { ExtendKcContext } from "./getKcContextFromWindow"; @@ -7,6 +6,7 @@ import { pathJoin } from "keycloakify/bin/tools/pathJoin"; import { pathBasename } from "keycloakify/tools/pathBasename"; import { mockTestingResourcesCommonPath } from "keycloakify/bin/mockTestingResourcesPath"; import { symToStr } from "tsafe/symToStr"; +import { kcContextMocks, kcContextCommonMock } from "keycloakify/account/kcContext/kcContextMocks"; export function getKcContext(params?: { mockPageId?: ExtendKcContext["pageId"]; diff --git a/src/account/kcContext/getKcContextFromWindow.ts b/src/account/kcContext/getKcContextFromWindow.ts index 799dad34..dc221d39 100644 --- a/src/account/kcContext/getKcContextFromWindow.ts +++ b/src/account/kcContext/getKcContextFromWindow.ts @@ -1,6 +1,6 @@ -import type { KcContext } from "./KcContext"; import type { AndByDiscriminatingKey } from "keycloakify/tools/AndByDiscriminatingKey"; import { ftlValuesGlobalName } from "keycloakify/bin/keycloakify/ftlValuesGlobalName"; +import type { KcContext } from "./KcContext"; export type ExtendKcContext = [KcContextExtension] extends [never] ? KcContext diff --git a/src/account/kcContext/kcContextMocks.ts b/src/account/kcContext/kcContextMocks.ts index 7915006d..cc1b2fdc 100644 --- a/src/account/kcContext/kcContextMocks.ts +++ b/src/account/kcContext/kcContextMocks.ts @@ -1,8 +1,8 @@ import "minimal-polyfills/Object.fromEntries"; -import type { KcContext } from "./KcContext"; import { mockTestingResourcesCommonPath, mockTestingResourcesPath } from "keycloakify/bin/mockTestingResourcesPath"; import { pathJoin } from "keycloakify/bin/tools/pathJoin"; import { id } from "tsafe/id"; +import type { KcContext } from "./KcContext"; const PUBLIC_URL = process.env["PUBLIC_URL"] ?? "/"; diff --git a/src/tools/AndByDiscriminatingKey.ts b/src/tools/AndByDiscriminatingKey.ts index 637ab87d..630c6d13 100644 --- a/src/tools/AndByDiscriminatingKey.ts +++ b/src/tools/AndByDiscriminatingKey.ts @@ -10,7 +10,11 @@ export declare namespace AndByDiscriminatingKey { U1, U1Again extends Record, U2 extends Record - > = U1 extends Pick ? Tf2 : U1; + > = U1 extends Pick + ? Tf2 + : U1Again[DiscriminatingKey] & U2[DiscriminatingKey] extends never + ? U1 | U2 + : U1; export type Tf2< DiscriminatingKey extends string, diff --git a/test/lib/tools/AndByDiscriminatingKey.type.ts b/test/lib/tools/AndByDiscriminatingKey.type.ts index 919662dc..f45e897f 100644 --- a/test/lib/tools/AndByDiscriminatingKey.type.ts +++ b/test/lib/tools/AndByDiscriminatingKey.type.ts @@ -2,72 +2,90 @@ import { AndByDiscriminatingKey } from "../../../src/tools/AndByDiscriminatingKe import { assert } from "tsafe/assert"; import type { Equals } from "tsafe"; -type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string }; +{ + type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string }; -type Extension = { pageId: "a"; onlyExtA: string } | { pageId: "b"; onlyExtB: string } | { pageId: "only ext"; onlyExt: string }; + type Extension = { pageId: "a"; onlyExtA: string } | { pageId: "b"; onlyExtB: string } | { pageId: "only ext"; onlyExt: string }; -type Got = AndByDiscriminatingKey<"pageId", Extension, Base>; + type Got = AndByDiscriminatingKey<"pageId", Extension, Base>; -type Expected = - | { pageId: "a"; onlyA: string; onlyExtA: string } - | { pageId: "b"; onlyB: string; onlyExtB: string } - | { pageId: "only base"; onlyBase: string } - | { pageId: "only ext"; onlyExt: string }; + type Expected = + | { pageId: "a"; onlyA: string; onlyExtA: string } + | { pageId: "b"; onlyB: string; onlyExtB: string } + | { pageId: "only base"; onlyBase: string } + | { pageId: "only ext"; onlyExt: string }; -assert>(); + assert>(); -const x: Got = null as any; + const x: Got = null as any; -if (x.pageId === "a") { - x.onlyA; - x.onlyExtA; + if (x.pageId === "a") { + x.onlyA; + x.onlyExtA; - //@ts-expect-error - x.onlyB; + //@ts-expect-error + x.onlyB; - //@ts-expect-error - x.onlyBase; + //@ts-expect-error + x.onlyBase; - //@ts-expect-error - x.onlyExt; + //@ts-expect-error + x.onlyExt; + } + + if (x.pageId === "b") { + x.onlyB; + x.onlyExtB; + + //@ts-expect-error + x.onlyA; + + //@ts-expect-error + x.onlyBase; + + //@ts-expect-error + x.onlyExt; + } + + if (x.pageId === "only base") { + x.onlyBase; + + //@ts-expect-error + x.onlyA; + + //@ts-expect-error + x.onlyB; + + //@ts-expect-error + x.onlyExt; + } + + if (x.pageId === "only ext") { + x.onlyExt; + + //@ts-expect-error + x.onlyA; + + //@ts-expect-error + x.onlyB; + + //@ts-expect-error + x.onlyBase; + } } -if (x.pageId === "b") { - x.onlyB; - x.onlyExtB; +{ + type Base = { pageId: "a"; onlyA: string } | { pageId: "b"; onlyB: string } | { pageId: "only base"; onlyBase: string }; - //@ts-expect-error - x.onlyA; + type Extension = { pageId: "only ext"; onlyExt: string }; - //@ts-expect-error - x.onlyBase; + type Got = AndByDiscriminatingKey<"pageId", Extension, Base>; - //@ts-expect-error - x.onlyExt; -} - -if (x.pageId === "only base") { - x.onlyBase; - - //@ts-expect-error - x.onlyA; - - //@ts-expect-error - x.onlyB; - - //@ts-expect-error - x.onlyExt; -} - -if (x.pageId === "only ext") { - x.onlyExt; - - //@ts-expect-error - x.onlyA; - - //@ts-expect-error - x.onlyB; - - //@ts-expect-error - x.onlyBase; + type Expected = + | { pageId: "a"; onlyA: string } + | { pageId: "b"; onlyB: string } + | { pageId: "only base"; onlyBase: string } + | { pageId: "only ext"; onlyExt: string }; + + assert>(); }