#It is mandatory to use kind at root level in a model?

4 messages · Page 1 of 1 (latest)

short summit
#

Hi all, at work we do use Adobe Experience Manager and they have something called Content Fragment where we generate JSON at the backend and then read them using NextJS and ReactJS in the frontend. This content fragments seems to have a restriction as per the backend team (I do not work directly with AEM) and is they cannot add a property at root level without any child. For example:

// not allowed
{
    "kind": "some"
}

// allowed
{
    discriminatorProperty: {
        "kind": "some";
    }
}

having said that I come up with this "solution":

// signup.model
interface A {
    discriminatorProperty: {
        kind: 'a';
    },
    someA: {
        a: string
    }
}

interface B {
    discriminatorProperty: {
        kind: 'b';
    },
    someB: {
        b: string
        c: number
    }
}

// signUp.tsx
const myFunction = ({param}:{param: A|B}) => {
    const canRender: boolean = true; // this is coming from an ENV variable
    return (
        <>
            {canRender && (param.discriminatorProperty.kind === 'a' ? param.someA : param.someB.c)}
        </>
    )
}

but TS is complaining because it does not recognize someA or someB under A or B interfaces ... I wonder if kind has to be at root level or if I am missing something here?

muted ravine
#

the check only narrows param.discriminatorProperty, not param, unfortunately

#

you could have something like this though

surreal gobletBOT
#
that_guy977#0

Preview:```ts
interface A {
data: {
kind: "a"
someA: {
a: string
}
}
}

interface B {
data: {
kind: "b"
someB: {
b: string
c: number
}
}
}

// signUp.tsx
const myFunction = ({
param: {data},
}: {
param: A | B
}) => {
...```