Hi all is there an easy was to convert `Output<...
# typescript
p
Hi all is there an easy was to convert
Output<Object<string>>
to
Object<Input<string>>
without having to explicitly list all the fields?
h
Is
Object
your own type in your example, something like this but with more fields?
Copy code
interface Object<T> { field: T }
Also, can you give more detail as to what's requiring the input to be in the latter form? Often input types are nested to give you flexibility on which way round your input's are structured e.g.
Copy code
type ExampleInput = pulumi.Input<{
  field: pulumi.Input<string>;
}>
p
Some background: We have an extended ResourceOptions library that helps us explicitly require that a provider be for azure/aws, etc. It also does other things but lets keep it simple for now… I’m trying to write some helper functions that take args and build out these options so that we can pass stack references outputs to them.
Copy code
export function AzureMinimal(
    name: string,
    args: MinimalOptionsArgs<pulumi.Input<string>>,
    opts?: ProviderOptions,
): AzureMinimalOptions {
    const provider = new azure.Provider(
        name,
        {
            subscriptionId: args.subscriptionId,
            tenantId: args.tenantId,
        },
        opts,
    )
    return {
        ...args,
        provider,
    }
}
This works except when calling it I have to explicitly pass fields… Works…
Copy code
options.AzureMinimal('azure', {
    subscriptionId: cloud.metadata.subscriptionId,
    tenantId: 'some-uuid,
}),
Doesnt work…
Copy code
options.AzureMinimal('azure', {
    ...cloud.metadata,
    tenantId: 'some-uuid,
}),
Copy code
interface MinimalOptionsArgs<StringOrIntputString> extends CustomResourceOptions {
    subscriptionId: StringOrIntputString
    tenantId: StringOrIntputString
}
(This is obviously fine for minimal opts but some get a bit larger)
I basically don’t want the caller to have to list out all the fields, I want to do it within the function.
apologies that this is a bit muddled…
the latter passes through an undefined subscription + tenant id
h
Ah - so the Typescript compiler is happy, but it fails with an undefined value at runtime?
p
yes
h
Assuming
cloud.metadata
is an output of your desired fields, could you try doing the following?
Copy code
options.AzureMinimal('azure', cloud.metadata.apply(metadata => ({
    ...metadata,
    tenantId: 'some-uuid,
}))),
p
that works 🙂
thanks
I’ll try to add some defensive coding in the function to protect from undefineds
h
Great! Happy to help ☺️