chore: move expand to @backstage/types

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2025-01-02 09:11:01 +01:00
parent ad364d7c7c
commit b853a4d5bf
3 changed files with 70 additions and 0 deletions
+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';