Fix the helper type that enable to extend the KcContext

This commit is contained in:
garronej 2023-03-21 14:21:09 +01:00
parent 77da00c2c5
commit 0fdd9e75a6
5 changed files with 80 additions and 58 deletions

View File

@ -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<KcContextExtension extends { pageId: string } = never>(params?: {
mockPageId?: ExtendKcContext<KcContextExtension>["pageId"];

View File

@ -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 { pageId: string }> = [KcContextExtension] extends [never]
? KcContext

View File

@ -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"] ?? "/";

View File

@ -10,7 +10,11 @@ export declare namespace AndByDiscriminatingKey {
U1,
U1Again extends Record<DiscriminatingKey, string>,
U2 extends Record<DiscriminatingKey, string>
> = U1 extends Pick<U2, DiscriminatingKey> ? Tf2<DiscriminatingKey, U1, U2, U1Again> : U1;
> = U1 extends Pick<U2, DiscriminatingKey>
? Tf2<DiscriminatingKey, U1, U2, U1Again>
: U1Again[DiscriminatingKey] & U2[DiscriminatingKey] extends never
? U1 | U2
: U1;
export type Tf2<
DiscriminatingKey extends string,

View File

@ -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<Equals<Got, Expected>>();
assert<Equals<Got, Expected>>();
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<Equals<Got, Expected>>();
}