Merge pull request #28346 from backstage/blam/expand

Move `Expand` types to `@backstage/types`
This commit is contained in:
Ben Lambert
2025-01-02 10:25:35 +01:00
committed by GitHub
12 changed files with 98 additions and 27 deletions
+7
View File
@@ -0,0 +1,7 @@
---
'@backstage/frontend-plugin-api': patch
'@backstage/core-plugin-api': patch
'@backstage/types': patch
---
Move `Expand` and `ExpandRecursive` to `@backstage/types`
@@ -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';
@@ -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<TMessages extends { [key in string]: string }> = {
: key]: TMessages[key];
};
/**
* Helper type that expands type hints
*
* @ignore
*/
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
/**
* Helper type that expands type hints recursively
*
* @ignore
*/
type ExpandRecursive<T> = T extends infer O
? { [K in keyof O]: ExpandRecursive<O[K]> }
: never;
/**
* Trim away whitespace
*
@@ -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';
@@ -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> = T extends infer O ? { [K in keyof O]: O[K] } : never;
/** @public */
export type CoreProgressProps = {};
@@ -15,7 +15,7 @@
*/
import { ApiHolder, AppNode } from '../apis';
import { Expand } from '../types';
import { Expand } from '@backstage/types';
import {
ResolveInputValueOverrides,
resolveInputOverrides,
@@ -15,7 +15,7 @@
*/
import { ApiHolder, AppNode } from '../apis';
import { Expand } from '../types';
import { Expand } from '@backstage/types';
import {
ExtensionDefinition,
ResolvedExtensionInputs,
@@ -15,7 +15,7 @@
*/
import { AppNode } from '../apis';
import { Expand } from '../types';
import { Expand } from '@backstage/types';
import { ResolvedExtensionInput } from './createExtension';
import {
ExtensionDataContainer,
+14
View File
@@ -21,6 +21,20 @@ export type DeferredPromise<
// @public
export function durationToMilliseconds(duration: HumanDuration): number;
// @public
export type Expand<T> = T extends infer O
? {
[K in keyof O]: O[K];
}
: never;
// @public
export type ExpandRecursive<T> = T extends infer O
? {
[K in keyof O]: ExpandRecursive<O[K]>;
}
: never;
// @public
export type HumanDuration = {
years?: number;
+38
View File
@@ -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<Test>;
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<Test>;
const result: Result = { a: 'string', b: { c: { e: 'string', d: true } } };
expect(result).toEqual({ a: 'string', b: { c: { e: 'string', d: true } } });
});
});
+31
View File
@@ -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> = T extends infer O ? { [K in keyof O]: O[K] } : never;
/**
* Helper type that expands type hints recursively
*
* @public
*/
export type ExpandRecursive<T> = T extends infer O
? { [K in keyof O]: ExpandRecursive<O[K]> }
: never;
+1
View File
@@ -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';