0

Why is discriminator any here? It's supposed to be, and defined as, DiscriminatorObject.

import type { OpenAPIV3_1 } from '@scalar/openapi-types'

const schemas: Record<string, OpenAPIV3_1.SchemaObject> = {}
schemas.foo?.discriminator // <-- discriminator is any, should be DiscriminatorObject

example

What's going on? Is there a way to make the type work?

4
  • 1
    mentioning the ide probably helpful Commented yesterday
  • I'm was thinking the IDE wasn't important as it's the TypeScript Language Server that's supplying the type to the IDE Commented yesterday
  • Please don't post textual information in the form of pictures. Please see: Why should I not upload images of code/data/errors? If you still need an image, it can complement the textual information. Commented yesterday
  • Yes, showing any is a known problem. It is not related to a known or unknown type, it is related to the object itself. To work around 1) Add a variable/property to the WATCH list under the Run and Debug container element, 2) Enter it in the Debug Console input line. Commented yesterday

1 Answer 1

2

The type definition for OpenAPIV3_1.SchemaObject is:

export type SchemaObject = (ArraySchemaObject | NonArraySchemaObject | MixedSchemaObject | boolean) & AnyOtherAttribute;

The problem is that the schema might be a boolean according to this type, and the intersection with AnyOtherAttribute is distributed across the union. The consequence of this is that TypeScript treats the boolean possibility as potentially having unknown attributes of any type. In my opinion, boolean & AnyOtherAttribute doesn't make sense as a type, and should perhaps be considered a bug in the library.

If you narrow the type of schemas.foo to eliminate the boolean possibility, the type of discriminator is as expected:

import type { OpenAPIV3_1 } from "@scalar/openapi-types";

const schemas: Record<string, OpenAPIV3_1.SchemaObject> = {};
if (typeof schemas.foo === "object") {
  schemas.foo.discriminator;
}
(property) discriminator?: OpenAPIV3.DiscriminatorObject | undefined

screenshot of the type hover for the discriminator property

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent answer! I feel like I learned something, thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.