Open mat-certn opened 1 month ago
Here's one scenario I can think of:
type BasePath = string & { __id: 'BasePath' };
type SchemaOne = {
case_name: string;
};
type SchemaTwo = {
case_summary: string;
};
type Paths = {
[path: `/api/internal/cases/${BasePath}`]: SchemaOne;
[path: `/api/internal/cases/${BasePath}/summary`]: SchemaTwo;
};
const caseDetail: Paths[`/api/internal/cases/${BasePath}`] = {
case_name: 'example-case',
};
const caseSummary: Paths[`/api/internal/cases/${BasePath}/summary`] = {
case_summary: 'example-summary',
};
@drwpow @mat-certn I want to know what you think?
i think for my use-case I want to find the type definition from the URL, e.g. I want to be able to do this:
const url = `/api/internal/cases/12345/summary`
const caseSummary: Paths[url] = {
case_summary: 'example-summary',
};
which doesn't work with the above: /api/internal/cases/12345/summary does not exist on type Paths
i think for my use-case I want to find the type definition from the URL, e.g. I want to be able to do this:
const url = `/api/internal/cases/12345/summary` const caseSummary: Paths[url] = { case_summary: 'example-summary', };
which doesn't work with the above:
/api/internal/cases/12345/summary does not exist on type Paths
Are there any good solutions for this in ts?
Yeah this is a tricky one! I will admit this flag has always had some rough edges for reasons like this. Loose pattern-matching in TS isn’t as robust as runtime equivalents. I’m sure there are ways to solve it, but they may not be straightforward. Either way, would welcome PRs on improving this.
Description
We have multiple paths in our schema that are "nested" below each other, like so:
openapi-typescript using the argument --path-params-as-types generates the following:
which conflicts with the error
this is because technically
/some/path/${string}/summary
is generated as incorrectly assignable to/some/path/${string}
- instead of ${string} it should be some sort of type that cannot include "/"openapi-typescript
7.0.2
5.3.3
or5.5.3
(have tried a few versions)18.18.2
macOS 14.5
Reproduction
Generate types using the following schema:
npx openapi-typescript example.yml --output example.ts --path-params-as-types
Expected result
A more complex type than ${string} that prevents both endpoints from matching each other.
Checklist
npx @redocly/cli@latest lint
)validating example.yml... example.yml: validated in 12ms
Woohoo! Your API description is valid. 🎉