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

type JSONSchemaType cannot be resolved in generic/nonresolved context #2495

Open
N247S opened this issue Sep 12, 2024 · 0 comments
Open

type JSONSchemaType cannot be resolved in generic/nonresolved context #2495

N247S opened this issue Sep 12, 2024 · 0 comments

Comments

@N247S
Copy link

N247S commented Sep 12, 2024

What version of Ajv are you using? Does the issue happen if you use the latest version?
Version: "8.17.1" (Currently latest)

Your typescript code

interface A<T> {
	someFunction() : JSONSchemaType<T>
}

class B<T> implements A<T> {
	
	private _schema: JSONSchemaType<T>;

	someFunction(): JSONSchemaType<T>
	{
		// gives error
		test(this);
		return this._schema;
	}
}

function test<T, TA extends A<T>>(a: TA): TA
{
	return a;
}

function testcase2<T, AT extends A<T>>(a: AT)
{
	// gives error (exactly the same)
	test(a);
}

function testcase3<T, AT extends B<T>>(a: AT)
{
	// gives error (exactly the same)
	test(a);
}

function testcase4<T>(a: A<T>)
{
	// gives error (exactly the same)
	test(a);
}

function testcase5<T>(a: B<T>)
{
	// gives error (exactly the same)
	test(a);
}

Typescript compiler error messages

Argument of type 'this' is not assignable to parameter of type 'A<unknown>'.
  Type 'B<T>' is not assignable to type 'A<unknown>'.
    The types returned by 'someFunction()' are incompatible between these types.
      Type 'JSONSchemaType<T>' is not assignable to type 'JSONSchemaType<unknown>'.
        Type '{ anyOf: readonly UncheckedJSONSchemaType<T, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<Known, true>> | undefined; definitions?: Record<...> | undefined; }' is not assignable to type 'JSONSchemaType<unknown>'.
          Type '{ anyOf: readonly UncheckedJSONSchemaType<T, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<Known, true>> | undefined; definitions?: Record<...> | undefined; }' is not assignable to type '{ type: readonly never[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<Known, true>> | undefined; definitions?: Record<...> | undefined; }'.
            Property 'type' is missing in type '{ anyOf: readonly UncheckedJSONSchemaType<T, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<Known, true>> | undefined; definitions?: Record<...> | undefined; }' but required in type '{ type: readonly never[]; }'.ts(2345)

Explanation of code
What I found out is that when the T parameter is not known/ is still a generic-type and it is passed to a generic function (like test). Somehow typescript cannot resolve this, eventhough in my mind everything should check out. I swapped the JSONSchemaType for other objects which includes generic-types. Couldn't find something simular (and less complex) which results in the same issue.

Question and/or bugreport
I am not sure if this is usererror (user = me), or this is because some complicated type defenition typescript cannot handle. I would love to get some feedback or hints about how to solve this.

Thanks in advance!

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

No branches or pull requests

1 participant