Comments (5)
I need to display the
string
corresponding to the status in the local<select valueEnum={status} />
and set status value.
That crosses over into state, which is separate from TypeScript types. Since you have to store the value in memory, I’d recommend something related to this SO answer, but using a discriminated union instead, e.g.:
import { components } from './schema.ts';
type MyEnumType = components["schema"]["MySchemaType"]["myEnumField"]; // 'one' | 'two' | 'three'
const selectOptions: Record<MyEnumType, string> = {
one: 'Option One',
two: 'Option Two',
three: 'Option Three',
four: 'Option Four' // ❌ invalid key; will throw TS error
}
// …
`<select valueEnum={selectOptions} />`
This is all pseudo-code; I don’t know what framework you’re using, but hopefully this makes sense. Main point is that your OpenAPI schema is meant to validate your code, not compose it. You still have to compose the actual code that executes, which often involves managing some values yourselves (this library only generates types, not actual code).
from openapi-typescript.
#266 merged! It will go out in the next release.
from openapi-typescript.
Can the enum
in Swagger be directly converted to the enum type
of TS?
// ts definition:
enum a {A, B, C}
// openapi definition:
interface b {
a: 'A' | 'B' | 'C'
}
a !== b.a
Because of the number
returned by the service, the string
I defined locally.
I need both. If you only use string
, the type will not match
Desired result
// openapi definition:
enum a {A, B, C}
interface b {
a: a
}
from openapi-typescript.
Can the enum in Swagger be directly converted to the enum type of TS?
Funny enough, the very first version did this! But after using it in a real application, I found it to be waay harder to use than a discriminated string union type. For example:
import { components } from './schema.ts';
const myObj: components["schema"]["MySchema"] = {
enum: 'A' // ✅
}
this just works. If we went with a true TS enum, we’d have to do something like:
import { components } from './schema.ts';
const myObj: components["schema"]["MySchema"] = {
enum: 'A' // ❌
enum: components["schema"]["MySchema"]["enum"].A // ✅
}
When you’re working a lot with schemas, this becomes pretty frustrating quickly. It’s just a problem with the way OpenAPI maps to TypeScript: enums are great going from TypeScript -> TypeScript. But quickly become annoying trying to generate them in OpenAPI.
Hopefully that explains some of the reasoning.
from openapi-typescript.
How to establish a relationship with the enum defined by the service?
The service api requires parameters whose status is number
I need to display the string
corresponding to the status in the local <select valueEnum={status} />
and set status value.
I must now set the enum field or root variable to as any
Is there any solution?
from openapi-typescript.
Related Issues (20)
- Headers passed in createClient not getting omitted from the headers in Client methods. HOT 1
- Transform only transforms referenced schemas HOT 1
- Hoping to wrap the client for single arg calls
- Metadata generation
- Allow client option for custom dispatcher into fetch requests (e.g. to disable certificate validation) HOT 6
- advice on how to connect a frontend (swagger?) while using this lib
- Set baseUrl per request
- 0.9.4 breaks some Typescript configurations HOT 3
- Response types from openapi-fetch ignore null values
- Multiple security schemes not supported
- With React Query: Invariant: headers() expects to have requestAsyncStorage, none available. HOT 3
- `--properties-required-by-default` and `--default-non-nullable` flags do not work HOT 1
- Nested object's properties are mapped to unknown, but in OpenAPI they are typed correctly HOT 3
- "AdditionalProperties" reflects onto existing properties and causes TypeScript compile errors HOT 1
- Issues with references (`$ref`) resolution HOT 2
- Error with negative numbers in enums HOT 2
- Allow for custom types in path params serialization (and maybe other places serialization too)
- Types derived for oneOf construct result in TS2502 error HOT 1
- Uncaught error when body is `undefined` in request init HOT 6
- 'path' does not exist in type '{ query?: Record<string, unknown> | undefined; }' HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi-typescript.