首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript Ajv圆结构

TypeScript Ajv圆结构
EN

Stack Overflow用户
提问于 2022-08-30 20:49:23
回答 1查看 32关注 0票数 -1

在NodeJS上使用Ajv,我似乎无法获得要在Ajv中定义的循环TypeScript结构。

一个简单的循环结构可以工作(参见“父结构”),但我无法获得一个数组来工作(参见“子结构”)。

取消注释“子”行以查看错误。

代码语言:javascript
复制
'use strict'
import Ajv, {JSONSchemaType} from "ajv"

type MyType = {
  name : string,
  parent? : MyType,
  //children? : Array<MyType>
};

const MySchema : JSONSchemaType<MyType> = {
  type:"object",
  properties:{
    name:{type:"string"},
    parent:{$ref:"#"},
    //children:{type:"array",items:{$ref:"#"}}
  },
  required:["name"]
}

let pnt:MyType = { name : "Parent" }
let c1:MyType = { name : "Child1" }
let c2:MyType = { name : "Child2" }
let c3:MyType = { name : "Child3" }

let node:MyType = { name : "Node1", parent : pnt }

//node.children = [c1,c2,c3];

// Validate
const ajv = new Ajv({discriminator:true,allErrors:true,allowUnionTypes:true})
const validate = ajv.compile(MySchema)

console.log(validate(pnt));
console.log(validate(c1));
console.log(validate(c2));
console.log(validate(c3));
console.log(validate(node));

错误,当“子”行未加注释时

类型'{ type:"object";属性:{ name:{ type:"string";};父类型:{ $ref: string;};子类型:{ type:"array";项:{ $ref: string;};};必需:"name"[];}‘UncheckedJSONSchemaType’是不可分配的.“properties.children”的类型在这些类型之间是不兼容的。类型'{ type:“数组”;项:{ $ref: string;};}‘不能分配到'{ $ref: string;}}’(未定义的UncheckedJSONSchemaType< MyType[]、false> &{ nullable: true;const?:空未定义的;枚举?):readonly (MyType[] \ null \未定义的)[]未定义的MyType[]?:MyType[].1.未定义的;}‘’。属性“项”的类型不兼容。键入'{ $ref: string;}‘不能分配到键入'UncheckedJSONSchemaType’。类型'{ $ref: string;}‘不能分配到以下类型:{ type:"object";additionalProperties?:boolean _ UncheckedJSONSchemaType未定义的;unevaluatedProperties?:boolean UncheckedJSONSchemaType未定义;.7;maxProperties?:未定义的数字;}&{. }’。类型“{ $ref: string;}”中缺少属性“type”,但类型“{ type:”“object”需要;additionalProperties?:boolean UncheckedJSONSchemaType颇具未定义的;unevaluatedProperties?:boolean UncheckedJSONSchemaType false>未定义;.多.;maxProperties?:未定义的数字;}‘。

EN

回答 1

Stack Overflow用户

发布于 2022-08-31 12:57:41

作为一项工作,将Ajv模式的类型声明为"any“允许TypeScript不出错。

TypeScript“类型”仍然是完全定义的,因此类型记录的执行前检查仍在进行。只有Ajv模式声明的“类型”不是100%,因为使用"as any“。Ajv用于数据的执行时间验证,而这个函数并没有通过添加"as any“来改变,所以这个工作看起来是合理的。

代码语言:javascript
复制
const MySchema : JSONSchemaType<MyType> = {
  .......
} as any; // <----- added type declaration

完整代码

代码语言:javascript
复制
'use strict'
import Ajv, {JSONSchemaType} from "ajv"

type MyType = {
  name : string,
  parent? : MyType,
  children? : Array<MyType>
};

const MySchema : JSONSchemaType<MyType> = {
  type:"object",
  properties:{
    name:{type:"string"},
    parent:{$ref:"#"},
    children:{type:"array",items:{$ref:"#"}}
  },
  required:["name"]
} as any; // <----- added type declaration

let pnt:MyType = { name : "Parent" }
let c1:MyType = { name : "Child1" }
let c2:MyType = { name : "Child2" }
let c3:MyType = { name : "Child3" }

let node:MyType = { name : "Node1", parent : pnt }

node.children = [c1,c2,c3];

// Validate
const ajv = new Ajv({discriminator:true,allErrors:true,allowUnionTypes:true})
const validate = ajv.compile(MySchema)

console.log(validate(pnt));
console.log(validate(c1));
console.log(validate(c2));
console.log(validate(c3));
console.log(validate(node));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73548316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档