Skip to content

Differing types in main schema and create cause TypeScript problems #3623

@AshotN

Description

@AshotN
// Main data model schema
export const companySchema = Type.Object(
  {
    id: Type.Number(),
    count: Type.String() //Stored as string in DB for example
  },
  { $id: 'Company', additionalProperties: false }
)


// Schema for creating new entries
export const companyDataSchema = Type.Object(
  {
    text: Type.String(),
    count: Type.Number() // Taken as number by API
  },
  {
    $id: 'CompanyData',
    additionalProperties: false
  }
)
export type CompanyData = Static<typeof companyDataSchema>

//Type error - Type string is not assignable to type PromiseOrLiteral<number | undefined>
export const companyDataResolver = resolve<CompanyData, HookContext<CompanyService>>({
  count: (value, obj, context, status) => {
    return value + ' People'
  }
})

Similarly, if you have differing types between the main and create schema. context.result in hooks get's typed as any

create: [
        async (context, next) => {
          const result = context.result
          // result is type any
          await next()
        }
      ]

Types should never fallback into any if there is a unexpected type, the output type should be never so you don't accidentally break bypass TypeScript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions