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.

Unexported types causing TypeScript errors 4023 and 7056

See original GitHub issue

What version of Ajv are you using? Does the issue happen if you use the latest version?

8.6.2

TypeScript version

4.3.5

TypeScript config

tsconfig.build.json is a tsconfig intended for building an NPM package. We use this successfully across many projects and with AJV 7.x. It extends a base tsconfig that successfully compiles, presumably because its rootDir is . (project root including node_modules). There’s no scope for changing this config in our org as a workaround for this particular issue.

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "./lib"
  }
}

The tsconfig.json that this extends (compiles successfully):

{
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "lib/**/*"
  ],
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "rootDir": ".",
    "strict": true,
    "typeRoots": [
      "./node_modules/@types",
      "./src"
    ],
    "declaration": true,
    "types": [
      "node"
    ],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "lib": ["ES2019"],
    "noImplicitAny": true,
    "strictNullChecks": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "resolveJsonModule": true,
    "stripInternal": true
  }
}

Your code

This is comparable to a function that we’ve successfully used in AJV 7.x:

import type { JSONSchemaType } from 'ajv'

export const willICompile = <T>(schema: JSONSchemaType<T>): boolean =>
  schema ? false : false

TypeScript error output

src/validation/test.ts:3:14 - error TS4023: Exported variable 'willICompile' has or is using name 'NumberKeywords' from external module "...projectRoot/node_modules/ajv/dist/types/json-schema" but cannot be named.

3 export const willICompile = <T>(schema: JSONSchemaType<T>): boolean =>
               ~~~~~~~~~~~~

src/validation/test.ts:3:14 - error TS4023: Exported variable 'willICompile' has or is using name 'StringKeywords' from external module "...projectRoot/node_modules/ajv/dist/types/json-schema" but cannot be named.

3 export const willICompile = <T>(schema: JSONSchemaType<T>): boolean =>
               ~~~~~~~~~~~~

src/validation/test.ts:3:14 - error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.

3 export const willICompile = <T>(schema: JSONSchemaType<T>): boolean =>
               ~~~~~~~~~~~~

What results did you expect?

I would expect this code to compile.

Various internally used types aren’t exported from the root of the ajv package. My working theory is that when an exported type uses one of these unexported types and the root dir does not include node_modules, we get a 4023 and/or a 7056 error.

We did not experience these errors with equivalent code and the same tsconfig in AJV 7.x.

Incidentally we also had the ability to declare something as a partial schema but this is also no longer possible due to unexported types.

Are you going to resolve the issue?

Could be a rabbit hole with no guarantee any PR would be accepted. Though not ideal I may have to roll my own versions of some types in types/json-schema.ts.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
erikbrinkmancommented, Jul 18, 2021

I get different results from you. neither tsconfig.json nor tsconfig.build.json compile (same errors for each), with the same ajv and typescript versions, which leads me to dismiss the roodDir hypothesis.

I was able to address it by doing what typescript asked:

interface WillICompile {
  <T>(schema: JSONSchemaType<T>): boolean;
}

export const willICompile: WillICompile = (schema) =>schema ? false : false

compiled just fine for me.

JSONSchemaType is right at the border of what typescript is willing to accept in terms of complexity, and I think the step up from 7 to 8 pushed it more towards that direction. My current hypothesis is that JSONSchemaType is just too complex. Potentially typescript was trying to simply the export by referencing NumberKeywords and StringKeywords which it can’t do because they’re internal, and that led to all of the errors. That hypothesis leads to investigating the third error: TS7056, but I’ve only found a handful of cases, and they all seem to reference “complicated” inferred types.

Given that I’m inclined to think it’s good practice manually annotate exported objects anyway, the above fix seems like good practice. From ajv side the fix would be to make JSONSchemaType simpler. I’m generally in favor of a breaking rewrite of that type, but JSONSchema is quite complicated, so even that is not super trivial.

1reaction
mgdigitalcommented, Jul 18, 2021

We do need to be careful using stripInternal - I might look into api-extractor. But I don’t think that’s related to this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Exported variable X has or is using name Y from external ...
Typescript error : "Return type of exported function has or is using name <n> from external module <M> but cannot be named" and...
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