From b853a4d5bf987fb523855348eaece400a86db667 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 2 Jan 2025 09:11:01 +0100 Subject: [PATCH 1/4] chore: move expand to @backstage/types Signed-off-by: blam --- packages/types/src/expand.test.ts | 38 +++++++++++++++++++++++++++++++ packages/types/src/expand.ts | 31 +++++++++++++++++++++++++ packages/types/src/index.ts | 1 + 3 files changed, 70 insertions(+) create mode 100644 packages/types/src/expand.test.ts create mode 100644 packages/types/src/expand.ts diff --git a/packages/types/src/expand.test.ts b/packages/types/src/expand.test.ts new file mode 100644 index 0000000000..007cef27ef --- /dev/null +++ b/packages/types/src/expand.test.ts @@ -0,0 +1,38 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Expand, ExpandRecursive } from './expand'; + +describe('expand', () => { + it('should expand type aliases into their equivalent type', () => { + type Test = { a: string } & { b: number }; + // expanded type + type Result = Expand; + + const result: Result = { a: 'string', b: 1 }; + expect(result).toEqual({ a: 'string', b: 1 }); + }); + + it('should expand types recursively', () => { + type Test = { a: string } & { b: { c: { e: string } } } & { + b: { c: { d: boolean } }; + }; + // expanded type + type Result = ExpandRecursive; + + const result: Result = { a: 'string', b: { c: { e: 'string', d: true } } }; + expect(result).toEqual({ a: 'string', b: { c: { e: 'string', d: true } } }); + }); +}); diff --git a/packages/types/src/expand.ts b/packages/types/src/expand.ts new file mode 100644 index 0000000000..6c417642e4 --- /dev/null +++ b/packages/types/src/expand.ts @@ -0,0 +1,31 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Utility type to expand type aliases into their equivalent type. + * @public + */ + +export type Expand = T extends infer O ? { [K in keyof O]: O[K] } : never; + +/** + * Helper type that expands type hints recursively + * + * @public + */ +export type ExpandRecursive = T extends infer O + ? { [K in keyof O]: ExpandRecursive } + : never; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 480c85818a..707c158b03 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -24,3 +24,4 @@ export { createDeferred, type DeferredPromise } from './deferred'; export type { JsonArray, JsonObject, JsonPrimitive, JsonValue } from './json'; export type { Observable, Observer, Subscription } from './observable'; export { type HumanDuration, durationToMilliseconds } from './time'; +export { type Expand, type ExpandRecursive } from './expand'; From f167aa83b194151d61c8423df565e2db0ca82984 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 2 Jan 2025 09:16:03 +0100 Subject: [PATCH 2/4] chore: move usage of expand type to @backstage/types Signed-off-by: blam --- .../src/apis/definitions/TranslationApi.ts | 18 +----------------- packages/frontend-plugin-api/src/types.ts | 7 ------- .../src/wiring/createExtension.ts | 2 +- .../src/wiring/createExtensionBlueprint.ts | 2 +- .../src/wiring/resolveInputOverrides.ts | 2 +- 5 files changed, 4 insertions(+), 27 deletions(-) diff --git a/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts b/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts index 202b2093a0..dba8753828 100644 --- a/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts +++ b/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts @@ -15,7 +15,7 @@ */ import { ApiRef, createApiRef } from '@backstage/core-plugin-api'; -import { Observable } from '@backstage/types'; +import { Expand, ExpandRecursive, Observable } from '@backstage/types'; import { TranslationRef } from '../../translation'; /** @@ -98,22 +98,6 @@ type CollapsedMessages = { : key]: TMessages[key]; }; -/** - * Helper type that expands type hints - * - * @ignore - */ -type Expand = T extends infer O ? { [K in keyof O]: O[K] } : never; - -/** - * Helper type that expands type hints recursively - * - * @ignore - */ -type ExpandRecursive = T extends infer O - ? { [K in keyof O]: ExpandRecursive } - : never; - /** * Trim away whitespace * diff --git a/packages/frontend-plugin-api/src/types.ts b/packages/frontend-plugin-api/src/types.ts index a658760758..9088a9d772 100644 --- a/packages/frontend-plugin-api/src/types.ts +++ b/packages/frontend-plugin-api/src/types.ts @@ -17,13 +17,6 @@ import { ReactNode } from 'react'; import { FrontendPlugin } from './wiring'; -// TODO(Rugvip): This might be a quite useful utility type, maybe add to @backstage/types? -/** - * Utility type to expand type aliases into their equivalent type. - * @ignore - */ -export type Expand = T extends infer O ? { [K in keyof O]: O[K] } : never; - /** @public */ export type CoreProgressProps = {}; diff --git a/packages/frontend-plugin-api/src/wiring/createExtension.ts b/packages/frontend-plugin-api/src/wiring/createExtension.ts index 16f9f341a7..baf0b31215 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtension.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtension.ts @@ -15,7 +15,7 @@ */ import { ApiHolder, AppNode } from '../apis'; -import { Expand } from '../types'; +import { Expand } from '@backstage/types'; import { ResolveInputValueOverrides, resolveInputOverrides, diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts index e22179127a..3d55b0bbe0 100644 --- a/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtensionBlueprint.ts @@ -15,7 +15,7 @@ */ import { ApiHolder, AppNode } from '../apis'; -import { Expand } from '../types'; +import { Expand } from '@backstage/types'; import { ExtensionDefinition, ResolvedExtensionInputs, diff --git a/packages/frontend-plugin-api/src/wiring/resolveInputOverrides.ts b/packages/frontend-plugin-api/src/wiring/resolveInputOverrides.ts index cf201779e0..0541ca3620 100644 --- a/packages/frontend-plugin-api/src/wiring/resolveInputOverrides.ts +++ b/packages/frontend-plugin-api/src/wiring/resolveInputOverrides.ts @@ -15,7 +15,7 @@ */ import { AppNode } from '../apis'; -import { Expand } from '../types'; +import { Expand } from '@backstage/types'; import { ResolvedExtensionInput } from './createExtension'; import { ExtensionDataContainer, From b40eb41fe25ede99ce5fa5b489acc49386131d5d Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 2 Jan 2025 09:17:40 +0100 Subject: [PATCH 3/4] chore: changeset Signed-off-by: blam --- .changeset/long-parrots-hide.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/long-parrots-hide.md diff --git a/.changeset/long-parrots-hide.md b/.changeset/long-parrots-hide.md new file mode 100644 index 0000000000..ac6c08b809 --- /dev/null +++ b/.changeset/long-parrots-hide.md @@ -0,0 +1,7 @@ +--- +'@backstage/frontend-plugin-api': patch +'@backstage/core-plugin-api': patch +'@backstage/types': patch +--- + +Move `Expand` and `ExpandRecursive` to `@backstage/types` From f9a6bd18050e5874036fba122e554672d39899e8 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 2 Jan 2025 09:29:53 +0100 Subject: [PATCH 4/4] chore: updating api-reports Signed-off-by: blam --- packages/core-plugin-api/report-alpha.api.md | 2 ++ packages/frontend-plugin-api/report.api.md | 1 + packages/types/report.api.md | 14 ++++++++++++++ 3 files changed, 17 insertions(+) diff --git a/packages/core-plugin-api/report-alpha.api.md b/packages/core-plugin-api/report-alpha.api.md index c0f0d02535..281d943e50 100644 --- a/packages/core-plugin-api/report-alpha.api.md +++ b/packages/core-plugin-api/report-alpha.api.md @@ -4,6 +4,8 @@ ```ts import { ApiRef } from '@backstage/core-plugin-api'; +import { Expand } from '@backstage/types'; +import { ExpandRecursive } from '@backstage/types'; import { Observable } from '@backstage/types'; import { TranslationMessages as TranslationMessages_2 } from '@backstage/core-plugin-api/alpha'; import { TranslationRef as TranslationRef_2 } from '@backstage/core-plugin-api/alpha'; diff --git a/packages/frontend-plugin-api/report.api.md b/packages/frontend-plugin-api/report.api.md index acc9efed61..24c2ff6d3b 100644 --- a/packages/frontend-plugin-api/report.api.md +++ b/packages/frontend-plugin-api/report.api.md @@ -39,6 +39,7 @@ import { ErrorApi } from '@backstage/core-plugin-api'; import { ErrorApiError } from '@backstage/core-plugin-api'; import { ErrorApiErrorContext } from '@backstage/core-plugin-api'; import { errorApiRef } from '@backstage/core-plugin-api'; +import { Expand } from '@backstage/types'; import { FeatureFlag } from '@backstage/core-plugin-api'; import { FeatureFlagsApi } from '@backstage/core-plugin-api'; import { featureFlagsApiRef } from '@backstage/core-plugin-api'; diff --git a/packages/types/report.api.md b/packages/types/report.api.md index 8169a506ef..b6bfae0862 100644 --- a/packages/types/report.api.md +++ b/packages/types/report.api.md @@ -21,6 +21,20 @@ export type DeferredPromise< // @public export function durationToMilliseconds(duration: HumanDuration): number; +// @public +export type Expand = T extends infer O + ? { + [K in keyof O]: O[K]; + } + : never; + +// @public +export type ExpandRecursive = T extends infer O + ? { + [K in keyof O]: ExpandRecursive; + } + : never; + // @public export type HumanDuration = { years?: number;