Skip to content

Commit

Permalink
Merge pull request backstage#28346 from backstage/blam/expand
Browse files Browse the repository at this point in the history
Move `Expand` types to `@backstage/types`
  • Loading branch information
benjdlambert authored Jan 2, 2025
2 parents ad364d7 + f9a6bd1 commit 5937051
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 27 deletions.
7 changes: 7 additions & 0 deletions .changeset/long-parrots-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@backstage/frontend-plugin-api': patch
'@backstage/core-plugin-api': patch
'@backstage/types': patch
---

Move `Expand` and `ExpandRecursive` to `@backstage/types`
2 changes: 2 additions & 0 deletions packages/core-plugin-api/report-alpha.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
18 changes: 1 addition & 17 deletions packages/core-plugin-api/src/apis/definitions/TranslationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions packages/frontend-plugin-api/report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 0 additions & 7 deletions packages/frontend-plugin-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-plugin-api/src/wiring/createExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { ApiHolder, AppNode } from '../apis';
import { Expand } from '../types';
import { Expand } from '@backstage/types';
import {
ResolveInputValueOverrides,
resolveInputOverrides,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { ApiHolder, AppNode } from '../apis';
import { Expand } from '../types';
import { Expand } from '@backstage/types';
import {
ExtensionDefinition,
ResolvedExtensionInputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { AppNode } from '../apis';
import { Expand } from '../types';
import { Expand } from '@backstage/types';
import { ResolvedExtensionInput } from './createExtension';
import {
ExtensionDataContainer,
Expand Down
14 changes: 14 additions & 0 deletions packages/types/report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions packages/types/src/expand.test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions packages/types/src/expand.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 5937051

Please sign in to comment.