router icon indicating copy to clipboard operation
router copied to clipboard

Generated router with TS errors using new `isolatedDeclarations`

Open SimenB opened this issue 1 year ago • 10 comments

Describe the bug

Using isolatedDeclarations in the TypeScript 5.5 beta gives errors as it's not explicitly typed. Generating (and exporting) the type annotations would be nice.

https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#isolated-declarations

Your Example Website or App

https://github.com/SimenB/router-isolated-declarations

Steps to Reproduce the Bug or Issue

Run the TypeScript compiler in the linked reproduction

Expected behavior

No type errors.

Screenshots or Videos

No response

Platform

N/A

Additional context

No response

SimenB avatar May 18 '24 18:05 SimenB

@chorobin @schiller-manuel is isolatedDeclarations a flag we hope working with TSR?

Looking at the article linked, the solution to this seems to be the use of explicit types (and return types).

isolatedDeclarations should be adopted on a case-by-case basis.

They do also mention this though 👆🏼.

Should we be looking at this now? or should we wait for Typescript 5.5 to be released and then added into our type-matrix in CI?

SeanCassiere avatar May 21 '24 04:05 SeanCassiere

Note that this issue is specifically about the code generated by @tanstack/router-generator output into the user's code base, not necessarily the router's own code base.

(in theory, this might improve type checking performance as well as noted in https://typescript-eslint.io/rules/explicit-module-boundary-types/, but my point was mainly to help/not block users from using the flag)

SimenB avatar May 21 '24 13:05 SimenB

5.5 is out btw, and this issue is blocking me from adopting the new flag

SimenB avatar Jun 30 '24 15:06 SimenB

how would the generated code need to change to support isolatedDeclarations?

schiller-manuel avatar Jun 30 '24 16:06 schiller-manuel

The exported value (i.e. routeTree) would need to be explicitly typed rather than inferred.

SimenB avatar Jun 30 '24 19:06 SimenB

that's not possible AFAIK

schiller-manuel avatar Jun 30 '24 19:06 schiller-manuel

Why is that not possible? If it's able to be inferred, it should be possible to generated as well.

If nothing else, you can use tsc itself to output the explicit types.

image

SimenB avatar Jul 05 '24 10:07 SimenB

@chorobin If we adjust the route-tree template output to infer from the global module declaration types, would this solve this?

SeanCassiere avatar Jul 05 '24 10:07 SeanCassiere

@chorobin we recently spoke about exporting everything and about exporting everything from the roue-tree as well.

Would this be caught by tsc --noEmit? If so, could we add an example that uses this isolatedDeclarations option and have this be a part of our integration process?

SeanCassiere avatar Sep 16 '24 05:09 SeanCassiere

@chorobin we recently spoke about exporting everything and about exporting everything from the roue-tree as well.

Would this be caught by tsc --noEmit? If so, could we add an example that uses this isolatedDeclarations option and have this be a part of our integration process?

Do you mean using isolatedDeclarations as a way for us to catch these errors?

Currently I don't see users using this option as you'd need to annotate every route definition which of course we could generate and they could use but you lose out a lot on DX due to no inference and just everything requiring generation

chorobin avatar Sep 16 '24 16:09 chorobin