import { ResourceInputs } from "../ResourceInputs"; import { SensitiveValue as ResourceSensitiveValue } from "../Resources/SensitveValue"; import { ContainerImageReference, PackageReference, SensitiveValue } from "@octopusdeploy/step-inputs"; import { RuntimePackageSelection } from "../RuntimePackageSelection"; import { RuntimeContainerImageSelection } from "../RuntimeContainerImageSelection"; // This file tests the RuntimeInputs mapped type. It is not a real test in that it does not do anything at runtime, // but rather it tests that the compiler behaves as expected and therefore "runs" at compile time describe("ResourceInputs", () => { test("primitives (excluding string) become unioned with strings to represent the bound value case", () => { assert, number | string>>(); assert, bigint | string>>(); assert, boolean | string>>(); assert, null | string>>(); assert, undefined | string>>(); assert, ResourceSensitiveValue | string>>(); }); test("package selection maps to runtime package selection", () => { assert, RuntimePackageSelection>>(); }); test("container image reference maps to runtime container image reference", () => { assert, RuntimeContainerImageSelection>>(); }); test("string are unchanged", () => { // strings don't need to have a separate bound value type // the string value is always assumed to be possibly bound, and variable resolution is always applied assert, string>>(); }); test("union types are unioned with strings", () => { assert, boolean | number | string>>(); assert, boolean | number | string>>(); }); test("inner type of an array is mapped", () => { // Primitive assert, Array>>(); // Union type assert>, Array>>(); // Object assert>, Array<{ y: boolean | string }>>>(); }); test("object types are preserved, and their properties are mapped", () => { // Primitive assert, { x: { y: boolean | string } }>>(); // Union type assert, { x: { y: boolean | number | string } }>>(); // Array assert }>, { x: Array }>>(); }); test("Complex types with nesting are supported", () => { assert< IsExactly< ResourceInputs<{ x: { y: boolean[]; z: Array<{ a: SensitiveValue }> } }>, { x: { y: Array; z: Array<{ a: ResourceSensitiveValue | string }> } } > >(); }); }); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore function assert() { // This test only performs compile time checks, so no runtime behaviour is required and this function can remain empty } // We don't want to distribute over conditional types in this mapped type // e.g. We want IsExactly to be `false`, since you can't assign a `boolean` to `string` // If we typed this as `T extends U ? U extends T ? "true" : "false" : "false""` then typescript would distribute over `string | object` // so then `IsExactly = IsExactly | IsExactly = "true" | "false"; // This is clearly not the result we want, but if we opt out of distributing over conditional types, then we would get the result we want // We can do this using a 1-tuple. Source: https://github.com/microsoft/TypeScript/issues/29368#issuecomment-453529532 type IsExactly = [T] extends [U] ? ([U] extends [T] ? "true" : "false") : "false";