ajv-validator / ajv-validator/ajv

type `JSONSchemaType` cannot be resolved in generic/nonresolved context

未关闭
#2,495 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
typescript
主要语言
TypeScript
星标
14.8k
派生
1k
PR 合并指标
30 天内没有已合并 PR

描述

**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**

```typescript
interface A {
someFunction() : JSONSchemaType
}

class B implements A {

private _schema: JSONSchemaType;

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

function test>(a: TA): TA
{
return a;
}

function testcase2>(a: AT)
{
// gives error (exactly the same)
test(a);
}

function testcase3>(a: AT)
{
// gives error (exactly the same)
test(a);
}

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

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

**Typescript compiler error messages**

```
Argument of type 'this' is not assignable to parameter of type 'A'.
Type 'B' is not assignable to type 'A'.
The types returned by 'someFunction()' are incompatible between these types.
Type 'JSONSchemaType' is not assignable to type 'JSONSchemaType'.
Type '{ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | undefined; definitions?: Record<...> | undefined; }' is not assignable to type 'JSONSchemaType'.
Type '{ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | undefined; definitions?: Record<...> | undefined; }' is not assignable to type '{ type: readonly never[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | undefined; definitions?: Record<...> | undefined; }'.
Property 'type' is missing in type '{ anyOf: readonly UncheckedJSONSchemaType[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record> | 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!

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。