3 ms·
Off the top of my head, I don't think you can perfectly describe the type (string format) of an UUID in typescript types, which you can do with OpenAPI. That ma
by thrance 2y ago
Off the top of my head, I don't think you can perfectly describe the type (string format) of an UUID in typescript types, which you can do with OpenAPI. That may be a reason.
- bterlson 2y agoYou can get by, e.g. you can use JSDoc comments describing format or pattern, generic types taking a parameter of either format or pattern along with a base type, or create a custom well-known type you define as having the properties of a UUID. But aside from comments, this is not reflected well in the tooling, and none of these options compose particularly well.
- n2d4 2y agoYou can do it with template string types: https://ybogomolov.me/type-level-uuid https://ybogomolov.me/type-level-uuid
- throwitaway1123 2y agoThe built in DOM types for the browser's `crypto.randomUUID` function actually already use this technique to a lesser extent: https://github.com/microsoft/TypeScript/blob/121c5dd36bd87e47a065ff7d6b220ce6d079075c/src/lib/dom.generated.d.ts#L5971 https://github.com/microsoft/TypeScript/blob/121c5dd36bd87e4...
- erikerikson 2y agoSee n2d4's comment but also AJV's JSONSchemaType import { JSONSchemaType } from 'ajv' It will bridge the JSON Schema and TypeScript types We've found this useful too: import { OpenAPIV3_1 as oa } from 'openapi-types' export type Schema<T> = JSONSchemaType<T> & oa.SchemaObject We validate that the type aligns with the schema and load the schema to validate all incoming requests