I'm mostly opening this issue to document the type errors below. For some of these, I'll open a PR and push the changes mentioned. Others are more involved and difficult to sort out.
There may be more type errors, also, but these were the ones I was able to work through in the last 4 or 5 hours. If I find more, I'll add them to the list.
Issues:
When running pnpm build in Next.js 14.0.4 I run into the following Type Errors:
1) Page "src/app/[[...paths]]/page.tsx" has an invalid "generateMetadata" export: Type GenerateMetadataParams is not valid.
This seems to be referring to wpUrl: string; in the type.
2) Type error: Object is possibly 'undefined'.
This is referring to the function getAllItems.
3) Type error: Object is possibly 'undefined'.
This is referring to the if branch in the function getAllItems.
4) Type error: Variable 'allData' implicitly has type 'any[]' in some locations where its type cannot be determined.
This one seems to be fixed by simply adding the Items type to allData.
5) Type error: 'menu' is possibly 'undefined'.
This one is referring to the fetch call using menus[0].
6) Type error: 'menu' is possibly 'undefined'.
This one is the same as above but in the error thrown.
7) Type error: 'err' is of type 'unknown'.
This one is a mystery to me, especially since it isn't throwing the error for the exact same line above it with menu.id instead of slug.
8) Type error: 'err' is of type 'unknown'.
This is the same as above in a different function, except that it seems to be solved when I try err as any.
9) Type error: Type 'SingleItem' is not assignable to type '{ data?: WpPage | undefined; previewData?: WpRevision | undefined; } | undefined'.
The first screenshot is of the console error, and the second is a prettified error message.
10) Type error: Object is possibly 'undefined'.
This one seems to be solved with the String() function and optional chaining for lines 42, 43, 52, and 54; and optional chaining for 46.
11) Type error: 'err' is of type 'unknown'.
This is the same as above in numbers 7 & 8, except that it seems to be solved when I try err as any.
12) Type error: 'postType' is possibly 'undefined'.
13) Type error: Property 'yoast_head_json' does not exist on type 'WpPage | ArchivePageData | TaxonomyPageData'.
14) Type error: Binding element 'postTypes' implicitly has an 'any' type.
This seems to be fixed when adding typing postTypes as string[].
15) Type error: Type 'number | undefined' is not assignable to type 'number'.
This seems to be solved by using the Number() function on line 25 and 51.
16) Type error: Object is possibly 'undefined'.
This one seems to be fixed with optional chaining.
17) Type error: Type 'undefined' cannot be used as an index type.
This one seems fixed by using the String() function.
18) Type error: 'matchingPostType' is possibly 'undefined'.
19) Type error: Binding element 'archiveSlug' implicitly has an 'any' type.
20) Type error: Object is possibly 'undefined'.
21) Type error: 'matchingPostType' is possibly 'undefined'.
22) Type error: Type '{ totalPages: number | undefined; totalItems: number | undefined; }' is not assignable to type '{ totalPages: number; totalItems: number; }'.
This one seems fixed by allowing totalPages and totalItems to be undefined on lines 142/143 and by using the Number() function in line 148.
23) Type error: 'err' is of type 'unknown'.
24) Type error: 'templates.archive' is possibly 'undefined'.
I'm mostly opening this issue to document the type errors below. For some of these, I'll open a PR and push the changes mentioned. Others are more involved and difficult to sort out.
There may be more type errors, also, but these were the ones I was able to work through in the last 4 or 5 hours. If I find more, I'll add them to the list.
Issues: When running
pnpm build
in Next.js 14.0.4 I run into the following Type Errors:1) Page "src/app/[[...paths]]/page.tsx" has an invalid "generateMetadata" export: Type
GenerateMetadataParams
is not valid. This seems to be referring towpUrl: string;
in the type.2) Type error: Object is possibly 'undefined'. This is referring to the function
getAllItems
.3) Type error: Object is possibly 'undefined'. This is referring to the if branch in the function
getAllItems
.4) Type error: Variable 'allData' implicitly has type 'any[]' in some locations where its type cannot be determined. This one seems to be fixed by simply adding the
Items
type toallData
.5) Type error: 'menu' is possibly 'undefined'. This one is referring to the fetch call using
menus[0]
.6) Type error: 'menu' is possibly 'undefined'. This one is the same as above but in the error thrown.
7) Type error: 'err' is of type 'unknown'. This one is a mystery to me, especially since it isn't throwing the error for the exact same line above it with![Screenshot from 2024-03-21 23-14-25](https://github.com/CalebBarnes/nextwp/assets/50815816/e7bba941-05be-46bf-9e32-2866f4c7324b)
menu.id
instead ofslug
.8) Type error: 'err' is of type 'unknown'. This is the same as above in a different function, except that it seems to be solved when I try![Screenshot from 2024-03-21 23-20-38](https://github.com/CalebBarnes/nextwp/assets/50815816/2abe07d8-c8c6-4ba4-8a46-079f3691fe90)
err
asany
.9) Type error: Type 'SingleItem' is not assignable to type '{ data?: WpPage | undefined; previewData?: WpRevision | undefined; } | undefined'. The first screenshot is of the console error, and the second is a prettified error message.
10) Type error: Object is possibly 'undefined'. This one seems to be solved with the String() function and optional chaining for lines 42, 43, 52, and 54; and optional chaining for 46.![Screenshot from 2024-03-21 23-25-56](https://github.com/CalebBarnes/nextwp/assets/50815816/ccf1ef52-633f-44eb-a101-f22741630fa2)
11) Type error: 'err' is of type 'unknown'. This is the same as above in numbers 7 & 8, except that it seems to be solved when I try![Screenshot from 2024-03-21 23-54-08](https://github.com/CalebBarnes/nextwp/assets/50815816/018dbdf6-0ead-4559-9dbc-320879fcee02)
err
asany
.12) Type error: 'postType' is possibly 'undefined'.
13) Type error: Property 'yoast_head_json' does not exist on type 'WpPage | ArchivePageData | TaxonomyPageData'.
14) Type error: Binding element 'postTypes' implicitly has an 'any' type. This seems to be fixed when adding typing postTypes as![Screenshot from 2024-03-22 00-09-35](https://github.com/CalebBarnes/nextwp/assets/50815816/1305732e-6866-4efa-85c7-eb81a3531262)
string[]
.15) Type error: Type 'number | undefined' is not assignable to type 'number'. This seems to be solved by using the Number() function on line 25 and 51.![Screenshot from 2024-03-22 00-14-45](https://github.com/CalebBarnes/nextwp/assets/50815816/dba70cd7-e926-4e81-b9c2-fe30454c0598)
16) Type error: Object is possibly 'undefined'. This one seems to be fixed with optional chaining.
17) Type error: Type 'undefined' cannot be used as an index type. This one seems fixed by using the String() function.![Screenshot from 2024-03-22 00-17-45](https://github.com/CalebBarnes/nextwp/assets/50815816/01f6bce3-d0be-40b9-970d-b02f233b22f7)
18) Type error: 'matchingPostType' is possibly 'undefined'.
19) Type error: Binding element 'archiveSlug' implicitly has an 'any' type.
20) Type error: Object is possibly 'undefined'.
21) Type error: 'matchingPostType' is possibly 'undefined'.
22) Type error: Type '{ totalPages: number | undefined; totalItems: number | undefined; }' is not assignable to type '{ totalPages: number; totalItems: number; }'. This one seems fixed by allowing![Screenshot from 2024-03-22 01-36-36](https://github.com/CalebBarnes/nextwp/assets/50815816/486ba48f-7292-40b6-bdc8-ffb683581944)
totalPages
andtotalItems
to be undefined on lines 142/143 and by using the Number() function in line 148.23) Type error: 'err' is of type 'unknown'.
24) Type error: 'templates.archive' is possibly 'undefined'.