代码之家  ›  专栏  ›  技术社区  ›  user1283776

为什么这个JSON模式不能分配给这个TypeScript接口?

  •  0
  • user1283776  · 技术社区  · 4 年前

    具有包含字符串的数组的以下架构没有错误:

      interface ExpectedBody2 {
        push: {
          changes: string[]
        }
      }
      
      const expectedBodySchema2: JSONSchemaType<ExpectedBody2> = {
        type: 'object',
        properties: {
          push: {
            type: 'object',
            properties: {
              changes: {
                type: 'array',
                items: {
                  type: 'string',
                },
              },
            },
            required: ['changes']
          }
        },
        required: ['push'],
      }
    

    这个稍微修改过的带有包含对象的数组的架构有错误:

      interface ExpectedBody3 {
        push: {
          changes: [{
            old: string,
          }]
        }
      }
      
      const expectedBodySchema3: JSONSchemaType<ExpectedBody3> = {
        type: 'object',
        properties: {
          push: {
            type: 'object',
            properties: {
              changes: {
                type: 'array',
                items: {
                  type: 'object',
                  properties: {
                    old: {
                      type: 'string'
                    },
                  },
                  required: ['old']
                },
              },
            },
            required: ['changes']
          }
        },
        required: ['push'],
      }
    
    Type '{ type: "object"; properties: { push: { type: "object"; properties: { changes: { type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }; }; required: "changes"[]; }; }; required: "push"[]; }' is not assignable to type 'UncheckedJSONSchemaType<ExpectedBody3, false>'.
      The types of 'properties.push' are incompatible between these types.
        Type '{ type: "object"; properties: { changes: { type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }; }; required: "changes"[]; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<{ changes: [{ old: string; }]; }, false> & { const?: { changes: [{ old: string; }]; } | undefined; enum?: readonly { changes: [{ ...; }]; }[] | undefined; default?: { ...; } | undefined; })'.
          The types of 'properties.changes' are incompatible between these types.
            Type '{ type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<[{ old: string; }], false> & { const?: [{ old: string; }] | undefined; enum?: readonly [{ old: string; }][] | undefined; default?: [...] | undefined; })'.
              Type '{ type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }' is not assignable to type '{ type: "array"; items: readonly [UncheckedJSONSchemaType<{ old: string; }, false> & { const?: { old: string; } | undefined; enum?: readonly { old: string; }[] | undefined; default?: { ...; } | undefined; }] & { ...; }; minItems: 1; } & { ...; } & { ...; } & { ...; } & { ...; }'.
                Property 'minItems' is missing in type '{ type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }' but required in type '{ type: "array"; items: readonly [UncheckedJSONSchemaType<{ old: string; }, false> & { const?: { old: string; } | undefined; enum?: readonly { old: string; }[] | undefined; default?: { ...; } | undefined; }] & { ...; }; minItems: 1; }'.ts(2322)
    json-schema.d.ts(41, 5): 'minItems' is declared here.
    

    我做错了什么?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Nalin Ranjan    4 年前

    我试过这个。。你能检查一下它是否适合你吗。。。

    interface ExpectedBody3 {
        push: IPush
    }
    
    interface IPush {
        changes: IOld[]
    }
    
    interface IOld {
        old: string
    }
    
    推荐文章