这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-kangaroos-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

feat(typescript): add `topType` option allowing to choose `any` over `unknown`
15 changes: 14 additions & 1 deletion packages/openapi-ts-tests/main/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,20 @@ describe(`OpenAPI ${version}`, () => {
input: 'additional-properties-true.json',
output: 'additional-properties-true',
}),
description: 'allows arbitrary properties on objects',
description: 'allows arbitrary properties on objects (unknown top type)',
},
{
config: createConfig({
input: 'additional-properties-true.json',
output: 'additional-properties-true-any',
plugins: [
{
name: '@hey-api/typescript',
topType: 'any',
},
],
}),
description: 'allows arbitrary properties on objects (any top type)',
},
{
config: createConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is auto-generated by @hey-api/openapi-ts

export * from './types.gen';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This file is auto-generated by @hey-api/openapi-ts

export type Foo = {
foo: string;
[key: string]: any | string;
};

export type Bar = Foo & {
[key: string]: any;
};

export type Baz = Foo & {
bar: string;
[key: string]: any | string;
};

export type Qux = {
[key: string]: any;
};

export type ClientOptions = {
baseUrl: `${string}://${string}` | (string & {});
};
1 change: 1 addition & 0 deletions packages/openapi-ts-tests/main/test/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export default defineConfig(() => {
// name: '我_responses_{{name}}',
// response: '他_response_{{name}}',
// },
topType: 'any',
// tree: true,
// webhooks: {
// name: 'Webby{{name}}Hook',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const defaultConfig: HeyApiTypeScriptPlugin['Config'] = {
case: 'PascalCase',
exportFromIndex: true,
style: 'preserve',
topType: 'unknown',
tree: false,
},
handler,
Expand Down
8 changes: 3 additions & 5 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ const arrayTypeToIdentifier = ({
}): ts.TypeNode => {
if (!schema.items) {
return tsc.typeArrayNode(
tsc.keywordTypeNode({
keyword: 'unknown',
}),
tsc.keywordTypeNode({ keyword: plugin.config.topType }),
);
}

Expand Down Expand Up @@ -332,7 +330,7 @@ const tupleTypeToIdentifier = ({
if (schema.const && Array.isArray(schema.const)) {
itemTypes = schema.const.map((value) => {
const expression = tsc.valueToExpression({ value });
return expression ?? tsc.identifier({ text: 'unknown' });
return expression ?? tsc.identifier({ text: plugin.config.topType });
});
} else if (schema.items) {
for (const item of schema.items) {
Expand Down Expand Up @@ -432,7 +430,7 @@ const schemaTypeToIdentifier = ({
});
case 'unknown':
return tsc.keywordTypeNode({
keyword: 'unknown',
keyword: plugin.config.topType,
});
case 'void':
return tsc.keywordTypeNode({
Expand Down
16 changes: 16 additions & 0 deletions packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ export type UserConfig = Plugin.Name<'@hey-api/typescript'> & {
*/
response?: StringName;
};
/**
* The top type to use for untyped or unspecified schema values.
*
* Can be:
* - `unknown` (default): safe top type, you must narrow before use
* - `any`: disables type checking, can be used anywhere
*
* @default 'unknown'
*/
topType?: 'any' | 'unknown';
/**
* Configuration for webhook-specific types.
*
Expand Down Expand Up @@ -429,6 +439,12 @@ export type Config = Plugin.Name<'@hey-api/typescript'> & {
*/
response: StringName;
};
/**
* The top type to use for untyped or unspecified schema values.
*
* @default 'unknown'
*/
topType: 'any' | 'unknown';
/**
* Configuration for webhook-specific types.
*
Expand Down
Loading