From b853a4d5bf987fb523855348eaece400a86db667 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 2 Jan 2025 09:11:01 +0100 Subject: [PATCH] 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';