Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error TS4023: Exported variable has or is using name 'NumberKeywords' from external module node_modules/ajv/dist/types/json-schema but cannot be named. #1814

Closed
hadrien-toma opened this issue Nov 12, 2021 · 8 comments

Comments

@hadrien-toma
Copy link
Contributor

hadrien-toma commented Nov 12, 2021

I built a library to wrap Ajv features but I don't reach to build it because of the Error TS4023: Exported variable has or is using name 'NumberKeywords' from external module node_modules/ajv/dist/types/json-schema but cannot be named..

I made a reproducible example available here.

Basically I would like to have this library building:

image

Here are the commands I used to build this repository:

npx create-nx-workspace@latest workspace --preset=empty --cli=nx --interactive=false --nx-cloud=false --packageManager=yarn --skipGit

cd workspace

yarn add \
	ajv \
	ajv-formats

yarn add --dev \
	@angular/cli \
	@nrwl/node \
    json

node_modules/.bin/json --in-place -f package.json -e "this.scripts['json'] = 'json';"
yarn run json --in-place -f package.json -e "this.scripts['nx'] = 'nx';"

yarn run nx generate @nrwl/node:library --directory=json-schemas --name=ajv --buildable --publishable --strict --importPath="@workspace/json-schemas-ajv" --standaloneConfig

yarn run json --in-place -f tsconfig.base.json -e "this.angularCompilerOptions = {};"
yarn run json --in-place -f tsconfig.base.json -e "this.angularCompilerOptions.fullTemplateTypeCheck = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.angularCompilerOptions.strictTemplates = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.angularCompilerOptions.trace = true;"

yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['allowSyntheticDefaultImports'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['alwaysStrict'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['esModuleInterop'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['forceConsistentCasingInFileNames'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['noImplicitAny'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['noImplicitReturns'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['noUnusedLocals'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['noUnusedParameters'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['resolveJsonModule'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['strict'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['strictNullChecks'] = true;"
yarn run json --in-place -f tsconfig.base.json -e "this.compilerOptions['stripInternal'] = true;"

# Adapt workspace/libs/json-schemas/ajv/src/lib to create an isValid map for Ajv validators.
  • Here is the content of the workspace/libs/json-schemas/ajv/src/lib/is-valid/json-schemas-ajv-is-valid.ts file:
// workspace/libs/json-schemas/ajv/src/lib/is-valid/json-schemas-ajv-is-valid.ts

import Ajv, { JSONSchemaType, ValidateFunction } from 'ajv';
import addFormats from 'ajv-formats';
import { jsonSchemasAjvKeywordsEven } from '../keywords/even/json-schemas-ajv-keywords-even';

export interface TsJsonSchemasError<T> {
	instancePath: string;
	keyword: string;
	message: string;
	params: { limit?: number } & T;
	schemaPath: string;
}

export let jsonSchemasAjvInstance = new Ajv({ allErrors: true });

jsonSchemasAjvInstance = addFormats(jsonSchemasAjvInstance);

jsonSchemasAjvInstance = jsonSchemasAjvKeywordsEven({ ajv: jsonSchemasAjvInstance });

export const tsJsonSchemasMap = new Map<unknown, unknown>();

export const jsonSchemasAjvIsValid = <S, D>({ schema, value }: { schema: JSONSchemaType<S>; value: D }) => {
	const isSchemaInSet = (<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).has(schema);
	if (!isSchemaInSet) {
		(<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).set(schema, jsonSchemasAjvInstance.compile(schema));
	}
	const validateFunction = <ValidateFunction<S>>(<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).get(schema);
	const isValid = (<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).has(schema) ? validateFunction(value) : false;
	const errorArray = validateFunction.errors?.map((error) => ({ ...error, message: error.message ?? '' })) ?? [];
	return { isValid, errorArray };
};

The command to build the library and its output:

cd workspace
yarn run nx run json-schemas-ajv:build

# Gives the following error:

# 21 export const jsonSchemasAjvIsValid = <S, D>({ schema, value }: { schema: JSONSchemaType<S>; value: D }) => {
#                ~~~~~~~~~~~~~~~~~~~~~
# libs/json-schemas/ajv/src/lib/is-valid/json-schemas-ajv-is-valid.ts:21:14 - error TS4023: Exported variable 'jsonSchemasAjvIsValid' has or is using name 'NumberKeywords' from external module "/home/hadrien_toma/gitlab.com/hadrien-toma/ajv-issue/workspace/node_modules/ajv/dist/types/json-schema" but cannot be named.

Notes:

Environment

> yarn run nx report
$ nx report

>  NX  Report complete - copy this into the issue template

  Node : 14.18.1
  OS   : linux x64
  yarn : 1.22.15
  
  nx : 13.1.4
  @nrwl/angular : Not Found
  @nrwl/cli : 13.1.4
  @nrwl/cypress : Not Found
  @nrwl/devkit : 13.1.4
  @nrwl/eslint-plugin-nx : 13.1.4
  @nrwl/express : Not Found
  @nrwl/jest : 13.1.4
  @nrwl/linter : 13.1.4
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : 13.1.4
  @nrwl/nx-cloud : Not Found
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 13.1.4
  @nrwl/web : Not Found
  @nrwl/workspace : 13.1.4
  @nrwl/storybook : Not Found
  @nrwl/gatsby : Not Found
  typescript : 4.3.5

Done in 1.89s.
@hadrien-toma
Copy link
Contributor Author

Updating jsonSchemasAjvIsValid to be a function solved the issue:

export function tsJsonSchemasValidate<S, D>({ schema, value }: { schema: JSONSchemaType<S>; value: D }) {
	const isSchemaInSet = (<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).has(schema);
	if (!isSchemaInSet) {
		(<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).set(schema, tsJsonSchemasAjv.compile(schema));
	}
	const validateFunction = <ValidateFunction<S>>(<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).get(schema);
	const isValid = (<Map<Record<string, unknown>, ValidateFunction<S>>>tsJsonSchemasMap).has(schema) ? validateFunction(value) : false;
	const errorArray: TsJsonSchemasError<unknown>[] = validateFunction.errors?.map((error) => ({ ...error, message: error.message ?? '' })) ?? [];
	return { isValid, errorArray };
}

@CMCDragonkai
Copy link

I'm getting this now with ajv 8.9.0. This only occurs in VScode though, not when using ts-node.

Then it says:

The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.ts(7056)

Is there some thing I need to do here?

@slifty
Copy link

slifty commented Sep 7, 2022

I am facing this as well and also fixed by using the function keyword instead of => but... I'm confused WHY that was a fix. Does anybody have insight?

An example of the message I'm getting in VSCode

Exported variable 'omitFromSchema' has or is using name 'NumberKeywords' from external module "/Users/slifty/Maestral/Code/open-tech-strategies/philanthropy-data-commons/service/node_modules/ajv/dist/types/json-schema" but cannot be named.

No error

export function omitFromSchema <T>(
  schema: JSONSchemaType<unknown>,
  fieldsToOmit: string[]
): JSONSchemaType<T> {
  if (schema.type === 'object') {
    return {
      type: schema.type,
      properties: removeAttributesFromObject(
        schema,
        fieldsToOmit,
      ),
      required: schema.required.filter(
        (item: string) => !fieldsToOmit.includes(item)
      ),
    }
  }
  return schema;
}

error

export const omitFromSchema = <T>(
  schema: JSONSchemaType<unknown>,
  fieldsToOmit: string[]
): JSONSchemaType<T> => {
  if (schema.type === 'object') {
    return {
      type: schema.type,
      properties: removeAttributesFromObject(
        schema,
        fieldsToOmit,
      ),
      required: schema.required.filter(
        (item: string) => !fieldsToOmit.includes(item)
      ),
    }
  }
  return schema;
}

@sziafs
Copy link

sziafs commented Oct 27, 2022

same problem with it, const results in error and as a function works fine.
but why? by the way is the team going to fix it?
appreciate it

@epoberezkin
Copy link
Member

Just to confirm, this is only VSCode problem and not reproducible with tsc compiler?

@cyrfer
Copy link

cyrfer commented Nov 21, 2022

I have not tried the function keyword workaround yet.

Should this issue be reopened?

@epoberezkin I'm seeing both VSCode and tsc complain after upgrade from Node 16 to Node 18

node --version
v18.12.1

I also tested node 18.11.0

npx tsc --version
Version 4.9.3

npm --version
8.19.2

package.json
"@types/node": "^18.11.9",


My workaround at build-time is to stop using structuredClone for assigning crafted JSON Schema to properties' subschema. I upgraded to Node 18 to be able to use structuredClone. AWS Lambda recently released Node 18 support allowing the use of structuredClone.

@Pagebakers
Copy link

Just to confirm, this is only VSCode problem and not reproducible with tsc compiler?

Having this issue when building with tsup.

@krinoid
Copy link

krinoid commented Jul 4, 2023

Just to confirm, this is only VSCode problem and not reproducible with tsc compiler?

Having this issue when building with tsup.

Same with tsc -b. Changing const fn = <T>() => {...} to function fn<T>() {...} works. The common part in the comments above seems to be that the functions are using generics. The last error for me is (after all Exported variable has or is using name 'NumberKeywords' ... errors):

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

8 participants