question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any better workarounds for TS7056?

See original GitHub issue

I’m responsible for a project with pretty complex schemas. We’re running into a lot of TS compile errors like this:

error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.

The best workaround I’ve found is to annotate schema declarations with zod types ahead of time (rather than using z.infer):

export type ZBlockEntity = z.ZodUnion<
  [
    typeof paragraphPropsOrElement,
    typeof layoutPropsOrElement,
    ...
    typeof escapeElement
  ]
>
export const blockEntity: ZBlockEntity = z.union([
  paragraphPropsOrElement,
  layoutPropsOrElement,
  ...
  escapeElement,
])

TypeScript will declare the array of typeof name referentially with this explicit annotation, otherwise it repeats a literal of each schema in the union.

The example I gave was simple, but some schemas that are hitting TS7056 are very tricky to mint and maintain explicit type annotations for, and even if it’s achievable it will greatly increase technical inertia for the features that hit that wall.

I’d like to find a way to compile schemas in a way that TypeScript can declare and reuse named types instead of it repeating the same literals so many times it breaks.

Is there a workable solution here, or have we hit a wall?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thurecommented, May 10, 2022

I’m not sure it would… regardless of the value of declaration, the project needs to build type declarations since it’s an intermediary package.

Maybe a top-level app could avoid emitting declarations, but this project can’t.

1reaction
thurecommented, Mar 23, 2022

The project is open source, yes. We’re getting TS7056 in these examples:

There are several others. You can see them by running pnpm build from packages/react (I think there was also one in packages/schemas) at this commit in the project: https://github.com/OfficeDev/fluent-blocks/tree/536f8dceccfc8142438f02d8aa6530977c76879b (it’s the moment I discovered the issue before starting to try to fix it).

TS7056 happens mostly when you’re putting a lot of schemas and other types into the same namespace

Letting TS7056 be the test for whether a name shares a file with another feels wrong-headed to me. You can see in the examples we’re not exporting very much from the package, it’s just very deeply composed.

That composition is why I’m asking, is there a way to cause tsc to refer to types rather than repeat literals? This seems to be the real issue.

As it is, maybe rather than the source of truth coming from zod, it should start with TypeScript, and we should use ts-to-zod to produce & refine schemas. I am experimenting with that now, since it appears to be the only way to ensure tsc creates and uses an identifier for the types that need them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing the TS7016 Error | Atomist Blog
Could not find declaration file -- How do you get past the TS7016? This article highlights the eight best and worst fixes.
Read more >
Typescript: "The inferred type of this node exceeds the ...
I had a more perfect version, but cut a ton of corners to get it simplified. In my "demo" it's extremely fast with...
Read more >
Car Hacking: For Poories - IOActive
With this simple setup, you can begin to work on problems like figuring out what types of protocols the ECU understands, getting security...
Read more >
7 really good reasons not to use TypeScript - everyday.codes
Sure wish they'd just add types to js itself. “let int x = 3” or something like that. Would sure solve a lot...
Read more >
Official Gazette of the United States Patent and Trademark ...
WATER SUPPLY AIR SUPPLY IV CONTROL TO INTERCEPT VALVE 81 82 84 HP BYPASS CONTROL HP BYPASS DESUPERHEAT CONTROL TO LP BYPASS CONTROL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found