From 4ba9dc2dc3b62b27e684e3ea9ec26769ca730d76 Mon Sep 17 00:00:00 2001 From: kahirokunn Date: Wed, 1 May 2024 18:11:31 +0900 Subject: [PATCH] fix strict type error --- packages/kubekit-codegen/package.json | 2 +- packages/kubekit-codegen/src/generate.ts | 45 +++++++++++++-- .../test/__snapshots__/cli.test.ts.snap | 14 +++-- .../generateEndpoints.test.ts.snap | 56 +++++++++++++------ 4 files changed, 90 insertions(+), 27 deletions(-) diff --git a/packages/kubekit-codegen/package.json b/packages/kubekit-codegen/package.json index 013f2621..8c750a19 100644 --- a/packages/kubekit-codegen/package.json +++ b/packages/kubekit-codegen/package.json @@ -1,6 +1,6 @@ { "name": "@kubekit/codegen", - "version": "0.0.18", + "version": "0.0.19", "main": "lib/index.js", "types": "lib/index.d.ts", "author": "kahirokunn", diff --git a/packages/kubekit-codegen/src/generate.ts b/packages/kubekit-codegen/src/generate.ts index 4bbb8468..a6e9a064 100644 --- a/packages/kubekit-codegen/src/generate.ts +++ b/packages/kubekit-codegen/src/generate.ts @@ -342,7 +342,7 @@ export async function generateApi( const bodyParameter = Object.values(queryArg).find((def) => def.origin === 'body'); if (bodyParameter) { - isUnusedArgs = false + isUnusedArgs = false; } const argsObject = factory.createIdentifier('args'); @@ -450,7 +450,7 @@ export async function generateApi( createObjectLiteralProperty(pickParams('query'), 'params'), ].filter(removeUndefined), false - ) + ); return { isUnusedArgs, queryFn, @@ -506,7 +506,10 @@ function getBodyNode(bodies: { [contentType: string]: ts.TypeNode }): ts.TypeNod // type NoWatch = Omit & { // watch?: false; // }; -// type PartialRequired = Id> & Omit>; +// type RequiredAndDefined = { +// [P in keyof T]-?: Exclude +// } +// type PartialRequired = Id> & Omit>; // type MinimumRequiredGet = Id, "metadata"> & { // metadata: PartialRequired< -// Required["metadata"], +// RequiredAndDefined["metadata"], // "name" | "namespace" | "creationTimestamp" | "resourceVersion" // >; // } @@ -577,6 +580,36 @@ const buildInTypes = [ ]), ]) ), + factory.createTypeAliasDeclaration( + undefined, + factory.createIdentifier('RequiredAndDefined'), + [factory.createTypeParameterDeclaration(undefined, factory.createIdentifier('T'), undefined, undefined)], + factory.createMappedTypeNode( + undefined, + factory.createTypeParameterDeclaration( + undefined, + factory.createIdentifier('P'), + factory.createTypeOperatorNode( + ts.SyntaxKind.KeyOfKeyword, + factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined) + ), + undefined + ), + undefined, + factory.createToken(ts.SyntaxKind.MinusToken), + factory.createTypeReferenceNode(factory.createIdentifier('Exclude'), [ + factory.createIndexedAccessTypeNode( + factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined), + factory.createTypeReferenceNode(factory.createIdentifier('P'), undefined) + ), + factory.createUnionTypeNode([ + factory.createLiteralTypeNode(factory.createNull()), + factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword), + ]), + ]), + undefined + ) + ), factory.createTypeAliasDeclaration( undefined, factory.createIdentifier('PartialRequired'), @@ -594,7 +627,7 @@ const buildInTypes = [ ], factory.createTypeReferenceNode(factory.createIdentifier('Id'), [ factory.createIntersectionTypeNode([ - factory.createTypeReferenceNode(factory.createIdentifier('Required'), [ + factory.createTypeReferenceNode(factory.createIdentifier('RequiredAndDefined'), [ factory.createTypeReferenceNode(factory.createIdentifier('Pick'), [ factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined), factory.createTypeReferenceNode(factory.createIdentifier('K'), undefined), @@ -653,7 +686,7 @@ const buildInTypes = [ undefined, factory.createTypeReferenceNode(factory.createIdentifier('PartialRequired'), [ factory.createIndexedAccessTypeNode( - factory.createTypeReferenceNode(factory.createIdentifier('Required'), [ + factory.createTypeReferenceNode(factory.createIdentifier('RequiredAndDefined'), [ factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined), ]), factory.createLiteralTypeNode(factory.createStringLiteral('metadata')) diff --git a/packages/kubekit-codegen/test/__snapshots__/cli.test.ts.snap b/packages/kubekit-codegen/test/__snapshots__/cli.test.ts.snap index f784b60b..9f7c3c4b 100644 --- a/packages/kubekit-codegen/test/__snapshots__/cli.test.ts.snap +++ b/packages/kubekit-codegen/test/__snapshots__/cli.test.ts.snap @@ -8,7 +8,10 @@ type Id = { type NoWatch = Omit & { watch?: false; }; -type PartialRequired = Id> & Omit>; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; +type PartialRequired = Id> & Omit>; type MinimumRequiredGet = Id< T extends { metadata?: any; @@ -17,7 +20,7 @@ type MinimumRequiredGet = Id< } ? Omit, 'metadata'> & { metadata: PartialRequired< - Required['metadata'], + RequiredAndDefined['metadata'], 'name' | 'namespace' | 'creationTimestamp' | 'resourceVersion' >; } @@ -13071,7 +13074,10 @@ type Id = { type NoWatch = Omit & { watch?: false; }; -type PartialRequired = Id> & Omit>; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; +type PartialRequired = Id> & Omit>; type MinimumRequiredGet = Id< T extends { metadata?: any; @@ -13080,7 +13086,7 @@ type MinimumRequiredGet = Id< } ? Omit, 'metadata'> & { metadata: PartialRequired< - Required['metadata'], + RequiredAndDefined['metadata'], 'name' | 'namespace' | 'creationTimestamp' | 'resourceVersion' >; } diff --git a/packages/kubekit-codegen/test/__snapshots__/generateEndpoints.test.ts.snap b/packages/kubekit-codegen/test/__snapshots__/generateEndpoints.test.ts.snap index 8ddb87ed..fcdba0f9 100644 --- a/packages/kubekit-codegen/test/__snapshots__/generateEndpoints.test.ts.snap +++ b/packages/kubekit-codegen/test/__snapshots__/generateEndpoints.test.ts.snap @@ -12,8 +12,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -26,7 +29,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -446,8 +449,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -460,7 +466,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -527,8 +533,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -541,7 +550,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -650,8 +659,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -664,7 +676,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -731,8 +743,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -745,7 +760,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -803,8 +818,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -817,7 +835,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -1227,8 +1245,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -1241,7 +1262,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; } @@ -1317,8 +1338,11 @@ type Id = { type NoWatch = Omit & { watch?: false; }; +type RequiredAndDefined = { + [P in keyof T]-?: Exclude; +}; type PartialRequired = Id< - Required> & Omit + RequiredAndDefined> & Omit >; type MinimumRequiredGet = Id< T extends { @@ -1331,7 +1355,7 @@ type MinimumRequiredGet = Id< "metadata" > & { metadata: PartialRequired< - Required["metadata"], + RequiredAndDefined["metadata"], "name" | "namespace" | "creationTimestamp" | "resourceVersion" >; }