api-platform / api-platform/create-client

Next.js: type errors with ReferenceLinks

Aperta
#372 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
TypeScript
Stelle
376
Fork
132
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

**create-client version(s) affected**: 0.10.0

**Description**

After generating an app in the pwa container derived from the standard distribution it works in the development environment but when running
```shell
docker compose exec pwa pnpm next build
```
i get the following type errors:
1. ./components/employee/List.tsx employee["hours"] is possibly 'undefined'.
2. the same error for ./components/employee/Show.tsx
3. ./components/hours/List hours["employee"] Type '{ href: string; name: string | undefined; }' is not assignable to type 'string'
4. The same error is in ./components/hours/Show.tsx

Causes:
1. List component ReferenceLinks if isReferences (and isEmbeddeds?)

This happens because templates/next/types/foo.ts defines all properties as optional so that pwa/types/Employee.ts is generated to define hours like this:
```javascript ts
public hours?: string[],
```
while on line 42-43 of templates/next/components/foo/List.tsx an undefined field is not addressed:
```javascript tsx
{{#if isReferences}}
({ href: getItemPath(ref, '/{{{lowercase reference.title}}}s/[id]'), name: ref })) } />
```

2. Show component ReferenceLinks if isReferences (and isEmbeddeds?)

Like 1, on line 57-58 of templates/next/components/foo/Show.tsx

3. List component ReferenceLinks if isReference (and isEmbedded?)

This happens because templates/next/types/foo.ts defines all properties as optional while
on line 44-45 of templates/next/components/foo/List.tsx an undefined field is not addressed:
{{else if reference}}

4. Show component ReferenceLinks if isReference (and isEmbedded?)

Similar to 3, line on line 57-58 of templates/next/components/foo/Show.tsx

**How to reproduce**

Clone MetaClass' Tutorial Api Platform repository using git:
```shell
git clone https://github.com/metaclass-nl/tutorial-api-platform.git
```

check out branch chapter2-next

start the containers with
```shell
docker compose up
```

The api container should execute the data base migrations automatically. If not do:
```shell
docker compose exec php ./bin/console doctrine:migrations:migrate
```

To clear the database and execute the fixtures enter the following command:
```shell
docker compose exec php bin/console doctrine:fixtures:load
```

Follow the instructions from readme.md. or point your browser to the [same branch on github](https://github.com/metaclass-nl/tutorial-api-platform/tree/chapter2-next)
and follow the instructions.

The paragraph "Checking types" describes how to reprocude this error as well as serveral others for wich seperate issues are created.

**Possible Solution**

The simpelest soloution is to change the type templates/next/components/ReferenceLinks.tsx
```javascript ts
interface Props {
items:
```
to allow undefined items and item names.

Alternatively each of the values passed to items can be checked of undefined, like is done in paragraph "Checking types" of the readme of the tutorial branch chapter2-next

**Additional Context**

Api Platform version v3.1.3

typescript config see [pnpm-lock.yaml](https://github.com/metaclass-nl/tutorial-api-platform/blob/chapter2-next/pwa/pnpm-lock.yaml) and [tsconfig.json](https://github.com/metaclass-nl/tutorial-api-platform/blob/chapter2-next/pwa/tsconfig.json)

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.