geut / openapi-box

Generate TypeBox schemas from your openapi.
MIT License
21 stars 4 forks source link

Consider better type for generated cache variable #7

Closed humphd closed 1 month ago

humphd commented 1 month ago

This may not be something you want to address, but I'm trying to use the generated file in TypeScript. That mostly works, but one thing that breaks is:

const cache = {};

TypeScript is unhappy with this due to the keys:

Element implicitly has an 'any' type because expression of type '"80a44415499f66d2e4772a7be3d417a1"' can't be used to index type '{}'. Property '80a44415499f66d2e4772a7be3d417a1' does not exist on type '{}'

In my code I'm switching it to this, which side-steps it:

const cache: { [key: string]: any } = {};
tinchoz49 commented 1 month ago

Hey @humphd can you give me an example of the code where you are getting this error?

The cache = {} cannot be type { [key: string]: any }, I define it as a dynamic object to allow typescript to translate into a namespace structure.

Example:

const cache = {}
cache.schema0 = Type.String()
cache.schema1 = Type.Number()

Typescript translate this into:

import * as _sinclair_typebox from '@sinclair/typebox';

type TSchema = _sinclair_typebox.TSchema;
type Static<T extends TSchema> = _sinclair_typebox.Static<T>;
declare namespace cache {
    let schema0: _sinclair_typebox.TString;
    let schema1: _sinclair_typebox.TNumber;
}
tinchoz49 commented 1 month ago

latest version remove the cache object